liveBook · Manning

Go SDK

As explained in Build and Orchestrator in Go (From Scratch)

The Docker SDK is a client library that lets software interact programmatically with the Docker Engine API.

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

How Build and Orchestrator in Go (From Scratch) covers it

How deep it goes
Abstracts HTTP mechanics and identifies six lifecycle methods, but stops short of production-scale orchestration details.
Examples
Uses curl inspection and a Run method combining image pull, container creation, and container startup.
What it assumes
Assumes non-trivial programming experience while keeping the Go implementation runnable for newcomers.
What you can do after
Operate Docker containers programmatically through the Docker Go SDK instead of assembling raw HTTP requests.
How it connects
Connects Go, Docker Engine API, Docker containers, HTTP clients, and orchestration systems.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build and Orchestrator in Go (From Scratch). The section links point to the parts of the book it draws on.