Bird
Raised Fist0
Dockerdevops~5 mins

Inspecting container details in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you need to see what is happening inside a running container or check its settings. Inspecting container details helps you understand its status, configuration, and resource usage.
When you want to check the IP address or network settings of a running container
When you need to see environment variables or volumes attached to a container
When you want to verify the container's current state and resource limits
When debugging why a container is not behaving as expected
When you want to see logs or the command used to start the container
Commands
This command lists all running containers so you can find the container ID or name to inspect.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b1f3c2a4d5e6 nginx:1.23.3 "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:8080->80/tcp example-nginx
→
-a - Show all containers, including stopped ones
This command shows detailed JSON information about the container named 'example-nginx', including network, volumes, and environment variables.
Terminal
docker inspect example-nginx
Expected OutputExpected
[ { "Id": "b1f3c2a4d5e6f7g8h9i0jklmnopqrstuv", "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, "StartedAt": "2024-06-01T12:00:05.000000000Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:abcdef1234567890", "NetworkSettings": { "IPAddress": "172.17.0.2", "Ports": { "80/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "8080" } ] } }, "Mounts": [] } ]
This command shows the logs output by the container, useful for debugging or checking what the container is doing.
Terminal
docker logs example-nginx
Expected OutputExpected
172.17.0.1 - - [01/Jun/2024:12:01:00 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0"
→
-f - Follow the log output live
Key Concept

If you remember nothing else from this pattern, remember: docker inspect shows all the detailed settings and state of a container in JSON format.

Common Mistakes
Trying to inspect a container using its image name instead of container name or ID
docker inspect requires a container name or ID, not an image name, so it will fail or show wrong info
Use 'docker ps' to find the container name or ID, then run 'docker inspect' with that name or ID
Running 'docker logs' on a container that is not running
If the container is stopped or does not exist, logs command will fail or show no output
Check container status with 'docker ps -a' before running logs
Summary
Use 'docker ps' to list running containers and find the container name or ID.
Use 'docker inspect <container>' to see detailed JSON info about the container's settings and state.
Use 'docker logs <container>' to view the container's output logs for debugging.

Practice

(1/5)
1. What does the command docker inspect CONTAINER do?
easy
A. Shows detailed JSON information about the specified container
B. Starts the specified container
C. Deletes the specified container
D. Lists all running containers

Solution

  1. Step 1: Understand the command purpose

    The docker inspect command is used to get detailed information about Docker objects like containers.
  2. Step 2: Identify the output type

    It returns detailed JSON data about the container's configuration and state.
  3. Final Answer:

    Shows detailed JSON information about the specified container -> Option A
  4. Quick Check:

    docker inspect = detailed JSON info [OK]
Hint: Remember: inspect means see detailed info [OK]
Common Mistakes:
  • Confusing inspect with start or delete commands
  • Expecting a simple list instead of JSON output
  • Using inspect without specifying a container
2. Which of the following is the correct syntax to get only the container's IP address using docker inspect?
easy
A. docker inspect --format='{{.NetworkSettings.IPAddress}}' CONTAINER
B. docker inspect --ip CONTAINER
C. docker inspect -ip CONTAINER
D. docker inspect --format='{{IPAddress}}' CONTAINER

Solution

  1. Step 1: Recall the --format option usage

    The --format flag uses Go templates to extract specific fields from the JSON output.
  2. Step 2: Identify the correct template for IP address

    The IP address is under .NetworkSettings.IPAddress, so the correct syntax is {{.NetworkSettings.IPAddress}}.
  3. Final Answer:

    docker inspect --format='{{.NetworkSettings.IPAddress}}' CONTAINER -> Option A
  4. Quick Check:

    Use --format with correct JSON path [OK]
Hint: Use --format with full JSON path in double braces [OK]
Common Mistakes:
  • Using incorrect flags like --ip or -ip
  • Missing the dot before NetworkSettings in template
  • Not quoting the template string properly
3. Given a container named webapp, what will the command docker inspect --format='{{.State.Status}}' webapp output if the container is running?
medium
A. created
B. paused
C. exited
D. running

Solution

  1. Step 1: Understand the .State.Status field

    The .State.Status field shows the current status of the container, such as running, exited, paused, or created.
  2. Step 2: Match the container state

    If the container is running, the output will be the string running.
  3. Final Answer:

    running -> Option D
  4. Quick Check:

    .State.Status = running when container runs [OK]
Hint: Check .State.Status for container current state [OK]
Common Mistakes:
  • Confusing status with container name or ID
  • Expecting JSON output instead of plain string
  • Using wrong field like .Status instead of .State.Status
4. You run docker inspect --format='{{.Config.Env}}' mycontainer but get an error. What is the most likely cause?
medium
A. The container name is misspelled
B. The format string syntax is incorrect
C. The .Config.Env field does not exist in the container JSON
D. The container is not running

Solution

  1. Step 1: Check the format string syntax

    The .Config.Env field is an array, so printing it directly requires proper formatting.
  2. Step 2: Identify the syntax error

    Using {{.Config.Env}} tries to print an array directly, which causes a template error. You need to format it, for example with {{range .Config.Env}}{{println .}}{{end}}.
  3. Final Answer:

    The format string syntax is incorrect -> Option B
  4. Quick Check:

    Arrays need special formatting in --format [OK]
Hint: Arrays need loops in --format templates [OK]
Common Mistakes:
  • Assuming all fields print directly without formatting
  • Ignoring container name correctness
  • Thinking container must be running to inspect
5. You want to list all container names and their IP addresses in a simple format. Which command correctly achieves this?
hard
A. docker ps -q | xargs -n1 docker inspect --format='{{.Name}} {{.NetworkSettings.IPAddress}}'
B. docker ps --format='{{.Name}} {{.NetworkSettings.IPAddress}}'
C. docker inspect --format='{{.Name}} {{.NetworkSettings.IPAddress}}' $(docker ps -q)
D. docker inspect $(docker ps) --format='{{.Name}} {{.NetworkSettings.IPAddress}}'

Solution

  1. Step 1: Understand how to get container IDs

    docker ps -q lists container IDs only, which can be passed to docker inspect.
  2. Step 2: Use inspect with multiple container IDs and format

    Passing IDs as arguments to docker inspect with --format outputs info for each container. docker inspect --format='{{.Name}} {{.NetworkSettings.IPAddress}}' $(docker ps -q) uses this correctly.
  3. Step 3: Check other options for errors

    docker ps -q | xargs -n1 docker inspect --format='{{.Name}} {{.NetworkSettings.IPAddress}}' uses xargs but may break if container names have spaces. docker ps --format='{{.Name}} {{.NetworkSettings.IPAddress}}' is invalid because docker ps does not support .NetworkSettings.IPAddress. docker inspect $(docker ps) --format='{{.Name}} {{.NetworkSettings.IPAddress}}' uses wrong syntax for docker ps.
  4. Final Answer:

    docker inspect --format='{{.Name}} {{.NetworkSettings.IPAddress}}' $(docker ps -q) -> Option C
  5. Quick Check:

    Use docker inspect with IDs from docker ps -q [OK]
Hint: Use docker inspect with IDs from docker ps -q [OK]
Common Mistakes:
  • Trying to get IP from docker ps directly
  • Using wrong command substitution syntax
  • Not passing container IDs correctly to inspect
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