Sub topic 2
Finding the Container and Reading Its State with docker ps and docker inspect
This chapter assumes you know basic Docker usage (images, docker run, docker build), can use a Linux shell, and know the difference between ENTRYPOINT and CMD. It also assumes you have seen the container states from the previous chapter: created, running, restarting, paused, exited, and dead.
Every debugging session starts with two questions. Which container is it, and what does Docker itself say happened to it? docker ps answers the first. docker inspect answers the second.
Finding the container with docker ps
docker ps lists running containers only. A container that crashed is not shown unless you add -a.
docker ps
docker ps -aThe most common mistake in a debugging session is running docker ps, seeing nothing, and concluding the container does not exist. A failed container is usually in the exited state, so use -a first.
Reading the columns
| Column | What it tells you |
|---|---|
| `CONTAINER ID` | A shortened ID. Any unique prefix works in later commands. |
| `IMAGE` | The image the container was created from. A tag may have moved since then. |
| `COMMAND` | The command the container runs, truncated by default. |
| `CREATED` | When the container was created, not when it last started. |
| `STATUS` | The state, plus time information and health. |
| `PORTS` | Published ports. An empty column on a service you expect to reach is a clue. |
| `NAMES` | The container name. |
The STATUS column carries a lot of information in a short string. Learn to read it:
Up 3 minutesmeans the container is running and has been for 3 minutes.Up 3 minutes (healthy)or(unhealthy)means a health check is defined and reports that result.Up 5 secondson a container you started an hour ago means it was restarted recently.Restarting (1) 12 seconds agomeans the restart policy keeps bringing the container back after it exits with code 1.Exited (137) 2 minutes agomeans the main process ended with exit code 137. The next chapters explain how to interpret this number.Createdmeans the container exists but never started.Up 2 hours (Paused)means its processes are frozen.
Filtering and formatting
On a busy host, the full list is hard to scan. The --filter option narrows it, and --format controls the output with a Go template.
docker ps -a --filter status=exited --format 'table {{.Names}}\t{{.Status}}\t{{.Image}}'This lists stopped containers with their names, status text, and images. It is the usual starting point for a failed container. Other useful filters include name=web, ancestor=myimage:latest, health=unhealthy, and exited=137.
docker ps -a --filter exited=137
docker ps -a --filter health=unhealthy
docker ps -a --filter name=web --no-truncUse --no-trunc when the COMMAND column is cut off and you need the full command line. Use -l to show only the most recently created container, and -q to print only IDs, which is handy for scripts.
Name matching with --filter name= is a substring match. name=web also matches web-db and myweb. Anchor it with a regular expression such as name=^web$ when you need exactness.
Reading state with docker inspect
docker ps shows a summary. docker inspect returns the full JSON document that the daemon keeps for the container. It works on stopped containers too, which makes it the main tool for post-failure analysis.
docker inspect webThe output is long. You will rarely read it all. Instead, use --format to pull out the fields you need, or pipe the JSON to jq if you have it installed.
The State object
The State section is the part you need most when something has gone wrong.
| Field | Meaning |
|---|---|
| `Status` | One of `created`, `running`, `paused`, `restarting`, `removing`, `exited`, `dead`. |
| `Running` | `true` while the main process is alive. |
| `Paused` | `true` if the container is frozen. |
| `Restarting` | `true` while Docker waits to restart it. |
| `OOMKilled` | `true` if the kernel's out-of-memory killer stopped the container. |
| `Dead` | `true` if Docker failed to remove the container cleanly. |
| `Pid` | The process ID of the main process on the host. `0` when stopped. |
| `ExitCode` | The exit code of the last run. |
| `Error` | A start error reported by the daemon, for example a missing executable. |
| `StartedAt` | When the last run began. |
| `FinishedAt` | When the last run ended. `0001-01-01T00:00:00Z` means it has not finished. |
| `Health` | Present only if a health check is defined. |
The following command prints the fields that classify most failures.
docker inspect --format 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}} restarts={{.RestartCount}} error={{.State.Error}}' webIt prints the exit code, whether the kernel's OOM killer stopped the container, the restart count, and any daemon-reported start error for the container named web.
Compare StartedAt with FinishedAt. A container that ran for 40 milliseconds probably failed on configuration or a missing file. One that ran for 6 hours and then died probably hit a resource limit or a runtime bug.
docker inspect --format '{{.State.StartedAt}} -> {{.State.FinishedAt}}' webTwo kinds of failure
State.Error and State.ExitCode separate two different problems:
- The daemon could not start the process.
Erroris filled in, for example with anexec: "myapp": executable file not found in $PATHmessage. The exit code is usually 126 or 127. - The process started and then ended.
Erroris empty, and the exit code comes from the application or from a signal.
Knowing which one you have decides where you look next. A start error points to the image, the entrypoint, or the mounts. An exit after a successful start points to the logs.
Configuration: what the container was asked to do
The Config and HostConfig sections hold what you passed to docker run or what came from Compose. These often explain behavior that the Dockerfile alone does not.
docker inspect --format '{{json .Config.Entrypoint}} {{json .Config.Cmd}}' web
docker inspect --format '{{json .Config.Env}}' web
docker inspect --format '{{json .HostConfig.RestartPolicy}}' web
docker inspect --format '{{.HostConfig.Memory}}' webUse these to check the final entrypoint and command after overrides, the environment as the process sees it, the restart policy, and the memory limit. A memory value of 0 means no limit.
Other sections you will use in later chapters are Mounts for volumes and bind mounts, and NetworkSettings for IP addresses and published ports.
docker inspect --format '{{json .Mounts}}' web
docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' webGo template tips
--format uses Go templates, which have a few quirks:
- Use
{{json .Field}}to print a nested object as JSON. Withoutjson, you get Go's internal formatting, which is hard to read. - Field names are case-sensitive.
.State.ExitCodeworks,.state.exitcodedoes not. - Keys that contain dashes or dots need
index, for example{{index .Config.Labels "com.docker.compose.service"}}. - Quote the whole template in single quotes so your shell does not touch the braces.
Inspecting the image versus the container
docker inspect accepts containers, images, volumes, and networks. When a name could match more than one, add --type.
docker inspect --type container web
docker inspect --type image myimage:latestThis matters when a container and an image share a name. It also helps you compare the two: Config.Cmd on the image shows the default, while the same field on the container shows what was actually used.
A first-pass triage session
The following sequence shows how the two commands combine. Suppose a service called web is not responding.
docker ps -a --filter name=web
docker inspect --format 'status={{.State.Status}} exit={{.State.ExitCode}} oom={{.State.OOMKilled}} restarts={{.RestartCount}}' webPossible results and what they suggest:
| Result | Likely direction |
|---|---|
| `status=running`, healthy, port missing in `PORTS` | Check the port publishing and the application's bind address. |
| `status=exited exit=0` | The process finished normally. The command may not be a long-running server. |
| `status=exited exit=1`, runtime under a second | Application error at startup. Read the logs. |
| `status=exited exit=137 oom=true` | Memory limit reached. See the resource chapter. |
| `status=restarting`, high restart count | A crash loop. Read the logs of the previous run. |
| `status=created` | The container never started. Check `State.Error` and the daemon logs. |
The diagram below shows the decision order.
Common errors
- Container not listed at all. You may be talking to a different Docker context or host. Run
docker context lsand checkDOCKER_HOST. If you use Compose, the container name includes the project prefix, so search with--filter name=ordocker compose ps -a. - A container was removed. If it ran with
--rm, Docker deletes it on exit anddocker inspecthas nothing to show. Rerun without--rmwhile debugging. - Reading an old exit code.
State.ExitCodedescribes the last run only. After a restart, an earlier failure's code is overwritten. Combine it withRestartCountandFinishedAt. - Trusting the image tag. A container keeps running the image ID it was created from, even if the tag now points elsewhere. Compare
.Imagein the container withdocker image inspect --format '{{.Id}}' myimage:latest. - Misreading
Up. A container can beUpand still broken.Uponly means the main process is alive.
Exercise
A container named api shows Exited (1) 3 seconds ago in docker ps -a. Write one docker inspect command that prints its exit code, whether it was OOM-killed, and any daemon start error. Then explain how you would tell a start failure from an application failure.
Show answer
Run docker inspect --format 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}} error={{.State.Error}}' api. If error is non-empty, the daemon could not start the process, so look at the entrypoint, command, image, and mounts. If error is empty and the exit code is non-zero, the process started and then failed, so read the logs with docker logs api.