The Docker Go SDK is a client library for controlling Docker containers from Go. It hides HTTP methods, status codes, request serialization, and response deserialization so orchestration code can focus on container operations. section 1.7 section 3.2
A small mental model is enough: create a client, pull an image, create a configured container, start it, stop it, and remove it. These methods form the basic control path for a Docker-focused orchestrator. section 3.2
The SDK methods used for the core container lifecycle are:
- NewClientWithOpts instantiates a Docker client. section 3.2
- ImagePull downloads an image to the local machine. section 3.2
- ContainerCreate creates a container from configuration. section 3.2
- ContainerStart starts the newly created container through Docker Engine. section 3.2
- ContainerStop stops a running container. section 3.2
- ContainerRemove removes the container from its host. section 3.2
A container run path
A Run method can combine ImagePull, ContainerCreate, and ContainerStart to turn a container request into a running workload. Later lifecycle operations can use ContainerStop and ContainerRemove. section 3.2
Listing 3.5 Querying the Docker API with the curl HTTP client
curl --unix-socket \
/var/run/docker.sock http://docker/containers/6970e8469684/json| jq .
{
"Id": "6970e8469684d439c73577c4caee7261bf887a67433420e7dcd637cc53b8ffa7",
"Created": "2021-05-15T20:58:36.283909602Z",
"Path": "docker-entrypoint.sh",
"Args": [
"postgres"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 270523,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-05-15T20:58:36.541148947Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
....
}
Compare SDK calls with the lower-level curl request used to query Docker’s API over a Unix socket. section 3.2
