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
Code language: CSS (css)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
Example Output:
172.17.0.2
Code language: CSS (css)
7. Show the Base Image of a Container
docker inspect -f '{{.Config.Image}}' my_container
Code language: JavaScript (javascript)
8. Check the Mount Point of a Volume
docker inspect -f '{{.Mountpoint}}' my_data_volume
Code language: JavaScript (javascript)
9. Use JSON Formatting for Better Readability
docker inspect my_container | jq '.'
Code language: JavaScript (javascript)
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)
Code language: JavaScript (javascript)
12. Find Environment Variables in a Container
docker inspect -f '{{json .Config.Env}}' my_container
Code language: JavaScript (javascript)
Example Output:
["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin", "APP_ENV=production"]
Code language: JSON / JSON with Comments (json)
13. Get CPU and Memory Limits
docker inspect -f 'CPU: {{.HostConfig.NanoCpus}}, Memory: {{.HostConfig.Memory}}' my_container
Code language: JavaScript (javascript)
14. Save Inspection Data to a File
docker inspect my_container > container_info.json
Code language: CSS (css)
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 inspectinto 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
--formatfor specific data extraction, especially when scripting. - Combine with
jqfor 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 -aordocker imagesto 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--formatto 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}}'
Code language: JavaScript (javascript)
Validate and Log Container Configurations
docker inspect my_app_container > app_config.json
Code language: CSS (css)
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals