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 understand that a container is a process with a state. Before you look at logs or exit codes, you need to find the right container and read what Docker already knows about it.
Finding the container with docker ps
docker ps lists running containers only. A container that crashed is not shown, which is a common source of confusion. Add -a to include stopped ones.
The default output is wide and hard to scan. The first command below filters to exited containers and prints only the columns you need.
docker ps -a --filter status=exited --format 'table {{.Names}}\t{{.Status}}\t{{.Image}}'The Status column carries useful text, such as Exited (137) 2 minutes ago or Up 5 seconds (health: starting). The number in parentheses is the exit code. The next chapters explain how to read it.
Other filters you will use often:
--filter status=restartingfinds containers caught in a restart loop.--filter name=webmatches names that containweb.--filter health=unhealthyfinds containers that fail their health check.
Reading state with docker inspect
docker inspect prints everything the daemon stores about a container as JSON. The output is long, so you usually extract single fields with a Go template passed to --format.
docker inspect --format 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}} restarts={{.RestartCount}} error={{.State.Error}}' webThis prints one line for the container named web. Each field answers a specific question:
| Field | What it tells you |
|---|---|
| `.State.Status` | Current lifecycle state, such as `running`, `exited`, or `restarting` |
| `.State.ExitCode` | The exit code of the main process from its last run |
| `.State.OOMKilled` | `true` if the kernel's out-of-memory killer stopped the container |
| `.RestartCount` | How many times Docker's restart policy has restarted it |
| `.State.Error` | A start error reported by the daemon, such as a missing executable |
| `.State.StartedAt` and `.State.FinishedAt` | When the last run began and ended |
Comparing StartedAt and FinishedAt shows how long the process lived. A container that runs for one second and exits points to a startup problem. One that runs for hours and then exits points to a runtime failure.
Reading configuration
inspect also shows how the container was configured, which often explains the failure. These fields are the most useful:
.Config.Cmdand.Config.Entrypointshow the command that actually ran..Config.Envlists environment variables..HostConfig.RestartPolicyshows the restart policy..Mountsshows volumes and bind mounts.
Use the json function to print a nested object readably:
docker inspect --format '{{json .Config.Entrypoint}} {{json .Config.Cmd}}' webWhich object does inspect return?
docker inspect web looks for a container, image, volume, or network with that name. If an image and a container share a name, you may get the wrong one. Use docker container inspect web to be explicit.
A first-look sequence
The diagram shows the order to follow when a container is misbehaving.
Exercise
A container named api shows Exited (1) 3 seconds ago in docker ps -a. Which docker inspect command prints its exit code and whether it was killed for running out of memory?
Show answer
docker inspect --format 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}}' api. The 3-second lifetime also suggests a startup failure, so check .State.Error and the logs next.
Exercise
Why might docker ps -a not show a container that crashed a minute ago?
Show answer
The container was probably started with --rm, so Docker removed it when it exited. Run it again without --rm to keep it for inspection.