Help us shape the future of Go! Take the 2026 Go Developer Survey

docker

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package docker is a thin wrapper over the Docker Engine API client that exposes only the operations shed needs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsConflict

func IsConflict(err error) bool

IsConflict reports whether err means that an object is still in use.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound reports whether err means that a Docker object does not exist.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client talks to a Docker Engine.

func New

func New() (*Client, error)

New returns a Client configured from the standard DOCKER_* environment variables. The API version is negotiated with the daemon on first use.

func (*Client) Close

func (c *Client) Close() error

Close releases the resources held by c.

func (*Client) CopyFrom

func (c *Client) CopyFrom(ctx context.Context, id, path string) (io.ReadCloser, error)

CopyFrom returns a tar stream of path in container id, using the Docker archive API. It works on created and stopped containers. The caller closes the stream.

func (*Client) CopyTo

func (c *Client) CopyTo(ctx context.Context, id, dir string, r io.Reader) error

CopyTo extracts the tar stream r into the directory dir of container id. The directory must exist.

func (*Client) Create

func (c *Client) Create(ctx context.Context, spec RunSpec) (string, error)

Create creates a container from spec without starting it and returns its ID. An empty spec.Network leaves the container with no networking configuration, so it uses the daemon's default network if it is ever started.

func (*Client) CreateIsolated

func (c *Client) CreateIsolated(ctx context.Context, id, name string, labels map[string]string) (string, error)

CreateIsolated creates a stopped copy of the container id, named name and labeled with labels instead of the original's labels, and returns its ID. The copy runs the same image, entrypoint, command, environment, user, and mounts, with the same resource limits. It has no network but loopback and publishes no ports, so nothing outside it can connect to it, and Docker never restarts it, not even when the daemon starts again. Anonymous volumes of the original are not shared: the copy gets its own.

func (*Client) DisconnectNetwork

func (c *Client) DisconnectNetwork(ctx context.Context, name, id string) error

DisconnectNetwork disconnects the container id from the network name, which also drops its DNS aliases there.

func (*Client) EnsureNetwork

func (c *Client) EnsureNetwork(ctx context.Context, name string) error

EnsureNetwork creates the bridge network name if it does not exist.

func (*Client) EnsureVolume

func (c *Client) EnsureVolume(ctx context.Context, name string) error

EnsureVolume creates the named volume if it does not exist.

func (*Client) Exec

func (c *Client) Exec(ctx context.Context, id string, cmd []string, stdin io.Reader, stdout, stderr io.Writer) error

Exec runs cmd in the running container id. stdin may be nil. stdout and stderr receive the command's output separately. Output is streamed, never buffered in memory. It returns an *ExitError when the command exits non-zero.

stdin is copied in full and then closed, so the command sees EOF. If reading stdin fails, the connection is aborted rather than closed cleanly, so the command cannot mistake truncated input for complete input, and Exec returns the read error. Exec does not return until the copy of stdin has finished, so a Read on stdin must not block forever once the command is gone.

Canceling ctx aborts the stream and returns ctx's error. The command itself may keep running in the container.

func (*Client) ExposedPorts

func (c *Client) ExposedPorts(ctx context.Context, ref string) ([]int, error)

ExposedPorts returns the TCP ports that the image ref declares as exposed, in ascending order.

func (*Client) Inspect

func (c *Client) Inspect(ctx context.Context, id string) (Container, error)

Inspect returns the state of the container.

func (*Client) List

func (c *Client) List(ctx context.Context, labels map[string]string) ([]Container, error)

List returns all containers, stopped ones included, that have every one of the given labels. An empty label value matches any value.

func (*Client) ListImages

func (c *Client) ListImages(ctx context.Context, repo string) ([]Image, error)

ListImages returns the local images tagged in repository repo, one entry per tag.

func (*Client) Logs

func (c *Client) Logs(ctx context.Context, id string, tail int, follow bool, w io.Writer) error

Logs copies the container's stdout and stderr to w, each line prefixed with its RFC 3339 timestamp and a space. A negative tail means all lines. With follow, Logs blocks until ctx is done or the container exits; canceling ctx is not an error.

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

Ping checks that the daemon is reachable.

func (*Client) PullImage

func (c *Client) PullImage(ctx context.Context, ref string, w io.Writer) error

PullImage pulls ref and writes human-readable progress lines to w.

func (*Client) ReconnectNetwork

func (c *Client) ReconnectNetwork(ctx context.Context, name, id string, aliases []string) error

ReconnectNetwork replaces the DNS aliases, besides its name, of the container id on the network name, to which it must be connected. Docker cannot change the aliases of a connected container, so it is disconnected and connected again, asking for the addresses it had so that they do not change.

func (*Client) Remove

func (c *Client) Remove(ctx context.Context, id string) error

Remove force-removes the container and its anonymous volumes, such as those Docker creates for the VOLUME paths of its image. Named volumes (like shed-vol-*) are never removed by RemoveVolumes: Docker deletes only the volumes it generated names for, like docker rm -v. A missing container is not an error.

func (*Client) RemoveImage

func (c *Client) RemoveImage(ctx context.Context, ref string) error

RemoveImage removes the image ref. A missing image is not an error.

func (*Client) RemoveNetwork

func (c *Client) RemoveNetwork(ctx context.Context, name string) error

RemoveNetwork removes the network name. A missing network is not an error.

func (*Client) RemoveVolume

func (c *Client) RemoveVolume(ctx context.Context, name string) error

RemoveVolume removes the named volume. A missing volume is not an error.

func (*Client) ResolveImage

func (c *Client) ResolveImage(ctx context.Context, ref string) (string, error)

ResolveImage returns the immutable local ID of the image referenced by ref.

func (*Client) Restart

func (c *Client) Restart(ctx context.Context, id string, timeout time.Duration) error

Restart stops the container, killing it if it has not exited within timeout, and starts it again.

func (*Client) Run

func (c *Client) Run(ctx context.Context, spec RunSpec) (string, error)

Run creates and starts a container and returns its ID. The container is restarted unless explicitly stopped.

A failed start can be ambiguous: Docker may have started the container although the request failed, for example when the connection dropped. Run then removes the container. If that removal fails too, Run returns the container's ID along with the error, since the container may be running.

func (*Client) Start

func (c *Client) Start(ctx context.Context, id string) error

Start starts an existing, stopped container.

func (*Client) Stats

func (c *Client) Stats(ctx context.Context, id string) (Stats, error)

Stats returns a single sample of the container's resource usage.

func (*Client) Stop

func (c *Client) Stop(ctx context.Context, id string, timeout time.Duration) error

Stop stops the container, killing it if it has not exited within timeout.

func (*Client) VolumeExists

func (c *Client) VolumeExists(ctx context.Context, name string) (bool, error)

VolumeExists reports whether the named volume exists. Unlike EnsureVolume, it never creates one.

type Container

type Container struct {
	ID      string
	Name    string
	Image   string
	State   string // created, running, paused, restarting, removing, exited or dead
	Running bool
	// ExitCode is the exit code of the last run.
	ExitCode int
	// IPs maps network name to the container's IP address on it.
	IPs    map[string]string
	Labels map[string]string
	// Volumes lists the named volumes mounted into the container.
	Volumes []string
	// CPULimit is the configured CPU limit in cores; zero means unlimited.
	// Only Inspect sets it.
	CPULimit float64
	// MemoryLimit is the configured memory limit in bytes; zero means
	// unlimited. Only Inspect sets it.
	MemoryLimit int64
}

Container is the observed state of a container.

type ExitError

type ExitError struct {
	Code int
}

ExitError reports that a command run by Exec exited with a non-zero status.

func (*ExitError) Error

func (e *ExitError) Error() string

Error implements the error interface.

type Image

type Image struct {
	Ref     string // repo:tag
	Created time.Time
}

Image is a tagged local image.

type Mount

type Mount struct {
	Volume, Target string
	ReadOnly       bool
}

Mount attaches a named volume to a container path.

type PortBinding

type PortBinding struct {
	HostPort, ContainerPort int
}

PortBinding publishes a container TCP port on all host interfaces.

type RunSpec

type RunSpec struct {
	Name    string
	Image   string
	Cmd     []string // nil means the image default
	Env     []string // KEY=VALUE
	Labels  map[string]string
	Network string
	Aliases []string // DNS names on Network
	Mounts  []Mount
	Publish []PortBinding
	// CPUs is the CPU quota in cores and Memory the memory limit in bytes,
	// with no extra swap. Zero means unlimited.
	CPUs   float64
	Memory int64
}

RunSpec describes a container to create, and usually to start.

type Stats

type Stats struct {
	Read time.Time
	// CPUTotal is the CPU time the container has used, in nanoseconds.
	CPUTotal uint64
	// SystemCPU is the CPU time the host has used across all CPUs, in
	// nanoseconds; zero if the daemon does not report it.
	SystemCPU  uint64
	OnlineCPUs uint32
	// MemoryUsage is the memory in use in bytes, excluding inactive page
	// cache.
	MemoryUsage uint64
	// NetRx and NetTx are bytes received and sent, summed over all
	// interfaces.
	NetRx, NetTx uint64
	// DiskRead and DiskWrite are block device bytes read and written.
	DiskRead, DiskWrite uint64
}

Stats is a point-in-time sample of a container's resource usage. Counters are cumulative since the container started; rates come from the difference between two samples.

Jump to

Keyboard shortcuts

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