Putting a Magnifying Glass to Docker with Docker Inspect

Published:14 September 2021 - 5 min. read

If you are looking for a detailed view of all the major components of Docker from Docker images, containers, and so on, the docker inspect command is for you.

In this tutorial, you’ll learn how to find become a sleuth to discover detailed information about many Docker components for debugging, troubleshooting, or just your mere curiosity.

Let’s begin!

Prerequisites

If you’d like to follow along step-by-step, ensure you have the following:

  • At least one container running on the Docker host.

Inspecting Docker Images with Docker Inspect

The Docker Inspect can provide information on many different Docker components but let’s first start with one of the most popular components; images. If you’ve downloaded an image, how do you then check for information about that image? Let’s find out.

Assuming you have at least one image on your Docker host, use the inspect command to query that image. The command below assumes that you have an image called nginx on your host.

docker image inspect nginx

Once executed, the command above returns a slew of information via JSON about that particular image. Some notable attributes are:

  1. The image tag.
  2. The time the image was created.
  3. The hostname of the associated container with Docker image nginx.
  4. The network ports the container makes available to the outside network.
  5. You will also notice the volume(s) attached to the container that belongs to the Docker image nginx.
Getting a detailed view of Docker image using Docker inspect command.
Getting a detailed view of Docker image using Docker inspect command.

Getting Detailed Information about Docker Networks

The docker inspect command isn’t just relegated to grabbing information about images and containers; it can also query networks. Docker networks allow containers to communicate in different ways, such as through a bridge network (a private network), a host network (a public network), or an overlay network.

Knowing Docker networks is important as it allows you to switch to another Docker network anytime as per the need of the application by disconnecting and then connecting back.

Assuming you are still on the terminal:

1. Run the command below. The docker network ls command lists all the networks present in the Docker. You’ll need the output of this command to find the network’s ID.

docker network ls
Listing the Docker network
Listing the Docker network

Next, pass a network ID to the docker inspect network command. This command returns various information about the particular network, as shown below.

docker inspect network 4748f122c987

1. The type of network.

2. The subnet address the network uses.

3 and 4. Summary information about every container that’s using this particular network.

Inspecting the Docker network

Understanding Docker’s Go Templates and the Format Parameter

Up to now, you have learned how to use Docker inspect command without using any additional options parameters. But if you need to manipulate the output format, consider using the --format flag. The --format parameter uses Go templates to filter and display the information returned from docker inspect.

Various functions are used along with the format flag, such as join, println, table, etc. Let’s discuss a few of them!

For example, maybe you want to join some values together from the output from a particular container. If so, the join command works well. Below, you’ll see that the join command concatenates strings together (nginx, -g, daemon off) with each element separated by a separator (,).

docker inspect --format '{{join .Args " , "}}' container_id
Demonstrating the join format function for Docker Inspect.

Maybe you’d like to capitalize a value? Use the upper command.

docker inspect --format "{{upper .Name}}" container_id
Demonstrating the upper command format function for Docker Inspect.

You can find many different examples of using the format parameter.

Finding a Docker Container’s Base Image Name

In the previous section, you saw how to get a detailed view of all your Docker images, including the containers using those images. But what if you already know a container ID and need to find the image that it was created from quickly? You can provide the container ID to docker inspect.

To find the name of the image that a particular container is built from, run the below docker inspect command. The below command passes the container ID (8f34d039fa3a in this case) to docker inspect and uses the format parameter to only return the image value from the Config node in the JSON output.

# 8f34d039fa3a is the container represented with ContainerID.
docker inspect --format='{{.Config.Image}}' 8f34d039fa3a
Docker inspect command that shows nginx image
Docker inspect command that shows nginx image

You can see what the entire Config node looks like below and where the Image name comes from.

Finding the name of the Image used by Contai
Finding the name of the Image used by Container

Finding a Container’s IP Address, Mac Address, and Port Bindings

Although you now know how to find the network a container uses, how do you know what IP address or Mac Address, and Port Bindings on that network a container uses? You guessed it! docker inspect.

To narrow down the IP address that a container uses, pass the container ID to the docker inspect command. Again, use the format parameter to filter out the unwanted JSON to get down to exactly what you’re looking for.

You’ll see the format parameter argument below tells docker inspect only to return the value embedded inside the NetworkSettingsNetworks→ IPAddress JSON attribute using the range attribute. Range attribute allows you to iterate over an array, setting the. value to each array element.

docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my_container
Finding the IP address using Docker inspect command.
Finding the IP address using Docker inspect command.

You’ll see the main NetworkSettings node below and where the IPAddress value is nested.

Inspecting the NetworkSettings values from Docker Inspect.

Similarly, to find the MAC address of the container, use docker inspect container command as shown below.

Again, You’ll see that the format parameter argument below uses the range attribute but this time, find the container’s Mac address by checking in NetworkSettingsNetworks→ Mac address.

docker inspect --format='{{range .NetworkSettings.Networks}}{{.MacAddress}}{{end}}' 3dd206041249
Retrieving the Mac address of the Docker Container.
Retrieving the Mac address of the Docker Container.

Finally, To check all port bindings of a container, run the below docker inspect command. You’ll see the format parameter argument below uses the range attribute to find the container’s port bindings by checking in NetworkSettingsHostPort.

One should know about the port bindings because if any port is already used, it shouldn’t be used again; otherwise, it will throw an error message stating the port is already allocated.

docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' 8f34d039fa3a
Checking the Port Binding of the container.
Checking the Port Binding of the container.

Getting a detailed view of Docker Volume Using Docker Inspect

Up to now, you have learned how to find detailed information about the images, containers, and networking, but you haven’t learned about storage. Let’s change that.

To ensure the tutorial has at least one mounted volume attached to a container, let’s first create a container and mount a storage volume.

If you already have a storage volume mounted to a container, you can skip this part.

The command below creates a new Docker container called my_container, exposes port 80 to the host (-p 80:80), mounts a volume based on the host’s /var/container_dir directory called mounted_val (-v mounted_vol:/var/container_dir) using the nginx image.

docker run --name my_container -p 80:80 -v mounted_vol:/var/container_dir nginx

Now that you have a volume mounted to a container provide the volume’s name to docker volume inspect. Notice below that the command returns JSON output, only providing information about the mounted storage volume.

docker volume inspect mounted_vol
Inspecting the docker volume
Inspecting the docker volume

Conclusion

You should now be able to use the docker inspect command in many different situations, from querying information about images, containers, networking, and storage.

How do you plan to use docker inspect to query information about your own Docker environment?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!