Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Skills in using docker inspect command

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Description and introduction

Docker inspect is the native command of the docker client to view the underlying basic information of the docker object. It includes 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 information about docker images.

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 option

The following table is extracted from the official website.

Name, shorthandDefaultDescription--format,-fFormat the output using the given Go template--size,-sDisplay total file sizes if the type is container--typeReturn 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 image, and it is used less frequently. For example, if you have a container named redis and an image redis:latest on your machine, you can use the following command to view the image information. If you do not use the type parameter, the container information is returned:

# View redis:latest image 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, SizeRootFs and SizeRw will be included in the output result (I am not sure about the meaning of these two values yet. Please let me know).

The above two parameters are less used,-- format is the most practical and frequently used. As you can see from the table description, the parameter values passed in should be a template for the go language. It is very powerful, can do a lot of go function operations, because my go language has not been introduced, so here do not say too much acrobatics, in order to avoid turning over, the following is commonly used.

Practice

In practice, we often only need to look at some of the information, such as directory mount information, network information. When you enter docker inspect container directly, it will output all the information of the container, which is relatively bloated, and it is not convenient for us to turn the page on the command line. At this point, the usefulness 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. You can see the specific mount location of each directory in the container on 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 value of Mounts for jsonization. It is also possible to remove json.

If you think this input is still not very good-looking, you can further deal with json, such as using python's json module or jq to beautify the output. The command is as follows:

# use python's json module to beautify docker inspect-- format= "{{json .Mounts}}" container | python-m json.tool# use jq to beautify docker inspect-- format= "{{json .Mounts}}" container | jq

View container network information

To view network information, you can 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 container network ip, gateway and other information docker inspect-format= "{{NetworkSettings.Networks}" container | jq

Extended learning

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

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

references

Docker official documentation

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report