Lesson 19 of 19 · 18m
Reading a Dead Container: docker diff, nsenter, and Postmortem Evidence
A stopped container keeps its writable layer, logs, and inspect state — docker diff, nsenter, and export read it before prune destroys it.
The container exited. Nothing was lost — the writable layer is still on disk, the logs are still readable, the inspect state still holds the exit. A stopped container is an evidence locker, and it stays one until docker rm or docker system prune empties it.
Read the state before touching anything
docker inspect dead-container gives you State: ExitCode, Error, StartedAt, FinishedAt, OOMKilled. Exit 137 plus OOMKilled: true is the cgroup limit, not your code. Exit 143 is SIGTERM — something asked politely. docker logs works on dead containers; docker events --since prints the die/oom/kill sequence for the window, which is how you learn who stopped it. docker top is moot once it's dead, but on a live one it lists host-side PIDs — the container's PID namespace makes the same process look different inside and out.
docker diff answers "what did it change?"
docker diff <container> diffs the writable layer against the image: A for added files, C for changed, D for deleted. This is the incident triage command. A web container suddenly carrying /tmp/.x/shell.php or a rewritten nginx.conf is an A and a C away from a guess. The reverse is just as informative — a compromised container with an empty diff means your read-only rootfs and tmpfs mounts did their job; there was nowhere to write.
Getting inside a live one
docker exec -it <ctr> sh joins a running container's namespaces with the container's own capability set — no extra privileges, no daemon socket mount needed. When the container lacks the tool you need — which it should, if the image was built worth deploying — nsenter joins individual namespaces from the host:
PID=$(docker inspect -f '{{.State.Pid}}' web)
sudo nsenter -t $PID -n tcpdump -i eth0Host tcpdump runs inside the container's network namespace without installing anything in it. The same trick with -m joins its mount view. For a full debugging shell inside the network namespace, docker run -it --rm --net container:web nicolaka/netshoot gives you tcpdump, mtr, drill, and friends pointed at the target's traffic — the image built for exactly this.
Preserve before you prune
Evidence capture on a stopped container:
docker cp web:/var/log/app ./evidence/— works whether the container runs or not.docker export web > web-fs.tar— the flattened merged filesystem, layers collapsed.docker commit web forensic/web:incident— freezes the writable layer as a new image, diffable later withdocker diff/dive.docker pause web— cgroup freezer, not a signal: the process stops mid-incident without exiting, for when you need it still but suspended.
Then — and only then — docker rm. Every docker system prune -a run is evidence destruction with a progress bar; run it knowing that.
Try this
docker run -d --name victim nginx:alpine, docker exec victim sh -c 'echo injected > /usr/share/nginx/html/x.html', docker stop victim, docker diff victim. One A — /usr/share/nginx/html/x.html — and you have read the attack the way the daemon saw it. Now docker export it before you delete it, because next time it won't be a drill.
Field notes
docker diffonly sees the writable layer — bind-mounted paths live outside it and won't appear; check mounts separately indocker inspect.nsentertargets a PID, so it works on dead hosts' live containers, not on stopped ones —State.Pidreads 0 after exit.docker commitof a compromised container preserves its full filesystem for offline analysis; treat the resulting image as evidence, quarantined accordingly.docker stats --no-streamon a live suspect shows live CPU/memory before you freeze or kill it — cheap data, gone after.--rmis convenient in dev and hostile to forensics: it deletes the evidence locker on exit, every time.docker eventssurvives the container too — thedieevent is timestamped evidence even afterrm.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
PostgreSQL 19 Is Out. REPACK Kept My Writes Running. The Docker Upgrade Will Stop Your App.
PostgreSQL 19 shipped on November 19 with REPACK (CONCURRENTLY), which removes table bloat without blocking writes. I measured it against VACUUM FULL, ran the upgrade from 18, and checked what breaks for self-hosters: Docker tags that refuse to start, a config setting that stops the server, and...
Self-Hosting Prometheus and Grafana: Infrastructure Monitoring That Actually Works
Build a production-grade monitoring stack with Prometheus and Grafana from scratch. Covers architecture, Docker Compose deployment, Node Exporter, alerting with Alertmanager, PromQL from basics to advanced, pre-built dashboards, and everything SudoSecurity uses to monitor its own infrastructure.
What is Kubernetes? Container Orchestration at Scale
Kubernetes runs your Docker containers across a cluster of machines, handles scheduling, health checking, rolling deployments, auto-scaling, and self-healing. When your application outgrows a single server, Kubernetes is how it runs on ten.
What is Docker? Containers, Images, and Why Every Developer Needs to Know This
Docker packages your application and everything it needs to run into a container that behaves identically on your laptop, your teammate's machine, and the production server. "It works on my machine" stops being a problem. Here is how it actually works and what production usage looks like.