Skills of using docker inspect command

  • 2021-07-22 11:51:54
  • OfStack

Description and Introduction

docker inspect is a native command for the docker client to view the underlying basic information of the docker object. Include container id, creation time, running status, startup parameters, directory mount, network configuration, and so on. In addition, this command can also be used to view the information of 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 taken from 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 docker object types, 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, if you have one container named redis and one image named redis: latest on your machine, you can use the following command to view the image information. If the type parameter is not used, the container information is returned:


#  View redis:latest Mirror information 
docker inspect --type=image redis

#  View redis Container information 
docker inspect redis

--size is used to view the file size of the container. With this parameter, the output results will include SizeRootFs and SizeRw (at present, I am not sure what these two values mean, please let me know).

The above two parameters are used less,-format is the most practical and frequently used. From the table description, the parameter values passed in should be templates of go language. It is very powerful, can do a lot of go function operation, because my go language has not started, so here will not say too much acrobatics, so as not to overturn, the following 1 commonly used.

Practice

In practice, we often only need to look at some of the information, such as directory mount information and network information. When entering docker inspect container directly, all the information of the container will be output, which is bloated. It is inconvenient for us to turn pages on the command line. At this point, the practicality of--format is reflected. Common actions in practice are as follows

View directory mount information

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


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

json in the parameter is the method name of go language, followed by taking the value of Mounts for json processing. It is also possible to remove json.
If you think this input is still not very good-looking, you can take another step to json, such as using json module of python or jq to beautify the output. The order is as follows:


# Use python Adj. json Module beautification 

docker inspect --format="{{json .Mounts}}" container | python -m json.tool

# Use jq Beautify 

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

View container network information

To view network information, use the following command:


# View complete network information 

docker inspect --format="{{json .NetworkSettings}}" container | jq

# View network port mapping 

docker inspect --format="{{json .NetworkSettings.Ports}}" container | jq

#  View the container's network ip , gateway, and so on 

docker inspect --format="{{json .NetworkSettings.Networks}}" 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 write go. For example, the command above, json is the method name of go

Therefore, you can combine other go methods (such as range, split) to play acrobatics, so this article will not teach you how to do it.

References
docker Official Document


Related articles: