Inspecting Containers and Images
LEVEL 0
The Problem
You need detailed information about a container or image.
- What environment variables are set?
- What volumes are mounted?
- What’s the container’s IP address?
- What layers make up this image?
- When was this image built?
docker ps gives you basic info. But for deep inspection, you need docker inspect.
LEVEL 1
The Concept — The X-Ray Machine
The Concept
Imagine an X-ray machine at an airport security checkpoint.
On the surface, a suitcase looks normal. But the X-ray reveals what’s inside: structure, contents, hidden compartments.
docker inspect is the X-ray for containers and images.
It reveals everything: configuration, state, networking, mounts, metadata.
LEVEL 2
The Mechanics — Using docker inspect
The Mechanics
Inspect a container:
docker inspect <container-name>
Returns JSON with full details.
Inspect an image:
docker image inspect <image-name>
Returns JSON with image metadata, layers, size, etc.
Extract specific fields with —format:
# Get container IP
docker inspect myapp --format='{{.NetworkSettings.IPAddress}}'
# Get exit code
docker inspect myapp --format='{{.State.ExitCode}}'
# Get mounts
docker inspect myapp --format='{{.Mounts}}'
# Get environment variables
docker inspect myapp --format='{{.Config.Env}}'
LEVEL 3
Useful Inspection Queries
Container state:
# Is it running?
docker inspect myapp --format='{{.State.Running}}'
# Exit code
docker inspect myapp --format='{{.State.ExitCode}}'
# Started at
docker inspect myapp --format='{{.State.StartedAt}}'
# Finished at
docker inspect myapp --format='{{.State.FinishedAt}}'
Networking:
# IP address
docker inspect myapp --format='{{.NetworkSettings.IPAddress}}'
# Ports
docker inspect myapp --format='{{.NetworkSettings.Ports}}'
# Networks
docker inspect myapp --format='{{.NetworkSettings.Networks}}'
Volumes and mounts:
# All mounts
docker inspect myapp --format='{{json .Mounts}}' | jq
# Specific mount
docker inspect myapp --format='{{range .Mounts}}{{.Source}} -> {{.Destination}}{{"\n"}}{{end}}'
Environment variables:
docker inspect myapp --format='{{range .Config.Env}}{{println .}}{{end}}'
Image layers:
docker image inspect nginx:alpine --format='{{json .RootFS.Layers}}' | jq
Shows the SHA256 hashes of all layers in the image.
Image size:
docker image inspect nginx --format='{{.Size}}'
LEVEL 4
Inspecting Image History
View layer history:
docker image history <image>
Shows each layer, the command that created it, and its size.
Example:
IMAGE CREATED BY SIZE
abc123 CMD ["nginx"] 0B
def456 COPY nginx.conf /etc/nginx/nginx.conf 2.5kB
ghi789 RUN apt-get install nginx 45MB
jkl012 FROM ubuntu:22.04 77MB
This shows how the image was built, layer by layer.
LEVEL 5
Practical Use Cases
1. Debugging network issues:
# Find container IP
docker inspect web --format='{{.NetworkSettings.IPAddress}}'
# Check what networks it's on
docker inspect web --format='{{range $net, $config := .NetworkSettings.Networks}}{{$net}}: {{$config.IPAddress}}{{"\n"}}{{end}}'
2. Finding mounted volumes:
docker inspect db --format='{{range .Mounts}}{{.Name}}: {{.Source}} -> {{.Destination}}{{"\n"}}{{end}}'
3. Checking resource limits:
# Memory limit
docker inspect myapp --format='{{.HostConfig.Memory}}'
# CPU limit
docker inspect myapp --format='{{.HostConfig.CpuQuota}}'
4. Finding why a container exited:
# Exit code
docker inspect myapp --format='{{.State.ExitCode}}'
# OOM killed?
docker inspect myapp --format='{{.State.OOMKilled}}'
# Error message
docker inspect myapp --format='{{.State.Error}}'
5. Inspecting image metadata:
# When was it built?
docker image inspect myapp --format='{{.Created}}'
# What's the entrypoint?
docker image inspect myapp --format='{{.Config.Entrypoint}}'
# What's the CMD?
docker image inspect myapp --format='{{.Config.Cmd}}'
# Environment variables baked into the image
docker image inspect myapp --format='{{.Config.Env}}'