SoFunction
Updated on 2025-03-10

Docker inspect command usage tips

Description and introduction

docker inspect is a native command of the docker client, used to view the underlying basic information of the docker object. Including the container's id, creation time, running status, startup parameters, directory mount, network configuration, etc. In addition, this command can also be used to view the information of the docker image.

The official description is as follows:

Return low-level information on Docker objects

grammar

The syntax is as follows:

docker inspect [OPTIONS] NAME|ID [NAME|ID...]

OPTIONS Options

The following table is excerpted from the official website

Name, shorthand Default Description
--format , -f Format the output using the given Go template
--size , -s Display total file sizes if the type is container
--type Return JSON for specified type

As shown in the above table, --type is used to specify the docker object type, such as: container, image. It can be used when the container has the same name as the mirror, and it is used less frequently. For example, when a container on your machine is called redis and a mirror is redis:latest, you can use the following command to view the mirror information. If the type parameter is not used, the container information will be returned:

# View redis:latest image informationdocker inspect --type=image redis

# View redis container informationdocker inspect redis

--size is used to view the file size of the container. Add this parameter to the output result. SizeRootFs and SizeRw will be included in the output (at this time I am not sure about the meaning of these two values, please let the insider know).

The above two parameters are used relatively little, and --format is most practical and has a relatively high frequency of use. From the table description, we can see that the passed parameter value should be a template for Go language. It is very powerful and can do a lot of go function operations. Since my go language has not yet been started, I won’t talk about too many acrobatics here to avoid overturning. Let’s talk about the commonly used ones below.

practice

In practice, we often only need to view some of the information, such as directory mount information and network information. When directly inputting docker inspect container, all the information in the container will be output, which will appear bloated, and it is not convenient for us to turn pages on the command line. At this time, the practicality of --format is reflected. Common operations in practice are as follows

View directory mount information

Enter the following command and the Mounts information of the container will be output, and you can see the specific mount location of each directory in the container in the host.

docker inspect --format="{{json .Mounts}}" container

The json in the parameter is the method name of the Go language, followed by taking the Mounts value for jsonization. It is also OK to remove json.
If you think this input is not very good, you can further process json, such as using python's json module or jq to beautify the output. The command is as follows:

#Beautify using python json module
docker inspect --format="{{json .Mounts}}" container | python -m 

#Beautify using jq
docker inspect --format="{{json .Mounts}}" container | jq

View container network information

To view network information, you can use the following command:

#View full network information
docker inspect --format="{{json .NetworkSettings}}" container | jq

#View network port mapping
docker inspect --format="{{json .}}" container | jq

# Check the container's network ip, gateway and other information
docker inspect --format="{{json .}}" container | jq

Extended learning

If you are interested, you can also make full use of this --format parameter, because it is the template syntax of go, which is almost the code that can be written to go. For example, in the above command, json is the method name of go

Therefore, you can combine other Go methods (such as range, split) to play acrobatics, and this article will not show off.

References
Docker official documentation

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.