Putting a Magnifying Glass to Docker with Docker Inspect


VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

Docker has been a revolution in the world of containerization, and is only continuing to grow. Docker’s flexibility brought about recent success with building production environments for companies such as IBM and Google, who use it for their high-demand cloud services.

docker inspect container” is a command that allows you to put a magnifying glass on the Docker runtime. It will allow you to find all of the information about your containers and images, as well as their current state.

Putting a Magnifying Glass to Docker with Docker Inspect

The docker inspect command will provide you a complete picture of all the primary components of Docker, including Docker images, containers, and so on.

In this article, you’ll learn how to become a detective and get extensive information on a variety of Docker components for debugging, troubleshooting, or just plain curiosity.

Let’s get started!


If you want to follow along, make sure you have the following items:

Installation and Use of Docker on Ubuntu (In the Real World)

  • On the Docker host, at least one container is running.

Docker for Windows: How to Deploy Your First Container

Docker Inspect for inspecting Docker Images

The Docker Inspect tool may offer information on a variety of Docker components, but let’s start with one of the most common: images. How do you look for information about a picture after you’ve downloaded it? Let’s have a look.

Use the inspect command to query an image on your Docker host, assuming you have at least one. The commands following presume you have a nginx image on your server.

nginx docker image inspection

The program above, when run, delivers a deluge of information about that specific picture in JSON format. The following are some significant characteristics:

  1. The tag for images.
  2. The date when the photograph was made.
  3. The hostname of the related Docker image nginx container.
  4. The external network ports that the container makes accessible.
  5. The volume(s) associated to the container that belongs to the Docker image nginx will also be visible.

Using the Docker inspect command to get a thorough look of a Docker image. Using the Docker inspect command to get a thorough look of a Docker image.

Obtaining Comprehensive Information about Docker Networks

The docker inspect tool may query networks in addition to gathering information about images and containers. Containers may interact over a bridge network (a private network), a host network (a public network), or an overlay network using Docker networks.

Knowing Docker networks is vital because it enables you to switch to a different Docker network whenever the application requires it simply disconnecting and reconnecting.

If you’re still in the terminal, follow these steps:

1. Execute the command shown below. The docker network ls tool displays a list of all the Docker networks. To discover the network’s ID, you’ll need the result of this command.

The Docker network is listed below.The Docker network is listed below.

Next, use the docker inspect network command to provide a network ID. As seen below, this command delivers a variety of network-related information.

4748f122c987 docker examine network

1. The network type.

The network’s subnet address is number two.

3 and 4: A summary of every container connected to this network.

Untitled Examination of the Docker network

Understanding the Format Parameter in Docker’s Go Templates

You’ve learnt how to use Docker inspect without any extra settings arguments up to this point. Consider using the —format parameter if you need to change the output format. The —format flag filters and displays the data produced by docker inspect using Go templates.

Join, println, table, and other operations are used in conjunction with the format option. Let’s take a look at a couple of them.

For example, suppose you wish to combine certain data from the output of a specific container. If this is the case, the join command is effective. The join command concatenates strings (nginx, -g, daemon off) with each element separated by a separator, as seen below (,).

container id docker inspect —format ‘join.Args “, “‘

image-81-1024x44Docker Inspect’s join format method is shown.

Perhaps you want to capitalize a value? Use the command above.

—format “upper.Name” docker inspect container id

image-82-1024x44 For Docker Inspect, I’m demonstrating the upper command format function.

There are several examples of how to use the format parameter.

Identifying the Base Image Name of a Docker Container

You learned how to receive a full view of all your Docker images, including the containers that use them, in the previous section. But what if you already have a container ID and need to quickly locate the picture from which it was created? Docker inspect may be given the container ID.

Run the docker inspect command to determine the name of the image that a given container is constructed from. The code below utilizes the format option to just return the image value from the Config node in the JSON output and provides the container ID (8f34d039fa3a in this example) to docker inspect.

ContainerID # 8f34d039fa3a represents the container. 8f34d039fa3a docker inspect —format=’.Config.Image’

The inspect command in Docker displays the nginx image. The inspect command in Docker displays the nginx image.

Below you can see how the complete Config node appears and where the Image name originates.

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

Finding the IP Address, Mac Address, and Port Bindings of a Container

Although you now know how to locate a container’s network, how can you determine the IP address or Mac address, as well as the network’s Port Bindings? You guessed correctly! examine docker

Pass the container ID to the docker inspect command to limit down the IP address that a container uses. To get down to precisely what you’re searching for, utilize the format argument to filter out the unnecessary JSON.

The format parameter argument informs Docker inspect to only return the value included inside the NetworkSettingsNetworks IPAddress JSON element using the range attribute, as seen below. You may iterate over an array and set the value for each array member using the Range property.

—format=’range.NetworkSettings.Networks.IPAddressend’ docker inspect my container

Using the Docker inspect command, you may get the IP address. Using the Docker inspect command, you may get the IP address.

The primary NetworkSettings node is shown below, as well as the IPAddress value.

image-86Using Docker Inspect to inspect the NetworkSettings values.

Similarly, use the docker inspect container command to get the container’s MAC address, as illustrated below.

The format parameter argument below utilizes the range attribute once again, but this time look for the container’s Mac address in NetworkSettingsNetworks Mac address.

inspect —format=’range.NetworkSettings.Networks.MacAddressend’ in docker 3dd206041249

Obtaining the Docker Container's Mac address. Obtaining the Docker Container’s Mac address.

Finally, use the docker inspect command to examine all of a container’s port bindings. By checking in NetworkSettingsHostPort, the format parameter argument below utilizes the range attribute to discover the container’s port bindings.

It’s important to understand port bindings because if a port has previously been used, it shouldn’t be used again; otherwise, an error notice will appear saying that the port has already been assigned.

docker inspect –format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}’ 8f34d039fa3a

Checking the container's port binding. Checking the container’s port binding.

Using Docker Inspect to get a thorough look of a Docker Volume

You’ve learned how to get precise information about pictures, containers, and networking up to this point, but not about storage. Let’s make a difference.

Let’s start by creating a container and mounting a storage volume to guarantee that the tutorial has at least one mounted volume.

You may skip this step if you already have a storage volume mounted to a container.

The command below creates a new Docker container named my container, exposes port 80 to the host (-p 80:80), and mounts a volume called mounted val based on the host’s /var/container dir directory (-v mounted vol:/var/container dir).

nginx mounted vol:/var/container dir docker run —name my container -p 80:80

Now that you’ve mounted a volume to a container, use docker volume inspect to get the volume’s name. The program below produces JSON output, which solely contains information on the mounted storage volume.

mounted vol docker volume inspect

Docker volume inspection Docker volume inspection


You should now be able to use the docker inspect command to query information about images, containers, networking, and storage in a variety of contexts.

How will you use docker inspect to get information about your Docker environment?

Docker inspect is a command-line tool that allows users to put a magnifying glass on the Docker environment. The tool enables users to view and modify the values of certain environment variables. Reference: docker inspect environment variables.

Related Tags

  • docker inspect image filesystem
  • docker inspect sample output
  • docker inspect example
  • docker inspect image id
  • docker hub