Here’s a complete tutorial on docker inspect
, covering what it does, examples, and use cases.
What is docker inspect
?
docker inspect
is a Docker command that retrieves detailed information about Docker objects, such as containers, images, networks, and volumes. It returns the full JSON-formatted metadata, including configuration, network settings, resource usage, and more.
Key Features:
- Retrieves deep insights into Docker objects.
- Helps in troubleshooting and debugging containers and networks.
- Supports filtering and formatting the output for specific fields.
Basic Syntax
docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Common Options:
-f
,--format
: Format the output using Go templates ({{.State.Status}}
).--type
: Specify the object type (container
,image
,network
, orvolume
).
Examples of docker inspect
1. Inspect a Running Container
docker inspect my_container
This returns a JSON document containing detailed information about my_container
.
2. Inspect an Image
docker inspect nginx:latest
This shows metadata about the nginx:latest
image, such as the layers, commands used to build it, and environment variables.
3. Inspect a Network
docker inspect my_custom_network
This returns network configuration details like subnet, gateway, and connected containers.
4. Inspect a Volume
docker inspect my_data_volume
This provides information about the volume, including its mount point and usage.
5. Format the Output to Show a Specific Field
docker inspect -f '{{.State.Status}}' my_container
Example Output:
running
This extracts the container’s status (running, exited, etc.).
6. Show Container’s IP Address
docker inspect -f '{{.NetworkSettings.IPAddress}}' my_container
Example Output:
172.17.0.2
7. Show the Base Image of a Container
docker inspect -f '{{.Config.Image}}' my_container
8. Check the Mount Point of a Volume
docker inspect -f '{{.Mountpoint}}' my_data_volume
9. Use JSON Formatting for Better Readability
docker inspect my_container | jq '.'
This pipes the JSON output to jq
for pretty-printing and easier reading.
10. Inspect Multiple Containers at Once
docker inspect my_container1 my_container2
This returns JSON data for both containers.
11. Filter Containers with Specific Metadata
List all containers and filter for those with a specific status:
docker inspect -f '{{.Name}}: {{.State.Status}}' $(docker ps -q)
12. Find Environment Variables in a Container
docker inspect -f '{{json .Config.Env}}' my_container
Example Output:
["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin", "APP_ENV=production"]
13. Get CPU and Memory Limits
docker inspect -f 'CPU: {{.HostConfig.NanoCpus}}, Memory: {{.HostConfig.Memory}}' my_container
14. Save Inspection Data to a File
docker inspect my_container > container_info.json
Use Cases for docker inspect
1. Debugging and Troubleshooting
- Inspect container logs and metadata to diagnose problems.
- Example: Check the container’s restart policy or network configuration to identify issues.
2. Verifying Container Configurations
- Ensure containers are configured with the correct environment variables, resource limits, and command options.
- Example: Verify that a database container is using the expected port and volume.
3. Auditing and Security
- Audit images for base image and build commands.
- Example: Ensure that containers do not contain unnecessary or outdated software.
4. Network Analysis
- Inspect Docker networks to find IP addresses, subnets, and connected containers.
- Example: Check if a container is connected to the right network.
5. Monitoring and Resource Management
- Monitor resource allocations for containers, such as CPU and memory limits.
- Example: Track containers that exceed their allocated memory.
6. Automating Configuration Checks
- Integrate
docker inspect
into scripts to automatically validate container configurations.
List of Common docker inspect
Commands
Command | Description |
---|---|
docker inspect my_container | Inspect a container |
docker inspect nginx:latest | Inspect an image |
docker inspect my_custom_network | Inspect a network |
docker inspect my_data_volume | Inspect a volume |
docker inspect -f '{{.State.Status}}' my_container | Show the container’s status |
docker inspect -f '{{.NetworkSettings.IPAddress}}' my_container | Show the container’s IP address |
docker inspect -f '{{.Config.Image}}' my_container | Show the base image used by the container |
docker inspect -f '{{.HostConfig.Memory}}' my_container | Show the memory limit for the container |
`docker inspect my_container | jq ‘.’` |
Best Practices for Using docker inspect
:
- Use
--format
for specific data extraction, especially when scripting. - Combine with
jq
for easier JSON processing and filtering. - Automate checks using inspection data in CI/CD pipelines.
- Monitor and audit images by inspecting their history and base image.
- Document important configuration details by saving the inspection output to a file.
Common Errors and Solutions
- “No such object”
→ Ensure the container, image, network, or volume exists. Usedocker ps -a
ordocker images
to verify. - “Empty output for a field”
→ The field may not exist for the inspected object. Check the full JSON output first. - “JSON output too large”
→ Use--format
to filter and reduce the output size.
Combining docker inspect
with Other Commands
Monitor and Check Container Status
docker ps -q | xargs -n 1 docker inspect -f '{{.Name}}: {{.State.Status}}'
Validate and Log Container Configurations
docker inspect my_app_container > app_config.json
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND