How to Inspect a Container in Docker: Commands and Examples
Use the
docker inspect <container_id_or_name> command to view detailed information about a Docker container. This command shows configuration, state, network settings, and more in JSON format.Syntax
The docker inspect command retrieves detailed information about a container or other Docker objects in JSON format.
docker inspect <container_id_or_name>: Inspect a specific container by its ID or name.docker inspect --format='{{json .}}' <container_id_or_name>: Customize output using Go templates.
bash
docker inspect <container_id_or_name>
Example
This example shows how to inspect a running container named my_container. It outputs detailed JSON data about the container's configuration and state.
bash
docker inspect my_container
Output
[
{
"Id": "e90e34656806",
"Created": "2024-06-01T12:00:00.000000000Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 12345,
"ExitCode": 0,
"Error": "",
"StartedAt": "2024-06-01T12:01:00.000000000Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Name": "/my_container",
"Image": "sha256:abc123def456",
"NetworkSettings": {
"IPAddress": "172.17.0.2",
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "8080"
}
]
}
}
}
]
Common Pitfalls
Common mistakes when inspecting containers include:
- Using the wrong container ID or name, which causes an error.
- Expecting human-readable output; the output is JSON and may need formatting.
- Not using
--formatto extract specific fields, making output hard to read.
Always verify the container exists with docker ps -a before inspecting.
bash
docker inspect wrong_container_name # Error: No such object: wrong_container_name # Correct usage: docker inspect my_container
Quick Reference
Summary tips for inspecting Docker containers:
- Use
docker ps -ato list containers and get correct IDs or names. - Use
docker inspect <container>to get full JSON details. - Use
docker inspect --format='{{.State.Status}}' <container>to get specific info like status. - Pipe output to
jqfor easier JSON reading:docker inspect <container> | jq.
Key Takeaways
Use
docker inspect <container_id_or_name> to get detailed JSON info about a container.Verify container ID or name with
docker ps -a before inspecting.Output is JSON; use tools like
jq or --format for easier reading.Common errors come from misspelling container names or IDs.
Inspecting helps understand container state, network, and configuration.