<p>Here's a small sample program which shows how you can access the <a href="https://www.docker.com">Docker CLI</a> from within a Go program using the <a href="https://github.com/moby/moby/tree/master/client">Docker API</a>.</p> <p>We'll start simple with showing how to list the running containers</p> <p>We first start with creating an empty project:</p> <pre><code>$ mkdir list-docker-containers $ cd list-docker-containers $ go mod init github.com/pieterclaerhout/go-example/go-list-docker-containers</code></pre> <p>In there, we create a <code>main.go</code> file containing:</p> <div class="highlight"><pre><span></span><span class="kn">package</span> <span class="nx">main</span> <span class="kn">import</span> <span class="p">(</span> <span class="s">&quot;context&quot;</span> <span class="s">&quot;github.com/docker/docker/api/types&quot;</span> <span class="s">&quot;github.com/docker/docker/client&quot;</span> <span class="s">&quot;github.com/pieterclaerhout/go-log&quot;</span> <span class="p">)</span> <span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span> <span class="nx">log</span><span class="p">.</span><span class="nx">PrintColors</span> <span class="p">=</span> <span class="kc">true</span> <span class="nx">log</span><span class="p">.</span><span class="nx">PrintTimestamp</span> <span class="p">=</span> <span class="kc">false</span> <span class="nx">cli</span><span class="p">,</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">client</span><span class="p">.</span><span class="nx">NewEnvClient</span><span class="p">()</span> <span class="nx">log</span><span class="p">.</span><span class="nx">CheckError</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="nx">containers</span><span class="p">,</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">cli</span><span class="p">.</span><span class="nx">ContainerList</span><span class="p">(</span><span class="nx">context</span><span class="p">.</span><span class="nx">Background</span><span class="p">(),</span> <span class="nx">types</span><span class="p">.</span><span class="nx">ContainerListOptions</span><span class="p">{</span> <span class="nx">All</span><span class="p">:</span> <span class="kc">true</span><span class="p">,</span> <span class="p">})</span> <span class="nx">log</span><span class="p">.</span><span class="nx">CheckError</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="k">for</span> <span class="nx">_</span><span class="p">,</span> <span class="nx">container</span> <span class="o">:=</span> <span class="k">range</span> <span class="nx">containers</span> <span class="p">{</span> <span class="nx">log</span><span class="p">.</span><span class="nx">InfoDump</span><span class="p">(</span><span class="nx">container</span><span class="p">,</span> <span class="nx">container</span><span class="p">.</span><span class="nx">Image</span><span class="p">)</span> <span class="p">}</span> <span class="p">}</span> </pre></div> <p>The way it works is pretty simple. We first setup the logging (we are using <a href="https://github.com/pieterclaerhout/go-log">go-log</a> 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 <code>ContainerList</code> to get the full list of running containers.</p> <p>To test, make sure you have one or more Docker containers running. You can start one by running a <a href="http://redis.io">Redis server</a> for example:</p> <pre><code>$ docker run redis:alpine</code></pre> <p>Then run our program from within the project directory and you should get a list of the running containers with all their details:</p> <pre><code>$ 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: &amp;types.SummaryNetworkSettings{ Networks: map[string]*network.EndpointSettings{ "bridge": &amp;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: "", }, }, }</code></pre>

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