Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Docker commands Guide – docker inspect with examples

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, or volume).

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

CommandDescription
docker inspect my_containerInspect a container
docker inspect nginx:latestInspect an image
docker inspect my_custom_networkInspect a network
docker inspect my_data_volumeInspect a volume
docker inspect -f '{{.State.Status}}' my_containerShow the container’s status
docker inspect -f '{{.NetworkSettings.IPAddress}}' my_containerShow the container’s IP address
docker inspect -f '{{.Config.Image}}' my_containerShow the base image used by the container
docker inspect -f '{{.HostConfig.Memory}}' my_containerShow the memory limit for the container
`docker inspect my_containerjq ‘.’`

Best Practices for Using docker inspect:

  1. Use --format for specific data extraction, especially when scripting.
  2. Combine with jq for easier JSON processing and filtering.
  3. Automate checks using inspection data in CI/CD pipelines.
  4. Monitor and audit images by inspecting their history and base image.
  5. Document important configuration details by saving the inspection output to a file.

Common Errors and Solutions

  1. “No such object”
    → Ensure the container, image, network, or volume exists. Use docker ps -a or docker images to verify.
  2. “Empty output for a field”
    → The field may not exist for the inspected object. Check the full JSON output first.
  3. “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

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x