Sub topic 14
Post-mortem with docker cp, docker diff, and docker commit
This chapter assumes you can read container state with docker inspect, read logs, and interpret exit codes. It covers what to do when a container has already stopped and you need to see what it left behind.
Why a stopped container is still useful
A container that has exited keeps its writable layer until you remove it with docker rm. That layer holds every file the process created, changed, or deleted. Crash logs, core dumps, partial output, and temporary files are all still there.
Do not run docker rm or docker run --rm on a container you still need to examine. With --rm, the container and its writable layer are deleted as soon as it exits, so there is nothing to inspect.
See what changed with docker diff
docker diff compares the container's filesystem with its image. It works on stopped containers too.
docker diff webEach line starts with a letter:
| Letter | Meaning |
|---|---|
| A | File or directory was added |
| C | File or directory was changed |
| D | File or directory was deleted |
A typical result shows where the application was writing when it failed:
C /app
A /app/crash.log
A /tmp/upload-8841.part
D /app/config.lockHere the process wrote /app/crash.log, left a half-written upload in /tmp, and removed a lock file. These are leads to follow next.
docker diff does not list files in volumes or bind mounts. Those live outside the container's writable layer.
Copy files out with docker cp
docker cp copies files between a container and the host. It works whether the container is running or stopped.
docker cp web:/app/crash.log ./crash.log
docker cp web:/tmp/. ./web-tmp/The /. ending on the second command copies the directory contents instead of the directory itself. Create ./web-tmp first. Copying out is safe and leaves the container unchanged.
Keep a copy of the whole state with docker commit
docker commit turns a container's current filesystem into a new image. You can then start a shell from that image and explore it, even after the original container is removed.
docker commit web web-postmortem:2026-12-04
docker run --rm -it --entrypoint sh web-postmortem:2026-12-04The second command starts a fresh container with the files as they were at the crash, but with a shell instead of the failing entrypoint.
Keep these limits in mind:
- Only the filesystem is saved. Processes, memory, and network state are not.
- Volume and bind mount contents are not included.
- The new image may contain secrets that were written to files. Do not push it to a shared registry.
- Delete it with
docker rmiwhen you finish.
A short post-mortem sequence
- Run
docker inspectto confirm the exit code andOOMKilledvalue. - Run
docker diffto find files the process touched. - Use
docker cpfor the specific files you want. - Use
docker commitonly if you need to explore the filesystem interactively. - Remove the container and any snapshot image when you are done.
Exercise
A container named api exited with code 1 and was not started with --rm. docker logs shows nothing useful. How do you find out whether it wrote a crash file, and how do you get that file onto your host?
Show answer
Run docker diff api to list added (A) and changed (C) files, and look for something like a crash log or core file. Then run docker cp api:/path/to/file ./file to copy it to the host. If the file is in a volume, docker diff will not show it, so check the mounted path instead.
Exercise
Why should you avoid pushing an image made with docker commit to a shared registry?
Show answer
It captures the container's whole writable layer, which can include secrets, tokens, or customer data that the application wrote to disk. It also isn't built from a Dockerfile, so others cannot reproduce it.