docker

package
v1.20.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package docker is a small Docker Engine API client built on the standard library. It implements only the calls the Agent uses, pinned to one API version, so the most privileged component carries no large SDK.

Index

Constants

View Source
const APIVersion = "1.44"

APIVersion is the Engine API version the Agent speaks (Docker 25 and later).

Variables

View Source
var ErrNotRunning = errors.New("the container is not running")

ErrNotRunning is returned when writing to a container that is not running; Docker would accept the write and drop it.

Functions

func IsConflict

func IsConflict(err error) bool

IsConflict reports whether err is a 409 from the Engine.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound reports whether err is a 404 from the Engine.

Types

type Client

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

Client talks to one Engine.

func New

func New(host string) (*Client, error)

New returns a client for host: "unix:///var/run/docker.sock" or "tcp://host:port".

func (*Client) ContainerCreate

func (c *Client) ContainerCreate(ctx context.Context, name string, cfg ContainerConfig) (string, error)

ContainerCreate creates a container and returns its ID.

func (*Client) ContainerInspect

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

ContainerInspect returns a container's state.

func (*Client) ContainerList

func (c *Client) ContainerList(ctx context.Context, label string) ([]ContainerSummary, error)

ContainerList lists all containers (running or not) carrying label ("key=value" or "key").

func (*Client) ContainerRemove

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

ContainerRemove removes a container, stopping it first.

func (*Client) ContainerStart

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

ContainerStart starts a container; starting a running one is a no-op.

func (*Client) ContainerStop

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

ContainerStop stops a container, killing it after timeout.

func (*Client) CopyFromContainer added in v1.12.0

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

CopyFromContainer returns a tar archive of path in a container's file system (it works on containers that never started). Symlinks are resolved within the container, never on the host.

func (*Client) CopyToContainer added in v1.11.0

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

CopyToContainer extracts a tar archive into a container's file system at dir. It works on stopped containers, and into their volumes even when the root file system is read-only.

func (*Client) ImageExists

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

ImageExists reports whether ref is present locally.

func (*Client) ImagePull

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

ImagePull pulls ref, waiting for the pull to finish.

func (*Client) Info

func (c *Client) Info(ctx context.Context) (Info, error)

Info returns facts about the Engine.

func (*Client) Logs added in v1.12.0

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

Logs streams a container's output: the last tail lines, then, with follow, new output until ctx ends or the container stops. Docker's stream framing is removed; stdout and stderr are interleaved in order.

func (*Client) NetworkConnect

func (c *Client) NetworkConnect(ctx context.Context, network, container string) error

NetworkConnect attaches a container to a network; attaching twice is a no-op.

func (*Client) NetworkEnsure

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

NetworkEnsure creates a bridge network unless it exists.

func (*Client) Output added in v1.12.0

func (c *Client) Output(ctx context.Context, id string, limit int64) (stdout, stderr []byte, err error)

Output returns a stopped container's complete stdout and stderr, each cut off at limit bytes.

func (*Client) Ping

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

Ping checks the Engine is reachable and supports APIVersion.

func (*Client) Stats added in v1.11.0

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

Stats returns one resource sample of a running container.

func (*Client) VolumeCreate

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

VolumeCreate creates a named volume; creating an existing one is a no-op.

func (*Client) VolumeExists

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

VolumeExists reports whether a volume exists.

func (*Client) VolumeRemove

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

VolumeRemove removes a volume.

func (*Client) Wait added in v1.12.0

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

Wait waits for a container to stop and returns its exit code.

func (*Client) WriteStdin added in v1.12.0

func (c *Client) WriteStdin(ctx context.Context, id string, data []byte) error

WriteStdin attaches to a running container's stdin and writes data, as if typed into its console. The container must have been created with OpenStdin.

type ContainerConfig

type ContainerConfig struct {
	Image      string   `json:"Image"`
	Entrypoint []string `json:"Entrypoint,omitempty"`
	Cmd        []string `json:"Cmd,omitempty"`
	WorkingDir string   `json:"WorkingDir,omitempty"`
	// OpenStdin keeps stdin open for the console.
	OpenStdin    bool                `json:"OpenStdin,omitempty"`
	Env          []string            `json:"Env,omitempty"`
	User         string              `json:"User,omitempty"`
	Labels       map[string]string   `json:"Labels,omitempty"`
	ExposedPorts map[string]struct{} `json:"ExposedPorts,omitempty"`
	HostConfig   HostConfig          `json:"HostConfig"`
}

ContainerConfig is the body of a container create request. Field names follow the Engine API.

type ContainerInfo

type ContainerInfo struct {
	ID     string `json:"Id"`
	Name   string `json:"Name"`
	Config struct {
		Image      string            `json:"Image"`
		Labels     map[string]string `json:"Labels"`
		User       string            `json:"User"`
		WorkingDir string            `json:"WorkingDir"`
		Cmd        []string          `json:"Cmd"`
	} `json:"Config"`
	State struct {
		Status     string    `json:"Status"`
		Running    bool      `json:"Running"`
		ExitCode   int       `json:"ExitCode"`
		OOMKilled  bool      `json:"OOMKilled"`
		StartedAt  time.Time `json:"StartedAt"`
		FinishedAt time.Time `json:"FinishedAt"`
	} `json:"State"`
	HostConfig      HostConfig `json:"HostConfig"`
	NetworkSettings struct {
		Networks map[string]struct {
			IPAddress string `json:"IPAddress"`
		} `json:"Networks"`
	} `json:"NetworkSettings"`
}

ContainerInfo is the part of an inspect answer the Agent uses.

type ContainerSummary

type ContainerSummary struct {
	ID     string            `json:"Id"`
	Names  []string          `json:"Names"`
	Labels map[string]string `json:"Labels"`
	State  string            `json:"State"`
}

ContainerSummary is one entry of a container list.

type Error

type Error struct {
	Status  int
	Message string
}

Error is an error answer from the Engine.

func (*Error) Error

func (e *Error) Error() string

type HostConfig

type HostConfig struct {
	NanoCPUs       int64                    `json:"NanoCpus,omitempty"`
	Memory         int64                    `json:"Memory,omitempty"`
	MemorySwap     int64                    `json:"MemorySwap,omitempty"`
	PidsLimit      *int64                   `json:"PidsLimit,omitempty"`
	CapDrop        []string                 `json:"CapDrop,omitempty"`
	SecurityOpt    []string                 `json:"SecurityOpt,omitempty"`
	ReadonlyRootfs bool                     `json:"ReadonlyRootfs,omitempty"`
	Tmpfs          map[string]string        `json:"Tmpfs,omitempty"`
	Mounts         []Mount                  `json:"Mounts,omitempty"`
	PortBindings   map[string][]PortBinding `json:"PortBindings,omitempty"`
	RestartPolicy  RestartPolicy            `json:"RestartPolicy"`
	LogConfig      LogConfig                `json:"LogConfig"`
	NetworkMode    string                   `json:"NetworkMode,omitempty"`
	Init           *bool                    `json:"Init,omitempty"`
}

HostConfig holds the isolation and resource settings of a container.

type Info

type Info struct {
	ServerVersion string `json:"ServerVersion"`
}

Info is the part of /info the Agent uses.

type LogConfig

type LogConfig struct {
	Type   string            `json:"Type"`
	Config map[string]string `json:"Config,omitempty"`
}

LogConfig selects the log driver.

type Mount

type Mount struct {
	Type   string `json:"Type"`
	Source string `json:"Source"`
	Target string `json:"Target"`
}

Mount attaches a volume.

type PortBinding

type PortBinding struct {
	HostIP   string `json:"HostIp,omitempty"`
	HostPort string `json:"HostPort"`
}

PortBinding publishes a container port on the host.

type RestartPolicy

type RestartPolicy struct {
	Name string `json:"Name"`
}

RestartPolicy is Docker's own restart policy; the Agent uses "no" and applies the server's policy itself.

type Stats added in v1.11.0

type Stats struct {
	CPUStats struct {
		CPUUsage struct {
			TotalUsage uint64 `json:"total_usage"`
		} `json:"cpu_usage"`
	} `json:"cpu_stats"`
	MemoryStats struct {
		Usage uint64            `json:"usage"`
		Stats map[string]uint64 `json:"stats"`
	} `json:"memory_stats"`
}

Stats is the part of a container stats sample the Agent reads.

func (Stats) MemoryBytes added in v1.11.0

func (s Stats) MemoryBytes() int64

MemoryBytes is the memory a container uses, without reclaimable page cache (as docker stats reports it).

Jump to

Keyboard shortcuts

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