docker

package
v1.50.25 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2025 License: MIT Imports: 15 Imported by: 2

Documentation

Index

Constants

View Source
const RetryAttempts = 3

Variables

View Source
var LinuxPlatformImageRetrier = func(l zerolog.Logger, startErr error, req tc.GenericContainerRequest) (tc.Container, error) {

	if startErr == nil {
		return nil, startErr
	}

	req.Reuse = false

	if !strings.Contains(startErr.Error(), "No such image") {
		l.Debug().
			Str("Start error", startErr.Error()).
			Str("Retrier", "PlatformImageRetrier").
			Msgf("Won't try to start %s container again, unsupported error", req.Name)
		return nil, startErr
	}

	l.Debug().
		Str("Start error", startErr.Error()).
		Str("Retrier", "PlatformImageRetrier").
		Msgf("Attempting to start %s container", req.Name)

	originalPlatform := req.ImagePlatform
	req.ImagePlatform = "linux/x86_64"

	removeErr := removeContainer(req)
	if removeErr != nil {
		l.Error().Err(removeErr).Msgf("Failed to remove %s container to initiate restart", req.Name)
		return nil, removeErr
	}

	ct, err := tc.GenericContainer(testcontext.Get(nil), req)
	if err == nil {
		l.Debug().
			Str("Retrier", "PlatformImageRetrier").
			Msgf("Successfully started %s container", req.Name)
		return ct, nil
	}

	req.ImagePlatform = originalPlatform

	if ct != nil {
		err := ct.Terminate(testcontext.Get(nil))
		if err != nil {
			l.Error().Err(err).Msgf("Cannot terminate %s container to initiate restart", req.Name)
			return nil, err
		}
	}

	l.Debug().
		Str("Original start error", startErr.Error()).
		Str("Current start error", err.Error()).
		Str("Retrier", "PlatformImageRetrier").
		Msgf("Failed to start %s container,", req.Name)

	return nil, startErr
}
View Source
var NaiveRetrier = func(l zerolog.Logger, startErr error, req tc.GenericContainerRequest) (tc.Container, error) {
	l.Debug().
		Str("Start error", startErr.Error()).
		Str("Retrier", "NaiveRetrier").
		Msgf("Attempting to start %s container", req.Name)

	req.Reuse = false

	removeErr := removeContainer(req)
	if removeErr != nil {
		l.Error().Err(removeErr).Msgf("Failed to remove %s container to initiate restart", req.Name)
		return nil, removeErr
	}

	ct, err := tc.GenericContainer(testcontext.Get(nil), req)
	if err == nil {
		l.Debug().
			Str("Retrier", "NaiveRetrier").
			Msgf("Successfully started %s container", req.Name)
		return ct, nil
	}
	if ct != nil {
		err := ct.Terminate(testcontext.Get(nil))
		if err != nil {
			l.Error().
				Err(err).
				Msgf("Cannot terminate %s container to initiate restart", req.Name)
			return nil, err
		}
	}

	l.Debug().
		Str("Original start error", startErr.Error()).
		Str("Current start error", err.Error()).
		Str("Retrier", "NaiveRetrier").
		Msgf("Failed to start %s container,", req.Name)

	return nil, startErr
}

Functions

func CreateNetwork

func CreateNetwork(l zerolog.Logger) (*tc.DockerNetwork, error)

CreateNetwork initializes a new Docker network with a unique name. It ensures no duplicate networks exist and returns the created network or an error if the operation fails.

func StartContainerWithRetry

func StartContainerWithRetry(l zerolog.Logger, req tc.GenericContainerRequest, retriers ...StartContainerRetrier) (tc.Container, error)

StartContainerWithRetry attempts to start a container with 3 retry attempts. It will try to start the container with the provided retriers, if none are provided it will use the default retriers. Default being: 1. tries to download image for "linux/x86_64" platform 2. simply starts again without changing anything

func WriteAllContainersLogs added in v1.50.19

func WriteAllContainersLogs(logger zerolog.Logger, directory string) error

WriteAllContainersLogs writes all Docker container logs to the default logs directory

Types

type StartContainerRetrier

type StartContainerRetrier func(l zerolog.Logger, startErr error, req tc.GenericContainerRequest) (tc.Container, error)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL