Tips for using the docker inspect command

Tips for using the docker inspect command

Description and Introduction

Docker inspect is a native command of the Docker client, which is used to view the underlying basic information of Docker objects. Including container ID, creation time, running status, startup parameters, directory mounting, 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

The following table is taken 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 table above, --type is used to specify the docker object type, such as container, image. This can be used when the container and the image have the same name, and is used less frequently. For example, if a container on your machine is named redis and an image is redis:latest, you can use the following command to view the image information. If the type parameter is not used, container information is returned:

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

# View the redis container information docker inspect redis

--size is used to view the file size of the container. With this parameter, the output will include SizeRootFs and SizeRw (I am not sure what these two values ​​mean yet, and I hope someone in the know can let me know).

The above two parameters are rarely used. --format is the most practical and is used more frequently. From the table description, we can see that the parameter value passed in should be a template in the go language. It is very powerful and can perform many operations of Go functions. Since I haven't gotten started with the Go language yet, I won't talk too much about its acrobatics here to avoid making mistakes. Let me talk about the commonly used ones below.

practice

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

View directory mount information

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

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

The json in the parameter is the method name of the Go language, followed by the value of Mounts converted to json. It is also possible to remove json.
If you think this input is still not very nice, you can further process the 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 the network IP, gateway and other information of the container 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 can almost write Go code. 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 perform acrobatics, but this article will not teach you how to do it.

References
Docker official documentation

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of how to install Docker and operate commands under CentOS 7
  • Docker cleanup command collection
  • Detailed explanation of how to use the Docker run command
  • Summary of Docker's commonly used commands for clearing container images
  • Does Docker need to restart after modifying files (command details)
  • Docker Basics: Detailed Explanation of Dockerfile Commands
  • Detailed explanation of Docker service command (summary)
  • Summary of common commands in Dockerfile
  • Docker common commands summary (practical version)
  • Solution to the problem that commands cannot be completed during docker testing
  • Docker common command operation method

<<:  WeChat applet implements a simple calculator

>>:  15 important variables you must know about MySQL performance tuning (summary)

Recommend

A brief discussion on the perfect adaptation solution for Vue mobile terminal

Preface: Based on a recent medical mobile project...

How to deploy ElasticSearch in Docker

1. What is ElasticSearch? Elasticsearch is also d...

Introduction to the use of CSS3 filter attribute

1. Introduction When writing animation effects fo...

Detailed explanation of publicPath usage in Webpack

Table of contents output output.path output.publi...

How to use ECharts in WeChat Mini Programs using uniapp

Today, we use uniapp to integrate Echarts to disp...

How to quickly install RabbitMQ in Docker

1. Get the image #Specify the version that includ...

Linux Check the installation location of the software simple method

1. Check the software installation path: There is...

MySQL/MariaDB Root Password Reset Tutorial

Preface Forgotten passwords are a problem we ofte...

Application of CSS3 animation effects in activity pages

background Before we know it, a busy year is comi...

Let's talk about the problem of Vue integrating sweetalert2 prompt component

Table of contents 1. Project Integration 1. CDN i...

Analysis of MySQL joint index function and usage examples

This article uses examples to illustrate the func...

Detailed steps for porting busybox to build a minimal root file system

Busybox: A Swiss Army knife filled with small com...

Vue custom table column implementation process record

Table of contents Preface Rendering setTable comp...