Skip to main content

Docker

c.Docker calls the API's Docker group. The Docker containers of the host.

Methods​

MethodReturnsPermissionDescription
List(ctx, hostname, params)*Response[Collection[DockerEntry]]docker:readList the containers of the host.
Create(ctx, hostname, input)*Response[Collection[DockerResult]]docker:writeCreate a container.
Get(ctx, hostname, name)*Response[Collection[DockerEntry]]docker:readGet one container.
Delete(ctx, hostname, name)*Response[Collection[DockerResult]]docker:writeRemove a container.
Exec(ctx, hostname, input)*Response[Collection[DockerExecResult]]docker:execRun a command in a container.
Start(ctx, hostname, name)*Response[Collection[DockerResult]]docker:executeStart a container.
Stop(ctx, hostname, input)*Response[Collection[DockerResult]]docker:executeStop a container.

List​

func (s *DockerService) List(
ctx context.Context,
hostname string,
params DockerListParams,
) (*Response[Collection[DockerEntry]], error)

List the containers of the host.

Reads each container through gohai's docker collector, which runs docker ps and docker inspect. state limits the list to the running or the stopped ones. A host whose docker does not answer reports skipped. The environment of a container shows only the names of its variables: osapi never reads their values.

It sends GET /api/node/{hostname}/docker, and needs the permission docker:read.

The examples of the method's definition:

A host with a container, go run docker.go list:

resp, err := c.Docker.List(ctx, target, client.DockerListParams{})

Only the stopped ones, go run docker.go list-2:

resp, err := c.Docker.List(ctx, target, client.DockerListParams{State: types.ContainerState("stopped")})

Create​

func (s *DockerService) Create(
ctx context.Context,
hostname string,
input DockerCreateInput,
) (*Response[Collection[DockerResult]], error)

Create a container.

Runs docker create, which pulls the image when the host does not have it. The environment goes on docker's standard input, not in its arguments, which each user of the host can read. auto_start starts the container once it is made. A container of the name that the host has already is not made again, and reports changed: false.

It sends POST /api/node/{hostname}/docker, and needs the permission docker:write.

The examples of the method's definition:

Create a container, go run docker.go create:

resp, err := c.Docker.Create(ctx, target, client.DockerCreateInput{
Command: []string{"sleep", "300"},
Image: "alpine:3.20",
Name: "osapitest",
})

Get​

func (s *DockerService) Get(
ctx context.Context,
hostname string,
name string,
) (*Response[Collection[DockerEntry]], error)

Get one container.

Reads one container by its name, its ID or the start of its ID. A container that the host does not have is not found.

It sends GET /api/node/{hostname}/docker/{name}, and needs the permission docker:read.

The examples of the method's definition:

A container, go run docker.go get:

resp, err := c.Docker.Get(ctx, target, "osapitest")

Delete​

func (s *DockerService) Delete(
ctx context.Context,
hostname string,
name string,
) (*Response[Collection[DockerResult]], error)

Remove a container.

Runs docker rm -f, which also removes a container that runs: a delete leaves no container of the name. A container that the host does not have reports changed: false.

It sends DELETE /api/node/{hostname}/docker/{name}, and needs the permission docker:write.

The examples of the method's definition:

Remove a container, go run docker.go delete:

resp, err := c.Docker.Delete(ctx, target, "osapitest")

Exec​

func (s *DockerService) Exec(
ctx context.Context,
hostname string,
input DockerExecInput,
) (*Response[Collection[DockerExecResult]], error)

Run a command in a container.

Runs docker exec, and answers what the command wrote to standard output and to standard error, and its exit code. A command that exits with a code other than 0 is an answer, not a failure. It changes what the command changes, which osapi cannot tell, so it reports changed: true. Its permission is docker:exec, which only the admin role gets, apart from the docker:execute of start and stop. An agent with agent.command.enabled false refuses it, as it refuses a command.

It sends POST /api/node/{hostname}/docker/{name}/exec, and needs the permission docker:exec.

The examples of the method's definition:

Run a command, go run docker.go exec:

resp, err := c.Docker.Exec(ctx, target, client.DockerExecInput{
Command: []string{"echo", "hello"},
Name: "osapitest",
})

Start​

func (s *DockerService) Start(
ctx context.Context,
hostname string,
name string,
) (*Response[Collection[DockerResult]], error)

Start a container.

Runs docker start. A container that runs already reports changed: false, and one that the host does not have is not found.

It sends POST /api/node/{hostname}/docker/{name}/start, and needs the permission docker:execute.

The examples of the method's definition:

Start a container, go run docker.go start:

resp, err := c.Docker.Start(ctx, target, "osapitest")

Stop​

func (s *DockerService) Stop(
ctx context.Context,
hostname string,
input DockerStopInput,
) (*Response[Collection[DockerResult]], error)

Stop a container.

Runs docker stop, which waits timeout seconds before docker kills the container, or docker's default of 10 when it is 0. A container that does not run reports changed: false.

It sends POST /api/node/{hostname}/docker/{name}/stop, and needs the permission docker:execute.

The examples of the method's definition:

Stop a container, go run docker.go stop:

resp, err := c.Docker.Stop(ctx, target, client.DockerStopInput{
Name: "osapitest",
Timeout: 1,
})

Types​

The fields of each type are the properties of its schema in the API's spec.

ContainerState​

ContainerState is the containers that a list shows, by their state.

A string, one of running, stopped, all.

DockerCreateInput​

FieldTypeDescription
AutoStartboolStart the container once it is created.
Command[]stringThe command, in place of the image's own.
ContainerHostnamestringThe hostname inside the container.
DNS[]stringThe DNS servers of the container.
Envmap[string]stringThe environment variables of the container. A secret: a reference, secret:///, or the value.
ImagestringThe image to run, such as alpine:3.20; docker pulls it when the host does not have it.
NamestringThe container's name.
Ports[]stringThe published ports, each host:container, such as 8080:80.
Volumes[]stringThe bind mounts, each host:container, such as /srv/data:/data.

DockerEntry​

FieldTypeDescription
CreatedstringWhen it was created, RFC 3339.
EnvNames[]stringThe names of its environment variables; osapi never reads their values.
ErrorstringError message if the agent failed.
HealthstringThe state of its health check: healthy, unhealthy or starting; empty without one.
HostnamestringHostname of the agent that produced this result.
IDstringThe container's full ID.
ImagestringThe image it runs.
Mounts[]stringThe mounts, source:destination, with :ro when read only.
NamestringThe container's name.
Networks[]stringThe networks, with the container's address and gateway on each.
Ports[]stringThe published ports, host:port->container port.
Statestringrunning, exited, created, paused or restarting.
StatusRowStatusWhat happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran.
SummarystringWhat docker says of its state, such as Up 2 minutes.

DockerExecInput​

FieldTypeDescription
NamestringThe name of the path. The container's name or ID.
Command[]stringThe command and its arguments.
Envmap[string]stringEnvironment variables for the command. A secret: a reference, secret:///, or the value.
WorkingDirstringThe directory the command runs in.

DockerExecResult​

FieldTypeDescription
ChangedboolAlways true: osapi cannot tell what the command changed.
ErrorstringError message if the agent failed.
ExitCodeintThe command's exit code.
HostnamestringHostname of the agent that produced this result.
StatusRowStatusWhat happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran.
StderrstringWhat the command wrote to standard error.
StdoutstringWhat the command wrote to standard output.

DockerResult​

FieldTypeDescription
ChangedboolWhether the host changed.
ErrorstringError message if the agent failed.
HostnamestringHostname of the agent that produced this result.
IDstringThe container's full ID, for create.
NamestringThe container.
StatusRowStatusWhat happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran.

DockerStopInput​

FieldTypeDescription
NamestringThe name of the path. The container's name or ID.
TimeoutintSeconds to wait before docker kills it; docker's default when 0.

RowStatus​

What happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran.

A string, one of ok, failed, skipped, timeout.

Example​

examples/sdk/client/docker.go runs each example of this page. With no argument, it lists them.