Sub topic 1
Container States: Created, Running, Restarting, Paused, Exited, and Dead
Prerequisites
This chapter assumes basic Docker commands, Linux shell commands, processes, signals, and Dockerfile ENTRYPOINT and CMD.
Understanding Container States
A Docker container is a process with configuration and filesystem layers. Its state describes what Docker currently knows about that process.
| State | Meaning |
|---|---|
| `created` | Configuration exists, but the container has not started |
| `running` | The main process is running |
| `restarting` | The main process stopped and Docker is trying to start it again |
| `paused` | Processes are temporarily frozen |
| `exited` | The main process stopped normally or with an error |
| `dead` | Docker could not cleanly remove or complete the container |
The most useful first distinction is running versus exited. A container runs only while its main process runs. A shell or server that finishes causes the container to exit.
Finding and Inspecting a Container
docker ps lists running containers. Add -a to include stopped containers. Use a name or ID with docker inspect for detailed state, configuration, mounts, and networking.
docker ps -a --filter status=exited --format 'table {{.Names}}\t{{.Status}}\t{{.Image}}'
docker inspect --format 'status={{.State.Status}} exit={{.State.ExitCode}} error={{.State.Error}}' webThe displayed status is a symptom, not a diagnosis. Inspect the exit code, error text, restart count, and timestamps together.
Reading Container Logs
Logs normally contain the main process's standard output and standard error. Read recent output before changing the container.
docker logs --timestamps --tail 100 web
docker logs --follow --since 5m webIf logs are empty, the process may fail before logging, write to a file, or use a logging configuration that sends output elsewhere.
Interpreting Exit Codes and Signals
The exit code is usually in .State.ExitCode.
| Code | Common interpretation |
|---|---|
| `0` | Successful completion |
| `1` | General application error |
| `126` | Command found but not executable |
| `127` | Command not found |
| `137` | Usually `SIGKILL` (9), often an out-of-memory kill |
| `139` | Usually `SIGSEGV` (11), a segmentation fault |
| `143` | Usually `SIGTERM` (15), a requested graceful stop |
These meanings are conventions. Confirm them with logs, OOMKilled, and the command configuration. A restart policy can turn an exit into restarting, so inspect the container instead of relying only on docker ps.
Exercise
A container shows exited (127). What should you investigate first?
Show answer
Check the configured ENTRYPOINT or CMD and logs. Exit code 127 commonly means the requested command was not found.