Docker
c.Docker calls the API's Docker group. The Docker containers of the host.
Methods
| Method | Returns | Permission | Description |
|---|---|---|---|
List(ctx, hostname, params) | *Response[Collection[DockerEntry]] | docker:read | List the containers of the host. |
Create(ctx, hostname, input) | *Response[Collection[DockerResult]] | docker:write | Create a container. |
Get(ctx, hostname, name) | *Response[Collection[DockerEntry]] | docker:read | Get one container. |
Delete(ctx, hostname, name) | *Response[Collection[DockerResult]] | docker:write | Remove a container. |
Exec(ctx, hostname, input) | *Response[Collection[DockerExecResult]] | docker:exec | Run a command in a container. |
Start(ctx, hostname, name) | *Response[Collection[DockerResult]] | docker:execute | Start a container. |
Stop(ctx, hostname, input) | *Response[Collection[DockerResult]] | docker:execute | Stop 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
| Field | Type | Description |
|---|---|---|
AutoStart | bool | Start the container once it is created. |
Command | []string | The command, in place of the image's own. |
ContainerHostname | string | The hostname inside the container. |
DNS | []string | The DNS servers of the container. |
Env | map[string]string | The environment variables of the container. A secret: a reference, secret:// |
Image | string | The image to run, such as alpine:3.20; docker pulls it when the host does not have it. |
Name | string | The container's name. |
Ports | []string | The published ports, each host:container, such as 8080:80. |
Volumes | []string | The bind mounts, each host:container, such as /srv/data:/data. |
DockerEntry
| Field | Type | Description |
|---|---|---|
Created | string | When it was created, RFC 3339. |
EnvNames | []string | The names of its environment variables; osapi never reads their values. |
Error | string | Error message if the agent failed. |
Health | string | The state of its health check: healthy, unhealthy or starting; empty without one. |
Hostname | string | Hostname of the agent that produced this result. |
ID | string | The container's full ID. |
Image | string | The image it runs. |
Mounts | []string | The mounts, source:destination, with :ro when read only. |
Name | string | The container's name. |
Networks | []string | The networks, with the container's address and gateway on each. |
Ports | []string | The published ports, host:port->container port. |
State | string | running, exited, created, paused or restarting. |
Status | 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. |
Summary | string | What docker says of its state, such as Up 2 minutes. |
DockerExecInput
| Field | Type | Description |
|---|---|---|
Name | string | The name of the path. The container's name or ID. |
Command | []string | The command and its arguments. |
Env | map[string]string | Environment variables for the command. A secret: a reference, secret:// |
WorkingDir | string | The directory the command runs in. |
DockerExecResult
| Field | Type | Description |
|---|---|---|
Changed | bool | Always true: osapi cannot tell what the command changed. |
Error | string | Error message if the agent failed. |
ExitCode | int | The command's exit code. |
Hostname | string | Hostname of the agent that produced this result. |
Status | 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. |
Stderr | string | What the command wrote to standard error. |
Stdout | string | What the command wrote to standard output. |
DockerResult
| Field | Type | Description |
|---|---|---|
Changed | bool | Whether the host changed. |
Error | string | Error message if the agent failed. |
Hostname | string | Hostname of the agent that produced this result. |
ID | string | The container's full ID, for create. |
Name | string | The container. |
Status | 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. |
DockerStopInput
| Field | Type | Description |
|---|---|---|
Name | string | The name of the path. The container's name or ID. |
Timeout | int | Seconds 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.