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

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

package main

import (


func main() {

    log.PrintColors = true
    log.PrintTimestamp = false

    cli, err := client.NewEnvClient()

    containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{
        All: true,

    for _, container := range containers {
        log.InfoDump(container, container.Image)


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{
  Image: "redis:alpine",
  ImageID: "sha256:c1949ec48c51d73e24be652eb3bcba0477121d7970f05abc234c2d5aef97d1a7",
  Command: " redis-server",
  Created: 1610642670,
  Ports: []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: "",
        IPAddress: "",
        IPPrefixLen: 16,
        IPv6Gateway: "",
        GlobalIPv6Address: "",
        GlobalIPv6PrefixLen: 0,
        MacAddress: "02:42:ac:11:00:02",
        DriverOpts: map[string]string{
  Mounts: []types.MountPoint{
      Type: "volume",
      Name: "0e689f87f96d0f4b7e84bf6063f884a0ceb4f7b8977cf6422db0b662b109673b",
      Source: "",
      Destination: "/data",
      Driver: "local",
      Mode: "",
      RW: true,
      Propagation: "",

Related Posts

  • Using the Docker client from Go part 2
  • Parsing a key pair from a PEM file in Go
  • Compile using a Docker container
  • Parsing App Store Connect API errors with Go
  • Connecting to the Apple App Store Connect API with Go