#development #docker #golang

Here's a small sample program which shows how you can access the Docker CLI from within a Go program using the Docker API.

We'll start simple with showing how to list the running containers

We first start with creating an empty project:

$ mkdir list-docker-containers
$ cd list-docker-containers
$ go mod init github.com/pieterclaerhout/go-example/go-list-docker-containers

In there, we create a main.go file containing:

 1package main
 2
 3import (
 4    "context"
 5
 6    "github.com/docker/docker/api/types"
 7    "github.com/docker/docker/client"
 8    "github.com/pieterclaerhout/go-log"
 9)
10
11func main() {
12
13    log.PrintColors = true
14    log.PrintTimestamp = false
15
16    cli, err := client.NewEnvClient()
17    log.CheckError(err)
18
19    containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{
20        All: true,
21    })
22    log.CheckError(err)
23
24    for _, container := range containers {
25        log.InfoDump(container, container.Image)
26    }
27
28}

The way it works is pretty simple. We first setup the logging (we are using go-log for this. We then create a client from the current environment (which should auto-detect your Docker installation). The, we can ask the API to use ContainerList to get the full list of running containers.

To test, make sure you have one or more Docker containers running. You can start one by running a Redis server for example:

$ docker run redis:alpine

Then run our program from within the project directory and you should get a list of the running containers with all their details:

$ go run .
redis:alpine types.Container{
  ID: "ef6a2229985cb6bd9ee1595c5b0c4e4a3e4d14282a863dd2f4d9a3d6c68f2b6a",
  Names: []string{
    "/brave_hoover",
  },
  Image: "redis:alpine",
  ImageID: "sha256:c1949ec48c51d73e24be652eb3bcba0477121d7970f05abc234c2d5aef97d1a7",
  Command: "docker-entrypoint.sh redis-server",
  Created: 1610642670,
  Ports: []types.Port{
    types.Port{
      IP: "",
      PrivatePort: 6379,
      PublicPort: 0,
      Type: "tcp",
    },
  },
  SizeRw: 0,
  SizeRootFs: 0,
  Labels: map[string]string{
  },
  State: "running",
  Status: "Up 3 seconds",
  HostConfig: struct { NetworkMode string "json:\",omitempty\"" }{
    NetworkMode: "default",
  },
  NetworkSettings: &types.SummaryNetworkSettings{
    Networks: map[string]*network.EndpointSettings{
      "bridge": &network.EndpointSettings{
        IPAMConfig: nil,
        Links: nil,
        Aliases: nil,
        NetworkID: "77b41b91ce98cf3a2ac8c7d4ce0984bc125eb6f8e656afb500d49ea195a1493d",
        EndpointID: "7c0cd8205d04df21d412b27e783422eb27bfa31ccf1b2bcf81e4252ce91f9600",
        Gateway: "172.17.0.1",
        IPAddress: "172.17.0.2",
        IPPrefixLen: 16,
        IPv6Gateway: "",
        GlobalIPv6Address: "",
        GlobalIPv6PrefixLen: 0,
        MacAddress: "02:42:ac:11:00:02",
        DriverOpts: map[string]string{
        },
      },
    },
  },
  Mounts: []types.MountPoint{
    types.MountPoint{
      Type: "volume",
      Name: "0e689f87f96d0f4b7e84bf6063f884a0ceb4f7b8977cf6422db0b662b109673b",
      Source: "",
      Destination: "/data",
      Driver: "local",
      Mode: "",
      RW: true,
      Propagation: "",
    },
  },
}