Bird
Raised Fist0
Dockerdevops~5 mins

Docker inspect for detailed info - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you need to see all the details about a running or stopped Docker container or image. Docker inspect shows you this detailed information in a structured way so you can understand how your container or image is configured and running.
When you want to check the IP address or network settings of a running container
When you need to see the exact command a container was started with
When you want to find the mount points or volumes used by a container
When you want to debug why a container is not working by checking its configuration
When you want to see metadata like environment variables or exposed ports of an image
Commands
This command runs an Nginx container in the background, names it 'example-nginx', and maps port 8080 on your computer to port 80 in the container.
Terminal
docker run -d --name example-nginx -p 8080:80 nginx:1.23
Expected OutputExpected
a1b2c3d4e5f67890123456789abcdef0123456789abcdef0123456789abcdef0
→
-d - Run container in detached mode (in background)
→
--name - Assign a custom name to the container
→
-p - Map host port to container port
This command shows detailed JSON information about the 'example-nginx' container, including network settings, volumes, environment variables, and more.
Terminal
docker inspect example-nginx
Expected OutputExpected
[ { "Id": "a1b2c3d4e5f67890123456789abcdef0123456789abcdef0123456789abcdef0", "Created": "2024-06-01T12:00:00.000000000Z", "Path": "/docker-entrypoint.sh", "Args": [ "nginx", "-g", "daemon off;" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 12345, "ExitCode": 0, "Error": "", "StartedAt": "2024-06-01T12:00:01.000000000Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Name": "/example-nginx", "Config": { "Hostname": "a1b2c3d4e5f6", "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "nginx", "-g", "daemon off;" ], "Image": "nginx:1.23", "ExposedPorts": { "80/tcp": {} } }, "NetworkSettings": { "Ports": { "80/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "8080" } ] }, "IPAddress": "172.17.0.2" } } ]
This command extracts and shows only the IP address of the 'example-nginx' container using a Go template format.
Terminal
docker inspect --format='{{.NetworkSettings.IPAddress}}' example-nginx
Expected OutputExpected
172.17.0.2
→
--format - Format output using a Go template to show specific fields
Key Concept

Docker inspect shows all the detailed settings and status of a container or image in JSON format, helping you understand exactly how it is configured and running.

Common Mistakes
Running docker inspect without specifying a container or image name
Docker will return an error because it needs to know which container or image to inspect
Always provide the container name, container ID, or image name after docker inspect
Trying to inspect a container that is not running or does not exist
Docker will return an error or empty output because the container is not found
Check running containers with docker ps or all containers with docker ps -a before inspecting
Not using --format when you want specific info and getting overwhelmed by full JSON output
The full JSON is large and hard to read for simple info like IP address or ports
Use --format with Go templates to extract only the needed fields
Summary
Run a container with a name and port mapping to have a target for inspection.
Use docker inspect with the container name to see detailed JSON info about its configuration and state.
Use docker inspect with --format to extract specific details like IP address in a simple way.

Practice

(1/5)
1. What is the primary purpose of the docker inspect command?
easy
A. To start a new Docker container
B. To show detailed information about Docker objects like containers and images
C. To remove unused Docker images
D. To list all running Docker containers

Solution

  1. Step 1: Understand the command's function

    docker inspect is used to get detailed JSON data about Docker objects such as containers, images, volumes, and networks.
  2. Step 2: Compare with other Docker commands

    Starting containers is done with docker run, removing images with docker rmi, and listing containers with docker ps. None of these show detailed info like docker inspect.
  3. Final Answer:

    To show detailed information about Docker objects like containers and images -> Option B
  4. Quick Check:

    docker inspect = detailed info [OK]
Hint: Remember: inspect means look closely at details [OK]
Common Mistakes:
  • Confusing inspect with commands that start or list containers
  • Thinking inspect modifies containers
  • Assuming inspect only works for images
2. Which of the following is the correct syntax to inspect a container named myapp?
easy
A. docker inspect myapp
B. docker inspect -n myapp
C. docker inspect --container myapp
D. docker inspect container=myapp

Solution

  1. Step 1: Recall the basic syntax of docker inspect

    The command is docker inspect [OPTIONS] NAME|ID. No extra flags are needed to specify container name.
  2. Step 2: Evaluate each option

    docker inspect myapp matches the correct syntax. Options B, C, and D use invalid flags or formats not recognized by Docker.
  3. Final Answer:

    docker inspect myapp -> Option A
  4. Quick Check:

    docker inspect + container name = correct syntax [OK]
Hint: Use just 'docker inspect' plus container name or ID [OK]
Common Mistakes:
  • Adding unnecessary flags like -n or --container
  • Using equal signs in command arguments
  • Confusing inspect syntax with other Docker commands
3. What will be the output of the command docker inspect --format='{{.NetworkSettings.IPAddress}}' mycontainer?
medium
A. An error because the format is incorrect
B. The container's environment variables
C. The container's mounted volumes
D. The IP address assigned to the container named mycontainer

Solution

  1. Step 1: Understand the --format option

    The --format flag allows extracting specific fields from the JSON output using Go templates. Here, {{.NetworkSettings.IPAddress}} targets the container's IP address.
  2. Step 2: Match the template to the expected output

    This template returns the IP address string of the container named mycontainer. Other options like environment variables or volumes require different template paths.
  3. Final Answer:

    The IP address assigned to the container named mycontainer -> Option D
  4. Quick Check:

    --format with IPAddress = container IP [OK]
Hint: Use --format with .NetworkSettings.IPAddress for container IP [OK]
Common Mistakes:
  • Confusing IPAddress with environment variables
  • Using wrong template syntax causing errors
  • Expecting full JSON output instead of filtered data
4. You run docker inspect --format='{{.Config.Env}}' mycontainer but get an empty output. What is the most likely reason?
medium
A. The container has no environment variables set
B. The container name is misspelled
C. The format string is missing quotes
D. The Docker daemon is not running

Solution

  1. Step 1: Check the format string correctness

    The format string {{.Config.Env}} is valid and properly quoted, so syntax is correct.
  2. Step 2: Consider container state and data

    If the container exists and the command runs without error but returns empty, it means no environment variables were set in the container configuration.
  3. Final Answer:

    The container has no environment variables set -> Option A
  4. Quick Check:

    Empty Env output = no env vars set [OK]
Hint: Empty Env output usually means no env vars set [OK]
Common Mistakes:
  • Assuming empty output means syntax error
  • Not verifying container name correctness
  • Ignoring Docker daemon status errors
5. You want to list all mount points of a container named webapp using docker inspect. Which command will correctly extract this information?
hard
A. docker inspect --format='{{.MountPoints}}' webapp
B. docker inspect --format='{{.Config.Mounts}}' webapp
C. docker inspect --format='{{json .Mounts}}' webapp
D. docker inspect --format='{{.NetworkSettings.Mounts}}' webapp

Solution

  1. Step 1: Identify the correct JSON path for mounts

    The .Mounts field contains an array of mount point objects. Using {{json .Mounts}} converts this array to JSON string output.
  2. Step 2: Evaluate other options for correctness

    .Config.Mounts and .NetworkSettings.Mounts do not exist. .MountPoints is not a valid field in Docker inspect output.
  3. Final Answer:

    docker inspect --format='{{json .Mounts}}' webapp -> Option C
  4. Quick Check:

    Use {{json .Mounts}} to get mount points [OK]
Hint: Use {{json .Mounts}} to get mount info in JSON format [OK]
Common Mistakes:
  • Using incorrect JSON paths like .Config.Mounts
  • Forgetting to use json function for arrays
  • Assuming mount info is under NetworkSettings
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard