Skip to content

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 eth0

Host 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 with docker 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 diff only sees the writable layer — bind-mounted paths live outside it and won't appear; check mounts separately in docker inspect.
  • nsenter targets a PID, so it works on dead hosts' live containers, not on stopped ones — State.Pid reads 0 after exit.
  • docker commit of a compromised container preserves its full filesystem for offline analysis; treat the resulting image as evidence, quarantined accordingly.
  • docker stats --no-stream on a live suspect shows live CPU/memory before you freeze or kill it — cheap data, gone after.
  • --rm is convenient in dev and hostile to forensics: it deletes the evidence locker on exit, every time.
  • docker events survives the container too — the die event is timestamped evidence even after rm.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.