Inspecting Containers
LEVEL 0
The Problem
You need to know details about a running container:
- What’s its IP address?
- What ports are mapped?
- What environment variables are set?
- When was it created?
- What image is it using?
You can’t just look at the container — you need to query this metadata.
LEVEL 1
The Concept — The Container's ID Card
The Concept
Every container has an “ID card” with complete information about itself. This isn’t information the container shows you — it’s information Docker keeps about the container.
docker inspect lets you read this ID card. It’s like asking “tell me everything you know about this container.”
LEVEL 2
The Mechanics — Inspecting Containers
The Mechanics
Full Inspection
docker inspect mycontainer
Returns a JSON document with everything: state, configuration, network settings, mounts, and more.
Getting Specific Values
The --format flag (Go templates) extracts specific values:
# Get IP address
docker inspect --format '{{.NetworkSettings.IPAddress}}' mycontainer
# Get mapped ports
docker inspect --format '{{.NetworkSettings.Ports}}' mycontainer
# Get environment variables
docker inspect --format '{{.Config.Env}}' mycontainer
# Get container state
docker inspect --format '{{.State.Status}}' mycontainer
# Get start time
docker inspect --format '{{.State.StartedAt}}' mycontainer
LEVEL 3
Key Information in Inspect Output
State Information
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"StartedAt": "2024-01-15T10:00:00.000000000Z",
"FinishedAt": "0001-01-01T00:00:00Z",
"ExitCode": 0
}
Network Information
"NetworkSettings": {
"IPAddress": "172.17.0.2",
"Ports": {
"80/tcp": [{ "HostIp": "0.0.0.0", "HostPort": "8080" }]
},
"Networks": { ... }
}
Mount Information
"Mounts": [
{
"Type": "volume",
"Source": "/var/lib/docker/volumes/mydata/_data",
"Destination": "/data"
}
]
Configuration
"Config": {
"Image": "nginx:latest",
"Env": ["PATH=...", "NGINX_VERSION=1.24.0"],
"Cmd": ["nginx", "-g", "daemon off;"],
"WorkingDir": "/",
"ExposedPorts": { "80/tcp": {} }
}
LEVEL 4
Practical Use Cases
Debugging Network Issues
# What's the container's IP?
docker inspect --format '{{.NetworkSettings.IPAddress}}' mycontainer
# What network is it on?
docker inspect --format '{{.NetworkSettings.Networks}}' mycontainer
Checking Port Mappings
docker inspect --format '{{range $p, $conf := .NetworkSettings.Ports}}{{$p}} -> {{(index $conf 0).HostPort}}{{"\n"}}{{end}}' mycontainer
Finding the Image Used
docker inspect --format '{{.Config.Image}}' mycontainer