docker

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: Apache-2.0 Imports: 18 Imported by: 16

Documentation

Overview

Package docker allows to interact with Docker and docker compose resources.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoContainerFound = errors.New("no container found")

ErrNoContainerFound is returned when no container is found with the given ID.

Functions

func Build deprecated added in v0.22.3

func Build(t testing.TestingT, path string, options *BuildOptions)

Build runs the 'docker build' command at the given path with the given options and fails the test if there are any errors.

Deprecated: Use BuildContext instead.

func BuildContext added in v1.0.0

func BuildContext(t testing.TestingT, ctx context.Context, path string, options *BuildOptions)

BuildContext runs the 'docker build' command at the given path with the given options and fails the test if there are any errors. The ctx parameter supports cancellation and timeouts.

func BuildContextE added in v1.0.0

func BuildContextE(t testing.TestingT, ctx context.Context, path string, options *BuildOptions) error

BuildContextE runs the 'docker build' command at the given path with the given options and returns any errors. The ctx parameter supports cancellation and timeouts.

func BuildE deprecated added in v0.22.3

func BuildE(t testing.TestingT, path string, options *BuildOptions) error

BuildE runs the 'docker build' command at the given path with the given options and returns any errors.

Deprecated: Use BuildContextE instead.

func DeleteImage deprecated added in v0.40.2

func DeleteImage(t testing.TestingT, img string, logger *logger.Logger)

DeleteImage removes a docker image using the Docker CLI. This will fail the test if there is an error.

Deprecated: Use DeleteImageContext instead.

func DeleteImageContext added in v1.0.0

func DeleteImageContext(t testing.TestingT, ctx context.Context, img string, logger *logger.Logger)

DeleteImageContext removes a docker image using the Docker CLI. This will fail the test if there is an error. The ctx parameter supports cancellation and timeouts.

func DeleteImageContextE added in v1.0.0

func DeleteImageContextE(t testing.TestingT, ctx context.Context, img string, logger *logger.Logger) error

DeleteImageContextE removes a docker image using the Docker CLI. The ctx parameter supports cancellation and timeouts.

func DeleteImageE deprecated added in v0.40.2

func DeleteImageE(t testing.TestingT, img string, logger *logger.Logger) error

DeleteImageE removes a docker image using the Docker CLI.

Deprecated: Use DeleteImageContextE instead.

func DoesImageExist deprecated added in v0.40.2

func DoesImageExist(t testing.TestingT, imgLabel string, logger *logger.Logger) bool

DoesImageExist lists the images in the docker daemon and returns true if the given image label (repo:tag) exists. This will fail the test if there is an error.

Deprecated: Use DoesImageExistContext instead.

func DoesImageExistContext added in v1.0.0

func DoesImageExistContext(t testing.TestingT, ctx context.Context, imgLabel string, logger *logger.Logger) bool

DoesImageExistContext lists the images in the docker daemon and returns true if the given image label (repo:tag) exists. This will fail the test if there is an error. The ctx parameter supports cancellation and timeouts.

func GetDockerHost added in v0.30.11

func GetDockerHost() string

GetDockerHost returns the name or address of the host on which the Docker engine is running.

func GetDockerHostFromEnv added in v1.0.0

func GetDockerHostFromEnv(env []string) string

GetDockerHostFromEnv parses the DOCKER_HOST value from the given environment variable list and returns the host portion. If DOCKER_HOST is absent, empty, or uses a local transport (unix/npipe), it returns "localhost".

func GitCloneAndBuild deprecated added in v0.40.2

func GitCloneAndBuild(
	t testing.TestingT,
	repo string,
	ref string,
	path string,
	dockerBuildOpts *BuildOptions,
)

GitCloneAndBuild builds a new Docker image from a given Git repo. This function will clone the given repo at the specified ref, and call the docker build command on the cloned repo from the given relative path (relative to repo root). This will fail the test if there are any errors.

Deprecated: Use GitCloneAndBuildContext instead.

func GitCloneAndBuildContext added in v1.0.0

func GitCloneAndBuildContext(
	t testing.TestingT,
	ctx context.Context,
	repo string,
	ref string,
	path string,
	dockerBuildOpts *BuildOptions,
)

GitCloneAndBuildContext builds a new Docker image from a given Git repo. This function will clone the given repo at the specified ref, and call the docker build command on the cloned repo from the given relative path (relative to repo root). This will fail the test if there are any errors. The ctx parameter supports cancellation and timeouts.

func GitCloneAndBuildContextE added in v1.0.0

func GitCloneAndBuildContextE(
	t testing.TestingT,
	ctx context.Context,
	repo string,
	ref string,
	path string,
	dockerBuildOpts *BuildOptions,
) error

GitCloneAndBuildContextE builds a new Docker image from a given Git repo. This function will clone the given repo at the specified ref, and call the docker build command on the cloned repo from the given relative path (relative to repo root). The ctx parameter supports cancellation and timeouts.

func GitCloneAndBuildE deprecated added in v0.40.2

func GitCloneAndBuildE(
	t testing.TestingT,
	repo string,
	ref string,
	path string,
	dockerBuildOpts *BuildOptions,
) error

GitCloneAndBuildE builds a new Docker image from a given Git repo. This function will clone the given repo at the specified ref, and call the docker build command on the cloned repo from the given relative path (relative to repo root).

Deprecated: Use GitCloneAndBuildContextE instead.

func Push deprecated added in v0.37.5

func Push(t testing.TestingT, logger *logger.Logger, tag string)

Push runs the 'docker push' command to push the given tag. This will fail the test if there are any errors.

Deprecated: Use PushContext instead.

func PushContext added in v1.0.0

func PushContext(t testing.TestingT, ctx context.Context, logger *logger.Logger, tag string)

PushContext runs the 'docker push' command to push the given tag. This will fail the test if there are any errors. The ctx parameter supports cancellation and timeouts.

func PushContextE added in v1.0.0

func PushContextE(t testing.TestingT, ctx context.Context, logger *logger.Logger, tag string) error

PushContextE runs the 'docker push' command to push the given tag. The ctx parameter supports cancellation and timeouts.

func PushE deprecated added in v0.37.5

func PushE(t testing.TestingT, logger *logger.Logger, tag string) error

PushE runs the 'docker push' command to push the given tag.

Deprecated: Use PushContextE instead.

func Run deprecated added in v0.22.3

func Run(t testing.TestingT, image string, options *RunOptions) string

Run runs the 'docker run' command on the given image with the given options and return stdout/stderr. This method fails the test if there are any errors.

Deprecated: Use RunContext instead.

func RunAndGetID deprecated added in v0.26.5

func RunAndGetID(t testing.TestingT, image string, options *RunOptions) string

RunAndGetID runs the 'docker run' command on the given image with the given options and returns the container ID that is returned in stdout. This method fails the test if there are any errors.

Deprecated: Use RunAndGetIDContext instead.

func RunAndGetIDContext added in v1.0.0

func RunAndGetIDContext(t testing.TestingT, ctx context.Context, image string, options *RunOptions) string

RunAndGetIDContext runs the 'docker run' command on the given image with the given options and returns the container ID that is returned in stdout. This method fails the test if there are any errors. The ctx parameter supports cancellation and timeouts.

func RunAndGetIDContextE added in v1.0.0

func RunAndGetIDContextE(t testing.TestingT, ctx context.Context, image string, options *RunOptions) (string, error)

RunAndGetIDContextE runs the 'docker run' command on the given image with the given options and returns the container ID that is returned in stdout, or any error. The ctx parameter supports cancellation and timeouts.

func RunAndGetIDE deprecated added in v0.26.5

func RunAndGetIDE(t testing.TestingT, image string, options *RunOptions) (string, error)

RunAndGetIDE runs the 'docker run' command on the given image with the given options and returns the container ID that is returned in stdout, or any error.

Deprecated: Use RunAndGetIDContextE instead.

func RunContext added in v1.0.0

func RunContext(t testing.TestingT, ctx context.Context, image string, options *RunOptions) string

RunContext runs the 'docker run' command on the given image with the given options and returns stdout/stderr. This method fails the test if there are any errors. The ctx parameter supports cancellation and timeouts.

func RunContextE added in v1.0.0

func RunContextE(t testing.TestingT, ctx context.Context, image string, options *RunOptions) (string, error)

RunContextE runs the 'docker run' command on the given image with the given options and returns stdout/stderr, or any error. The ctx parameter supports cancellation and timeouts.

func RunDockerCompose deprecated

func RunDockerCompose(t testing.TestingT, options *Options, args ...string) string

RunDockerCompose runs docker compose with the given arguments and options and return stdout/stderr.

Deprecated: Use RunDockerComposeContext instead.

func RunDockerComposeAndGetStdOut deprecated added in v0.30.7

func RunDockerComposeAndGetStdOut(t testing.TestingT, options *Options, args ...string) string

RunDockerComposeAndGetStdOut runs docker compose with the given arguments and options and returns only stdout.

Deprecated: Use RunDockerComposeAndGetStdOutContext instead.

func RunDockerComposeAndGetStdOutContext added in v1.0.0

func RunDockerComposeAndGetStdOutContext(t testing.TestingT, ctx context.Context, options *Options, args ...string) string

RunDockerComposeAndGetStdOutContext runs docker compose with the given arguments and options and returns only stdout. This method fails the test if there are any errors. The ctx parameter supports cancellation and timeouts.

func RunDockerComposeContext added in v1.0.0

func RunDockerComposeContext(t testing.TestingT, ctx context.Context, options *Options, args ...string) string

RunDockerComposeContext runs docker compose with the given arguments and options and returns stdout/stderr. This method fails the test if there are any errors. The ctx parameter supports cancellation and timeouts.

func RunDockerComposeContextE added in v1.0.0

func RunDockerComposeContextE(t testing.TestingT, ctx context.Context, options *Options, args ...string) (string, error)

RunDockerComposeContextE runs docker compose with the given arguments and options and returns stdout/stderr, or any error. The ctx parameter supports cancellation and timeouts.

func RunDockerComposeE deprecated

func RunDockerComposeE(t testing.TestingT, options *Options, args ...string) (string, error)

RunDockerComposeE runs docker compose with the given arguments and options and return stdout/stderr.

Deprecated: Use RunDockerComposeContextE instead.

func RunE deprecated added in v0.22.3

func RunE(t testing.TestingT, image string, options *RunOptions) (string, error)

RunE runs the 'docker run' command on the given image with the given options and return stdout/stderr, or any error.

Deprecated: Use RunContextE instead.

func Stop deprecated added in v0.23.2

func Stop(t testing.TestingT, containers []string, options *StopOptions) string

Stop runs the 'docker stop' command for the given containers and return the stdout/stderr. This method fails the test if there are any errors

Deprecated: Use StopContext instead.

func StopContext added in v1.0.0

func StopContext(t testing.TestingT, ctx context.Context, containers []string, options *StopOptions) string

StopContext runs the 'docker stop' command for the given containers and returns stdout/stderr. This method fails the test if there are any errors. The ctx parameter supports cancellation and timeouts.

func StopContextE added in v1.0.0

func StopContextE(t testing.TestingT, ctx context.Context, containers []string, options *StopOptions) (string, error)

StopContextE runs the 'docker stop' command for the given containers and returns stdout/stderr, or any error. The ctx parameter supports cancellation and timeouts.

func StopE deprecated added in v0.23.2

func StopE(t testing.TestingT, containers []string, options *StopOptions) (string, error)

StopE runs the 'docker stop' command for the given containers and returns any errors.

Deprecated: Use StopContextE instead.

Types

type BuildOptions added in v0.22.3

type BuildOptions struct {
	// Additional environment variables to pass in when running docker build command.
	Env map[string]string

	// Set a logger that should be used. See the logger package for more info.
	Logger *logger.Logger

	// Target build arg to pass to the 'docker build' command
	Target string

	// Tags for the Docker image
	Tags []string

	// Build args to pass the 'docker build' command
	BuildArgs []string

	// All architectures to target in a multiarch build. Configuring this variable will cause terratest to use docker
	// buildx to construct multiarch images.
	// You can read more about multiarch docker builds in the official documentation for buildx:
	// https://docs.docker.com/buildx/working-with-buildx/
	// NOTE: This list does not automatically include the current platform. For example, if you are building images on
	// an Apple Silicon based MacBook, and you configure this variable to []string{"linux/amd64"} to build an amd64
	// image, the buildx command will not automatically include linux/arm64 - you must include that explicitly.
	Architectures []string

	// Custom CLI options that will be passed as-is to the 'docker build' command. This is an "escape hatch" that allows
	// Terratest to not have to support every single command-line option offered by the 'docker build' command, and
	// solely focus on the most important ones.
	OtherOptions []string

	// Whether or not to push images directly to the registry on build. Note that for multiarch images (Architectures is
	// not empty), this must be true to ensure availability of all architectures - only the image for the current
	// platform will be loaded into the daemon (due to a limitation of the docker daemon), so you won't be able to run a
	// `docker push` command later to push the multiarch image.
	// See https://github.com/moby/moby/pull/38738 for more info on the limitation of multiarch images in docker daemon.
	Push bool

	// Whether or not to load the image into the docker daemon at the end of a multiarch build so that it can be used
	// locally. Note that this is only used when Architectures is set, and assumes the current architecture is already
	// included in the Architectures list.
	Load bool

	// Whether ot not to enable buildkit. You can find more information about buildkit here https://docs.docker.com/build/buildkit/#getting-started.
	EnableBuildKit bool
}

BuildOptions defines options that can be passed to the 'docker build' command.

type ContainerInspect added in v0.26.5

type ContainerInspect struct {
	// ID of the inspected container
	ID string

	// Name of the inspected container
	Name string

	// time.Time that the container was created
	Created time.Time

	// String representing the container's status
	Status string

	// String with the container's error message, if there is any
	Error string

	// Ports exposed by the container
	Ports []Port

	// Volume bindings made to the container
	Binds []VolumeBind

	// Health check
	Health HealthCheck

	// Container's exit code
	ExitCode uint8

	// Whether the container is currently running or not
	Running bool
}

ContainerInspect defines the output of the Inspect method, with the options returned by 'docker inspect' converted into a more friendly and testable interface

func Inspect deprecated added in v0.26.5

func Inspect(t testing.TestingT, id string) *ContainerInspect

Inspect runs the 'docker inspect {container id}' command and returns a ContainerInspect struct, converted from the output JSON, along with any errors

Deprecated: Use InspectContext instead.

func InspectContext added in v1.0.0

func InspectContext(t testing.TestingT, ctx context.Context, id string) *ContainerInspect

InspectContext runs the 'docker inspect {container id}' command and returns a ContainerInspect struct, converted from the output JSON. This will fail the test if there are any errors. The ctx parameter supports cancellation and timeouts.

func InspectContextE added in v1.0.0

func InspectContextE(t testing.TestingT, ctx context.Context, id string) (*ContainerInspect, error)

InspectContextE runs the 'docker inspect {container id}' command and returns a ContainerInspect struct, converted from the output JSON, along with any errors. The ctx parameter supports cancellation and timeouts.

func InspectE deprecated added in v0.26.5

func InspectE(t testing.TestingT, id string) (*ContainerInspect, error)

InspectE runs the 'docker inspect {container id}' command and returns a ContainerInspect struct, converted from the output JSON, along with any errors

Deprecated: Use InspectContextE instead.

func (*ContainerInspect) GetExposedHostPort added in v0.32.14

func (c *ContainerInspect) GetExposedHostPort(containerPort uint16) uint16

GetExposedHostPort returns an exposed host port according to requested container port. Returns 0 if the requested port is not exposed.

type HealthCheck added in v0.27.1

type HealthCheck struct {
	// Health check status
	Status string `json:"Status"`

	// Log of failures
	Log []HealthLog `json:"Log"`

	// Current count of failing health checks
	FailingStreak uint8 `json:"FailingStreak"`
}

HealthCheck represents the current health history of the container

type HealthLog added in v0.27.1

type HealthLog struct {
	// Start time of health check
	Start string `json:"Start"`

	// End time of health check
	End string `json:"End"`

	// Output of health check
	Output string `json:"Output"`

	// Exit code of health check
	ExitCode uint8 `json:"ExitCode"`
}

HealthLog represents the output of a single Health check of the container

type Image added in v0.40.2

type Image struct {
	// ID is the image ID in docker, and can be used to identify the image in place of the repo and tag.
	ID string `json:"ID"`

	// Repository is the image repository.
	Repository string `json:"Repository"`

	// Tag is the image tag wichin the repository.
	Tag string `json:"Tag"`

	// CreatedAt represents a timestamp for when the image was created.
	CreatedAt string `json:"CreatedAt"`

	// CreatedSince is a diff between when the image was created to now.
	CreatedSince string `json:"CreatedSince"`

	// SharedSize is the amount of space that an image shares with another one (i.e. their common data).
	SharedSize string `json:"SharedSize"`

	// UniqueSize is the amount of space that is only used by a given image.
	UniqueSize string `json:"UniqueSize"`

	// VirtualSize is the total size of the image, combining SharedSize and UniqueSize.
	VirtualSize string `json:"VirtualSize"`

	// Containers represents the list of containers that are using the image.
	Containers string `json:"Containers"`

	// Digest is the hash digest of the image, if requested.
	Digest string `json:"Digest"`
}

Image represents a docker image, and exports all the fields that the docker images command returns for the image.

func ListImages deprecated added in v0.40.2

func ListImages(t testing.TestingT, logger *logger.Logger) []Image

ListImages calls docker images using the Docker CLI to list the available images on the local docker daemon.

Deprecated: Use ListImagesContext instead.

func ListImagesContext added in v1.0.0

func ListImagesContext(t testing.TestingT, ctx context.Context, logger *logger.Logger) []Image

ListImagesContext calls docker images using the Docker CLI to list the available images on the local docker daemon. This will fail the test if there is an error. The ctx parameter supports cancellation and timeouts.

func ListImagesContextE added in v1.0.0

func ListImagesContextE(t testing.TestingT, ctx context.Context, logger *logger.Logger) ([]Image, error)

ListImagesContextE calls docker images using the Docker CLI to list the available images on the local docker daemon. The ctx parameter supports cancellation and timeouts.

func ListImagesE deprecated added in v0.40.2

func ListImagesE(t testing.TestingT, logger *logger.Logger) ([]Image, error)

ListImagesE calls docker images using the Docker CLI to list the available images on the local docker daemon.

Deprecated: Use ListImagesContextE instead.

func (Image) String added in v0.40.2

func (image Image) String() string

String returns the image reference in "repository:tag" format.

type Options

type Options struct {
	EnvVars    map[string]string
	Logger     *logger.Logger
	WorkingDir string

	ProjectName string

	// Whether ot not to enable buildkit. You can find more information about buildkit here https://docs.docker.com/build/buildkit/#getting-started.
	EnableBuildKit bool
}

Options are Docker options.

type Port added in v0.26.5

type Port struct {
	Protocol      string
	HostPort      uint16
	ContainerPort uint16
}

Port represents a single port mapping exported by the container

type RunOptions added in v0.22.3

type RunOptions struct {
	// Set a logger that should be used. See the logger package for more info.
	Logger *logger.Logger

	// Override the default ENTRYPOINT of the Docker image
	Entrypoint string

	// Assign a name to the container
	Name string

	// Set platform
	Platform string

	// Username or UID
	User string

	// Override the default COMMAND of the Docker image
	Command []string

	// Set environment variables
	EnvironmentVariables []string

	// Bind mount these volume(s) when running the container
	Volumes []string

	// Custom CLI options that will be passed as-is to the 'docker run' command. This is an "escape hatch" that allows
	// Terratest to not have to support every single command-line option offered by the 'docker run' command, and
	// solely focus on the most important ones.
	OtherOptions []string

	// If set to true, pass the --detach flag to 'docker run' to run the container in the background
	Detach bool

	// If set to true, pass the --init flag to 'docker run' to run an init inside the container that forwards signals
	// and reaps processes
	Init bool

	// If set to true, pass the --privileged flag to 'docker run' to give extended privileges to the container
	Privileged bool

	// If set to true, pass the --rm flag to 'docker run' to automatically remove the container when it exits
	Remove bool

	// If set to true, pass the -tty flag to 'docker run' to allocate a pseudo-TTY
	Tty bool
}

RunOptions defines options that can be passed to the 'docker run' command.

type StopOptions added in v0.23.2

type StopOptions struct {
	// Set a logger that should be used. See the logger package for more info.
	Logger *logger.Logger

	// Seconds to wait for stop before killing the container (default 10)
	Time int
}

StopOptions defines the options that can be passed to the 'docker stop' command

type VolumeBind added in v0.26.5

type VolumeBind struct {
	Source      string
	Destination string
}

VolumeBind represents a single volume binding made to the container

Jump to

Keyboard shortcuts

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