In this topic, you will explore the docker inspect command. This command is a tool you can use to retrieve comprehensive information about Docker containers and other objects. You will explore examples of the command and syntax for inspecting containers, images, volumes, and more.
Docker inspect container
Docker inspect is a command used to retrieve detailed information about Docker objects such as containers, images, volumes, and networks. By using a Docker container, you can gain a deeper understanding of your Docker containers and troubleshoot issues if necessary. It is a valuable tool for container management and debugging in Docker-based environments.
Before you can inspect a container, you need to know its ID or name. To list all running containers, use the command: docker ps. Once you have the container ID or name, you can use the docker inspect command to get detailed information. Here's the syntax:
docker inspect <container-id or container-name>
This command will output a large JSON object containing information about the specified container. The information will include the following points:
- Configuration: Details about the container's configuration, such as its image, command, entry point, environment, variables, and exposed ports.
- Network settings: Information about the container's networking, including its IP address, port mapping, and attached networks.
- Volume mounts: A list of mounted volumes and their respective source and destination paths within the container.
- Environment variables: The environment variables that are defined with the container.
- Runtime information: Information about the container's runtime, such as its status, start time, and resource usage.
Options
Docker inspect command allows you to work with different objects such as containers, images, networks, volumes, and services, however, the syntax for each type of entity stays the same. To filter and display specific fields, you can utilize the format flag which helps customize the output according to your requirements.
For example, by using the --format flag, you can display the IP address of a container. Let's consider the following example: Assuming there is an nginx container in your local environment
$ docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' nginx
172.17.0.2
The output of this command provides the IP address of the nginx container.
If you need to check the size of the Docker container, you can utilize the --size flag. It provides information about the size of the specified container. This option only works for containers.
For example, to check the size of the above nginx containers, use the docker inspect command with the --size option and the container name or ID:
docker inspect --size 952b1ebe5e2b
The output of the command given above displays a large JSON file. This JSON file is the result of the docker inspect command along with additional details:
- SizeRootFs: This indicates the total bytes of all files inside the container.
- sizeRw: This measures the added or changed file size in bytes within the container compared to its original image.
"SizeRw": 1095,
"SizeRootFs": 186861413,
If you want to search for a particular type of object in docker you can use the --type flag options to specify the object type, followed by the object specifier.
For the previously mentioned nginx container, you can see the volume details using the following command:
$ docker inspect --type=volume nginx.conf
[
{
"CreatedAt": "2023-08-28T09:28:49Z",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/nginx.conf/_data",
"Name": "nginx.conf",
"Options": null,
"Scope": "local"
}
]
The output will provide details about the specified nginx volume.
Docker objects inspect
Apart from inspecting Docker containers, you can use docker inspect for other Docker objects. Here are some examples of inspecting different Docker objects:
Image inspect: To inspect docker images, provide the image's ID or its name.
docker inspect <image-id or image-name>
You'll receive detailed data regarding the image including its layers, exposed ports, labels, and other metadata.
Volume Inspect: To inspect Docker volumes, use the volume's ID or name.
docker inspect <volume-id or volume-name>
This command displays information about volume, such as its mount point, size, and the containers it is attached to.
Network Inspect: To inspect Docker network, you can use the network's ID or name
docker inspect <network-id or network-name>
The output contains details about the network including its subnet, connected container, and any custom configuration.
As shown in the examples above, you can also inspect for context and plugins.
Another use case of docker inspect is debugging. For example, you can use it to debug issues related to container utilization. If you observe a container running slowly or consuming excessive resources, you can use the inspect command to gather information about its CPU, memory, and disk usage. This approach allows you to troubleshoot and resolve optimization concerns, enabling you to optimize resource allocation for enhanced container performance.
Conclusion
In conclusion, the Docker inspect command is an essential tool for gaining deeper insights into various Docker objects. By using the docker inspect command, users can access valuable information about containers, images, volumes, networks, as well as context and plugins. This information is invaluable for container management, troubleshooting, and debugging in Docker-based environments.