How to Use Docker Inspect for Debugging Containers
Use
docker inspect [container_name_or_id] to get detailed JSON output about a container's configuration and state. This helps debug issues by showing network settings, mounts, environment variables, and runtime status.Syntax
The basic syntax of docker inspect is simple and flexible:
docker inspect [OPTIONS] CONTAINER|IMAGE|VOLUME|NETWORK- CONTAINER|IMAGE|VOLUME|NETWORK: Specify the name or ID of the Docker object to inspect.
- OPTIONS: Flags like
--formatto customize output.
This command returns detailed JSON data about the specified Docker object.
bash
docker inspect [OPTIONS] CONTAINER|IMAGE|VOLUME|NETWORK
Example
This example shows how to inspect a running container named myapp. It outputs all details in JSON format, which helps you see configuration and runtime info for debugging.
bash
docker inspect myapp
Output
[
{
"Id": "e90e34656806",
"Created": "2024-06-01T12:00:00.000000000Z",
"Path": "/bin/sh",
"Args": ["-c", "npm start"],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 12345,
"ExitCode": 0,
"StartedAt": "2024-06-01T12:01:00.000000000Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"NetworkSettings": {
"IPAddress": "172.17.0.2",
"Ports": {
"80/tcp": [{"HostIp": "0.0.0.0", "HostPort": "8080"}]
}
},
"Mounts": [
{
"Type": "volume",
"Name": "mydata",
"Source": "/var/lib/docker/volumes/mydata/_data",
"Destination": "/data",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
]
}
]
Common Pitfalls
Common mistakes when using docker inspect include:
- Using the wrong container or image name/ID, which returns an error or empty result.
- Not using
--formatto filter output, making it hard to find needed info in large JSON. - Confusing container IDs with image IDs.
Always verify the container is running or exists before inspecting.
bash
docker inspect wrongname
# Error: No such object: wrongname
docker inspect --format='{{.State.Status}}' myapp
# Outputs: runningOutput
Error: No such object: wrongname
running
Quick Reference
Tips for effective debugging with docker inspect:
- Use
docker psto list containers and get correct names/IDs. - Use
--formatwith Go templates to extract specific fields, e.g.,docker inspect --format='{{.NetworkSettings.IPAddress}}' myapp. - Look at
.Statefor container status and.Mountsfor volume info. - Combine with
jqtool for pretty JSON filtering.
Key Takeaways
Use
docker inspect to get detailed JSON info about containers for debugging.Always specify the correct container or image name/ID to avoid errors.
Use
--format to filter output and find relevant details quickly.Check container
.State and .NetworkSettings for runtime issues.Combine with tools like
jq for easier JSON parsing.