Sub topic 10
Watching the Daemon with docker events
This chapter covers docker events, which streams what the Docker daemon does as it does it. It assumes you can already list and inspect containers, read exit codes, and know what a restart policy does. Earlier chapters showed you the state a container is in now. This one shows you the sequence of things that led there.
What docker events shows
The Docker daemon emits an event whenever something changes: a container is created, started, killed, or stopped, an image is pulled, a network is connected, a volume is mounted. docker events connects to the daemon and prints those events as they occur.
Logs come from your application. docker inspect gives a snapshot of one container. Events are the daemon's own record of actions on the container from outside, which makes them the right tool when the application logs are empty or misleading, or when you need to know who or what stopped a container.
Run it in one terminal and start a container in another:
docker eventsdocker run --rm --name demo alpine echo helloThe first terminal prints lines like these (timestamps shortened, IDs truncated):
2026-12-04T11:30:01 container create 3f1c... (image=alpine, name=demo)
2026-12-04T11:30:01 container attach 3f1c... (image=alpine, name=demo)
2026-12-04T11:30:01 network connect 8a2e... (container=3f1c..., name=bridge, type=bridge)
2026-12-04T11:30:02 container start 3f1c... (image=alpine, name=demo)
2026-12-04T11:30:02 container die 3f1c... (exitCode=0, image=alpine, name=demo)
2026-12-04T11:30:02 network disconnect 8a2e... (container=3f1c..., name=bridge, type=bridge)
2026-12-04T11:30:02 container destroy 3f1c... (image=alpine, name=demo)Without a filter, the stream includes every event type. On a busy host it is noisy, so you will nearly always filter.
Event types and the ones that matter for debugging
Each event has a type (container, image, network, volume, daemon, and others) and an action. For container failures, a small set of actions carries most of the information.
| Action | Meaning | What to look for |
|---|---|---|
| `create` | The container was created from an image. | Confirms Compose or your script reached this step. |
| `start` | The main process was started. | A `start` with no later `die` means it is still running. |
| `die` | The main process exited. | The `exitCode` attribute gives the exit code. |
| `kill` | A signal was sent to the container. | The `signal` attribute shows which one (for example 9 or 15). |
| `stop` | A stop was requested and completed. | Follows a `kill` with signal 15 from `docker stop`. |
| `oom` | The kernel's OOM killer acted on the container. | Memory limit exceeded. Appears before `die` with exit code 137. |
| `health_status` | A health check changed status. | The action reads like `health_status: unhealthy`. |
| `restart` | The restart policy restarted the container. | Repeated `restart` events indicate a crash loop. |
| `destroy` | The container was removed. | Explains why `docker inspect` no longer finds it. |
The die event is the most useful one. Its exitCode attribute is the same number docker inspect reports, but the event also carries a timestamp, so you can compare it with the events around it.
Filtering the stream
The --filter flag narrows the output. You can repeat it. Different filter keys are combined with AND. Repeated values for the same key are combined with OR.
docker events --filter container=web --filter event=dieThat shows only die events for the container named web. Other useful filter keys are type, image, label, and event.
docker events --filter type=container --filter event=oom --filter event=dieThis one matches container events whose action is either oom or die.
You can also change the output format. A Go template makes the stream easy to scan or pipe into other tools:
docker events --filter type=container \
--format '{{.Time}} {{.Actor.Attributes.name}} {{.Action}} exit={{index .Actor.Attributes "exitCode"}}'For machine processing, use --format '{{json .}}', which prints one JSON object per event.
Looking at the past with --since and --until
By default docker events only shows new events. It does not replay history from before you started it. To see recent events, pass --since, which accepts a duration such as 30m, a timestamp, or a Unix time. Add --until to stop at a given point. When --until is in the past, the command prints the matching events and exits instead of waiting.
docker events --since 30m --until 0s --filter container=webUsing --until 0s (now) makes the command return after printing history, which is what you want when reading the log of a past failure.
Reading a crash loop from events
A container with restart: always that keeps failing looks healthy to a casual docker ps, because its status flips between Up and Restarting. Events show the loop plainly:
11:40:00 container start web (exitCode not set)
11:40:01 container die web (exitCode=1)
11:40:02 container start web
11:40:03 container die web (exitCode=1)
11:40:05 container start web
11:40:06 container die web (exitCode=1)Three things stand out. The process lives about one second each time, so it fails during startup rather than under load. The exit code is always 1, which points to an application error instead of a signal. The gaps between a die and the next start grow, which is the restart policy's backoff delay. With that you know to read the logs from the first second of a run (docker logs --timestamps) and not to look at memory or the host.
Compare that with an out-of-memory pattern:
12:05:10 container oom web (image=myimage:latest, name=web)
12:05:10 container die web (exitCode=137, oom=true)
12:05:11 container start webThe oom event appears first, followed by exit code 137, and the container runs for a long and variable time between failures. That pattern points at memory growth, which you then confirm with docker stats and OOMKilled.
Telling who stopped the container
A die event alone does not say why the process exited. The events just before it do.
- A
killevent withsignal=15followed bydiewith exit code 143 (or 137 after the stop timeout) means someone or something randocker stop, or an orchestrator did. - A
killevent withsignal=9and adiewith exit code 137, with nooomevent, means a manualdocker killor another tool sent SIGKILL. - A
diewith no precedingkilloroommeans the process exited by itself. The exit code is then the application's own.
This distinction saves time. If a container exited with 137 and you assume memory, you will chase a memory problem that never existed. The events tell you whether anything outside the container acted on it.
Combining events with the other tools
Events work best next to the commands from earlier chapters. A practical routine for an intermittent failure is:
- Start a recorder in the background so you do not miss the next occurrence.
- Reproduce or wait for the failure.
- Correlate the timestamps in the event stream with
docker logs --timestampsoutput.
docker events --filter container=web --format '{{json .}}' >> web-events.jsonl &When the failure happens, line up the die timestamp with the last application log lines. If the log ends a moment before die, the application stopped itself. If the log is cut off in the middle of normal work, something external terminated it.
Common mistakes
- Starting
docker eventsafter the failure and expecting history. Use--sinceor run it before you reproduce. - Filtering by container name when the container is recreated with a new name or ID. Compose names can change between runs, so a label filter such as
--filter label=com.docker.compose.service=webis more stable. - Forgetting
--until. Without it, the command keeps waiting for new events and appears to hang. - Treating the
killevent as proof of a manual kill. Docker itself sends signals during normaldocker stopand during restarts.
Exercise
A container named api keeps coming back up. You run docker events --since 10m --until 0s --filter container=api and see repeated die events with exitCode=137, each preceded by a kill event with signal=9 and no oom event. Is memory the most likely cause, and what would you check next?
Show answer
Memory is unlikely. An OOM kill by the kernel produces an oom event, and none appears. The kill event with signal 9 means something sent SIGKILL through Docker, such as a docker kill command, a script, a monitoring tool, or an orchestrator that stops the container after a failed check or a timeout. Next, look for what issues those commands: check cron jobs and scripts on the host, any tool with access to the Docker socket, and the health check configuration (docker inspect --format '{{json .State.Health}}' api). Also run docker inspect --format 'oom={{.State.OOMKilled}}' api to confirm the kernel did not act.
Exercise
You want to capture every die and oom event for any container carrying the label app=checkout over the next hour, writing one JSON object per line to a file. Which command does this?
Show answer
docker events --filter type=container --filter label=app=checkout --filter event=die --filter event=oom --format '{{json .}}' >> checkout-events.jsonl run in the background, for example with &. The type, label, and event filters combine with AND, while the two event values combine with OR. Add --until 1h to make it stop on its own after an hour.