>_ IzzyLab

Hands-on task

This task is step 5 of the course “Docker: everyday work”

A container's ID card: docker inspect

medium~8 min

An old container legacy runs on the server, and nobody remembers anything about it. Find out:

  • its IP address on Docker's internal network — write it to /root/ip.txt;
  • which server directory is attached to the container as /data — write the full path to /root/data-path.txt.

Each file holds only the value.

What you'll practise:
  • docker inspect
  • --format
  • Go templates

Topic breakdown

docker inspect is a container's (or image's, volume's, network's) full passport in JSON. The main sections of a container:

  • Config — image, command, variables (Env);
  • State — whether it's running, the exit code, when it was started;
  • HostConfig — run options: port publishing, restart policy, limits;
  • NetworkSettings — networks and addresses;
  • Mounts — attached directories and volumes.

Every container gets its own IP on Docker's internal network — by default on the bridge network, with addresses like 172.17.0.x. From the server you can reach a container at this address directly, from outside you can't.

To pull out a single value, use -f with a Go template: the dot is the whole object, then the path through dots.

docker inspect -f '{{.State.Status}}' api        # running
docker inspect -f '{{.Config.Image}}' api
docker inspect -f '{{range .Config.Env}}{{println .}}{{end}}' api

range walks over a list or a map, and inside it the dot is the current element. You can also do this: docker inspect api | jq ….

What's checked

  1. /root/ip.txt has legacy's IP address
  2. /root/data-path.txt has the server directory attached as /data

What it looks like