Docker API from CLI

Tags:

Docker website

Docker has command line programs, but they are not well suited to automation. Return values and pars-able output are both lacking, but there is an API that you can use instead. The Dockerd api can listen on a local socket or even a remote accessible port, but in these examples I'll show local socket only.

Raw

Use curl to connect to the docker socket, and give a URL related to Docker's API. In this example you'll get compressed and unreadable JSON about all installed Docker images.

List all docker images

curl --unix-socket /run/docker.sock http://docker/images/json

Pretty and practical

Pretty the input using jq.

curl --unix-socket /run/docker.sock http://docker/images/json| jq .

Here's an excerpt, one image:

{
    "Id": "sha256:25e41e468c9cb4351dbbeedf2a9d266bcdf2257d3b1188a73b421b2035513f01",
    "ParentId": "",
    "RepoTags": [
          "perl:5.24"
    ],
    "RepoDigests": [
      "perl@sha256:6c589d851fed973cf7e1285abc13f4f92d31665f6acf4e094819d5292160e5c7"
    ],
    "Created": 1477122199,
    "Size": 655927650,
    "VirtualSize": 655927650,
    "Labels": {}
},

We can further use jq to search and return selected information.

Return just the image ids

curl -s --unix-socket /run/docker.sock http://docker/images/json \
    | jq '.[]|.Id'

Return image ids and creation epoch time

curl -s --unix-socket /run/docker.sock http://docker/images/json \
    | jq '.[]|.Id,.Created'

Search for a given image and return created epoch time

curl -s --unix-socket /run/docker.sock http://docker/images/json \
    | jq '.[]| select( .Id \
    == \
    "sha256:52c080433dada462d6c7fbaa7f84f96a20448a23cdcec115cf17deaee8d3c4a8" )\
    |.Created'

Search for images created earlier than 10 minutes ago

curl -s --unix-socket /run/docker.sock http://docker/images/json \
    | jq '.[]| select( .Created < ( now - 600 ))|.Id'

Search for images that contain given RepoTags and return the created epoch time

curl -s --unix-socket /run/docker.sock http://docker/images/json \
    | jq '.[] | select( .RepoTags[] == "perl:5.24" )|.Created'

References:

  1. Bash that JSON (with jq)
  2. Docker API
submit to reddit