<p>Here's another 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>Today, we are going to learn how to list all images which are downloaded on our system.</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-images</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;strings&quot;</span> <span class="s">&quot;time&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-formatter&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">images</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">ImageList</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">ImageListOptions</span><span class="p">{</span> <span class="nx">All</span><span class="p">:</span> <span class="kc">false</span><span class="p">,</span> <span class="p">})</span> <span class="k">for</span> <span class="nx">_</span><span class="p">,</span> <span class="nx">image</span> <span class="o">:=</span> <span class="k">range</span> <span class="nx">images</span> <span class="p">{</span> <span class="k">for</span> <span class="nx">_</span><span class="p">,</span> <span class="nx">tag</span> <span class="o">:=</span> <span class="k">range</span> <span class="nx">image</span><span class="p">.</span><span class="nx">RepoTags</span> <span class="p">{</span> <span class="nx">tagParts</span> <span class="o">:=</span> <span class="nx">strings</span><span class="p">.</span><span class="nx">SplitN</span><span class="p">(</span><span class="nx">tag</span><span class="p">,</span> <span class="s">&quot;:&quot;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="nx">log</span><span class="p">.</span><span class="nx">Infof</span><span class="p">(</span> <span class="s">&quot;%-25s | %-15s | %s | %-30s | %10s&quot;</span><span class="p">,</span> <span class="nx">tagParts</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nx">tagParts</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="nx">image</span><span class="p">.</span><span class="nx">ID</span><span class="p">[</span><span class="mi">7</span><span class="p">:</span><span class="mi">19</span><span class="p">],</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Unix</span><span class="p">(</span><span class="nx">image</span><span class="p">.</span><span class="nx">Created</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="nx">formatter</span><span class="p">.</span><span class="nx">FileSize</span><span class="p">(</span><span class="nx">image</span><span class="p">.</span><span class="nx">Size</span><span class="p">),</span> <span class="p">)</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>ImageList</code> to get the full list of running containers. Then we loop through the images and all their tags and output a nicely formatted table.</p> <p>Then run our program from within the project directory and you should get a list of the images on the system in a nicely formatted table:</p> <pre><code>$ go run . temporalio/admin-tools | 1.5.0 | c55157e36ac3 | 2020-12-23 00:03:15 +0100 CET | 244.29 MB temporalio/auto-setup | 1.5.0 | 3548ab044e07 | 2020-12-23 00:02:35 +0100 CET | 333.88 MB temporalio/web | 1.4.1 | d53c4f30b7b3 | 2020-12-18 18:56:48 +0100 CET | 369.83 MB cassandra | 3.11 | 8baadf8d390f | 2020-11-26 04:13:40 +0100 CET | 405.43 MB mysql | 8 | dd7265748b5d | 2020-11-21 02:22:38 +0100 CET | 545.31 MB golang | 1.15.5-alpine | 1de1afaeaa9a | 2020-11-12 22:22:23 +0100 CET | 298.83 MB redis | alpine | c1949ec48c51 | 2020-10-27 19:34:35 +0100 CET | 31.15 MB example-environ-server | latest | 4f136e1edaa3 | 2020-09-18 17:01:37 +0200 CEST | 23.21 MB alpine | 3.12 | a24bb4013296 | 2020-05-29 23:19:46 +0200 CEST | 5.57 MB</code></pre>

Related Posts

  • Using the Docker client from Go part 1
  • 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