Sub topic 10
Watching the daemon with docker events
This chapter assumes you can list and inspect containers, read exit codes, and know what a restart policy does. Logs and docker inspect show you the state of one container now. docker events shows you what the Docker daemon did over time, which helps when a container disappears, restarts, or flaps faster than you can look at it.
What docker events reports
The daemon publishes an event for each action on objects such as containers, images, volumes, and networks. Each event has a type, an action, and attributes such as the container name and image. docker events streams these as they happen. It does not replay old history by default.
For debugging containers, these actions matter most:
| Action | Meaning |
|---|---|
| `create` | The container was created. |
| `start` | The main process started. |
| `die` | The main process exited. The event carries `exitCode`. |
| `oom` | The kernel's OOM killer acted on a process in the container. |
| `kill` | A signal was sent to the container. |
| `stop` | A stop was requested and completed. |
| `restart` | The container was restarted by a command. |
| `health_status` | The health check result changed. |
| `destroy` | The container was removed. |
Filtering the stream
An unfiltered stream is noisy. Filter by container and by event, as in the spine's example:
docker events --filter container=web --filter event=dieFilters of different kinds combine with AND. Repeating the same filter, such as two event= values, combines them with OR. To watch the whole lifecycle of one container:
docker events --filter container=web --filter event=start --filter event=die --filter event=oomLooking at past events
Use --since and --until to read events the daemon still remembers. The daemon keeps only a limited recent history, so do this soon after the incident.
docker events --since 30m --until 0s --filter container=webWithout --until, the command keeps streaming after it prints the past events.
Reading a crash loop
Suppose web has restart: on-failure. Run the filtered stream and you may see this sequence repeat:
container start ... (name=web)
container die ... (exitCode=1, name=web)
container start ... (name=web)
container die ... (exitCode=1, name=web)The timestamps tell you how long each run lasts. A die a second after each start points to a startup failure. Pair it with docker logs for the cause. A die with exitCode=137 preceded by an oom event confirms a memory kill. A kill followed by die shows that something sent a signal, such as docker stop, an orchestrator, or a script.
Seeing who acted
A container that vanishes sometimes was removed by a person or a script, not by a crash. If you see kill, stop, or destroy with no die caused by an application error, look for automation: cleanup jobs, CI runners, or docker compose down. Events show that an action occurred but not who ran it, so correlate the timestamps with your own tooling.
Formatting for scripts
Use --format to produce compact lines:
docker events --filter event=die --format '{{.Time}} {{.Actor.Attributes.name}} exit={{.Actor.Attributes.exitCode}}'Exercise
A container named api keeps restarting. You run docker events --filter container=api and see oom followed by die on every cycle. What does this tell you, and what is the next step?
Show answer
The kernel killed a process in the container for exceeding its memory limit, and the container then exited, typically with code 137. Confirm with docker inspect --format '{{.State.OOMKilled}}' api and docker stats, then raise the memory limit or reduce the application's memory use.