🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

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, including its purpose, how it works, and a comprehensive list of examples.


What is docker inspect?

docker inspect is a Docker command used to retrieve detailed information about containers, images, networks, and volumes in JSON format. It provides extensive data such as configuration, networking, mounts, process details, and more.

Key Features:

  • Works with containers, images, networks, and volumes.
  • Returns data in JSON format.
  • Ideal for debugging, auditing, and scripting.
  • Supports filtering specific data with --format.

Basic Syntax

docker inspect [OPTIONS] NAME|ID [NAME|ID...]

Options:

  • --format: Format the output using a Go template.
  • --type: Specify the type of object (container, image, network, or volume).

Examples of docker inspect

1. Inspect a Running Container

docker inspect my_container

This returns the full JSON output containing all details about my_container.


2. Inspect a Docker Image

docker inspect ubuntu

This command retrieves metadata for the ubuntu image, including its ID, creation date, layers, and configuration.


3. Inspect a Network

docker inspect my_network

This shows details of the my_network Docker network, such as the driver type (bridge, overlay), subnet, and connected containers.


4. Inspect a Volume

docker inspect my_volume

This retrieves information about the my_volume volume, including its mount path and creation time.


Using --format to Filter Specific Information

The full JSON output can be overwhelming. Use the --format option to extract specific data using Go templates.

5. Get a Container’s IP Address

docker inspect --format='{{ .NetworkSettings.IPAddress }}' my_container

This returns only the IP address of my_container.


6. Get a Container’s Mounts

docker inspect --format='{{ json .Mounts }}' my_container

This returns details about the container’s mounted volumes in JSON format.


7. Get the Container’s Image Name

docker inspect --format='{{ .Config.Image }}' my_container

8. Get the Image ID of an Image

docker inspect --format='{{ .Id }}' ubuntu

This returns the unique ID of the ubuntu image.


9. Get a Container’s Environment Variables

docker inspect --format='{{ .Config.Env }}' my_container

This returns all environment variables set in my_container.


10. Check the Restart Policy of a Container

docker inspect --format='{{ .HostConfig.RestartPolicy.Name }}' my_container

11. Get the Labels of a Container

docker inspect --format='{{ .Config.Labels }}' my_container

This returns any labels associated with the container.


12. Check the Container’s State (Running, Stopped, or Paused)

docker inspect --format='{{ .State.Status }}' my_container

This shows whether the container is running, exited, or paused.


13. Inspect Multiple Containers at Once

docker inspect my_container1 my_container2

This displays detailed information about both containers.


14. Get the Path to the Container’s Log File

docker inspect --format='{{ .LogPath }}' my_container

15. Get Network Information (IP, Gateway, and Subnet)

docker inspect --format='{{ .NetworkSettings.Networks.bridge.IPAddress }}' my_container

This command extracts the IP address from the bridge network.


Examples of Inspecting Specific Object Types (--type)

16. Inspect Only Containers

docker inspect --type container my_container

17. Inspect Only Networks

docker inspect --type network my_network

18. Inspect Only Images

docker inspect --type image ubuntu

Combining docker inspect with Other Commands

19. Check if a Container is Running Before Stopping It

if [ "$(docker inspect --format='{{ .State.Running }}' my_container)" == "true" ]; then
  docker stop my_container
fi

20. Monitor a Container’s CPU Usage

docker inspect --format='{{ .HostConfig.CpuShares }}' my_container

List of Common docker inspect Commands

CommandDescription
docker inspect my_containerInspect a running container
docker inspect ubuntuInspect a Docker image
docker inspect my_networkInspect a Docker network
docker inspect my_volumeInspect a Docker volume
docker inspect --format='{{ .Config.Env }}'Get environment variables from a container
docker inspect --format='{{ .State.Status }}'Check if a container is running, exited, or paused
docker inspect --type networkInspect networks only
docker inspect --type imageInspect images only

Best Practices for Using docker inspect:

  1. Use --format to filter and avoid scrolling through long JSON output.
  2. Automate monitoring scripts using filtered docker inspect data.
  3. Inspect logs and network settings for debugging.
  4. Combine with other Docker commands for advanced workflows.

Common Errors and Solutions

  1. “No such object: “
    → Ensure the container, image, or network exists. Check with docker ps, docker images, or docker network ls.
  2. Incorrect JSON Path with --format
    → Verify the JSON structure using the full docker inspect output before creating a custom filter.

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