Sub topic 3
Watching the Daemon with `docker events`
Assume you know docker run, Linux processes, namespaces, mounts, and basic shell commands. Use docker events when the current state does not explain a failure. It shows lifecycle changes as the Docker daemon observes them.
Watch events
Filter by container and event type to reduce noise:
docker events --filter container=web --filter event=dieUseful events include create, start, stop, die, restart, health_status, and oom. Event output gives timing and attributes, but logs and inspection explain the cause.
Debug minimal images by sharing namespaces
A production image may have no shell, ps, or network tools. Start a temporary tooling container in the target's process and network namespaces:
docker run --rm -it --pid=container:web --network=container:web nicolaka/netshootFrom this container, inspect processes and test connections with ps, ss, curl, or dig. This does not change the target image.
> callout note > Namespace sharing is a diagnostic tool. It grants visibility into the target's processes and network, so use it only on containers you are authorized to inspect.
Check networking
Test in layers:
- Confirm the application listens on the expected address and port with
ss -lntp. - From the container, test its dependency by service name, such as
curl http://db:5432. - From the host, verify published ports with
docker port web. - Check DNS, routes, and network membership with
docker inspect web.
A service listening only on 127.0.0.1 cannot receive connections through the container network.
Check volumes and permissions
Inspect mounts and compare the container user with the mounted path's owner:
docker inspect --format '{{json .Mounts}}' web
docker exec web id
docker exec web ls -ld /app/dataA bind mount can hide files that were present in the image. A numeric UID mismatch can produce “permission denied” even when the path looks correct.
Post-mortem evidence
Stopped containers remain inspectable. Copy artifacts and compare filesystem changes:
docker cp web:/app/crash.log ./crash.log
docker diff webdocker diff shows changes since image creation. docker commit web debug-snapshot:latest preserves a temporary snapshot for investigation, but it is not a replacement for fixing the Dockerfile or storing data properly.
Compose and the debugging checklist
For Compose projects, inspect the rendered configuration before debugging assumptions:
docker compose ps -a
docker compose logs --tail 50 web
docker compose configFollow this order:
- Capture the event and timestamp.
- Inspect state, exit code, health, mounts, and networks.
- Read logs.
- Check resources and dependencies.
- Enter the container or attach a tooling container.
- Preserve useful files before removal.
- Reproduce the smallest failing case and fix its configuration or image.
Exercise
A container is running but its health check fails, and the image has no shell. What should you do first?
Show answer
Inspect its health output and logs, then attach a temporary tooling container sharing its network namespace to test the health endpoint and dependencies.