Sub topic 14
Post-mortem with docker cp, docker diff, and docker commit
A stopped container is not gone. Docker keeps its writable layer, its metadata, and its logs until you remove it with docker rm. This chapter shows how to examine that leftover state to learn what a container did before it died.
This chapter assumes you can read container state with docker ps and docker inspect, and that you know how to read exit codes. It also assumes you know the basic image and layer model: an image is a stack of read-only layers, and each container adds one writable layer on top.
Why the writable layer matters
Logs tell you what the process chose to print. The filesystem tells you what it actually did. A crashed application may have written a core dump, a partial config, a lock file, a temp file that filled the disk, or a crash log that never reached stdout.
All of those changes sit in the container's writable layer. Three commands let you work with it:
| Command | What it does | Works on stopped containers |
|---|---|---|
| `docker diff` | Lists files added, changed, or deleted compared with the image | Yes |
| `docker cp` | Copies files between the container and the host | Yes |
| `docker commit` | Saves the container's filesystem as a new image | Yes |
The first thing to remember is the cleanup trap. If you started the container with --rm, Docker deletes it as soon as it exits, and the evidence goes with it. When you are debugging a crash, run the container without --rm. Otherwise the post-mortem is not possible.
The post-mortem flow
Finding what changed with docker diff
docker diff compares the container's filesystem with its image and prints one line per change. Each line starts with a letter:
Ameans the path was added.Cmeans the path was changed. For a directory, this means something inside it changed.Dmeans the path was deleted.
docker diff webA typical result for a crashed service might look like this:
C /app
A /app/crash.log
C /tmp
A /tmp/upload-8841.part
C /var/log
A /var/log/app.logRead the output as a list of leads. A crash log or a core file points to the cause directly. A very large temp file points to a full disk. A deleted file under /etc or /app can point to a startup script that removed something it needed.
docker diff has limits you should know:
- It does not show file contents or sizes. You need
docker cpor a shell for that. - It does not show changes made inside mounted volumes or bind mounts. Those paths live outside the writable layer. If your application writes to a volume, look at the volume on the host or from another container.
- It does not show changes to files in
tmpfsmounts, because those exist only in memory and vanish when the container stops. - Noise is common. Directories such as
/tmpand/var/logare markedCwhenever anything inside them changes.
Copying files out with docker cp
docker cp copies files between a container and the host. It works on stopped containers, which is what makes it useful here.
docker cp web:/app/crash.log ./crash.logThe source is container:path and the destination is a host path. You can reverse the order to copy a file into a container. You can also copy a whole directory:
mkdir -p ./web-evidence
docker cp web:/var/log/. ./web-evidence/The trailing /. copies the contents of the directory instead of the directory itself. This matters when the destination already exists, because it controls whether you get ./web-evidence/log/ or the files directly in ./web-evidence/.
Some behavior to expect:
- File ownership on the host is set to the user running the command, unless you use
-a(archive mode), which keeps the original UID and GID. - Symbolic links are copied as links by default. Use
-Lto follow them. docker cpdoes not copy data from volumes or bind mounts mounted at the source path. It reads the container's own filesystem view. In practice, if you copy a path that is a mount point, you get the mounted data, not the layer's data. When in doubt, checkdocker inspectfor theMountslist.- It cannot copy from special filesystems such as
/proc,/sys,/dev, or tmpfs mounts.
A useful trick is to copy files you want to compare against the image. For example, copy the config the container saw at crash time, then diff it against the one in your repository:
docker cp web:/etc/app/config.yaml ./config.crashed.yaml
diff ./config.crashed.yaml ./config/config.yamlIf the files differ, an entrypoint script or an environment-driven template changed the file at startup. That is often the bug.
Saving the state with docker commit
Sometimes diff and cp are not enough. You want a shell in the exact filesystem the container had when it died. The original container cannot give you one, because it is stopped and its entrypoint fails. docker commit solves this by turning the container's current filesystem into a new image.
docker commit web web-debug:crash1
docker run --rm -it --entrypoint sh web-debug:crash1The first command creates an image named web-debug with tag crash1. The second starts a fresh container from it with a shell in place of the usual entrypoint. You are now looking at the files exactly as the failed container left them.
Keep these points in mind:
docker commitcaptures the filesystem only. Running processes, memory, and network state are not saved.- The new image keeps the original image's
ENTRYPOINT,CMD,ENV, and exposed ports unless you override them. That is why you still need--entrypoint sh. - Volumes and bind mounts are not included in the commit. Mount them again with
-vif the failure depends on them. - The commit includes everything in the writable layer, including secrets or customer data that the application wrote. Treat the debug image as sensitive. Do not push it to a shared registry, and delete it when you are done with
docker rmi web-debug:crash1. - Commit is a debugging tool. It is not a build method. Images made this way are not reproducible and have no record of how they were built. Fix the cause in the Dockerfile instead.
By default, docker commit pauses the container while it commits. Use --pause=false if you must avoid that on a running container. On a stopped container the pause has no effect.
A worked example
A service named web exits a few seconds after start. The logs show only starting, then nothing. The exit code is 1.
- List what changed:
docker diff webThe output includes A /app/data/index.lock and C /app/data.
- Copy the lock file out to look at it:
docker cp web:/app/data/index.lock ./index.lock
cat ./index.lockThe file holds a process ID, 1. This indicates that a previous run was killed before it could clean up. The new run found the lock, treated it as a live owner, and exited without logging.
- Confirm in a shell. Commit and open the state:
docker commit web web-debug:lock
docker run --rm -it --entrypoint sh web-debug:lock
ls -l /app/data- Fix the application so it checks whether the lock owner is alive, or store the data directory on a volume with a clean-up step. Then remove the evidence:
docker rmi web-debug:lock
docker rm webNotice that logs alone would not have solved this. The filesystem showed the lock file, which the application never mentioned.
Common mistakes
- Starting with
--rmand losing the container. Drop--rmwhile debugging. - Running
docker compose upagain, which recreates the container. Compose may replace the failed container with a new one and discard the old writable layer. Copy files out first, or usedocker compose up --no-recreate. - Expecting
docker diffto show volume changes. It only reports the writable layer. - Forgetting that
docker cphas no wildcard support. Copy a directory and filter on the host. - Leaving debug images around. They may contain sensitive data and use disk space.
Exercise
A container named api exited with code 1 and logged nothing. You run docker diff api and see A /var/lib/app/state.db and C /var/lib/app, but your Compose file mounts a named volume at /var/lib/app. Why should you doubt this output, and what should you check?
Show answer
docker diff only reports the writable layer, and a volume mounted at /var/lib/app is stored outside it. Changes in a mounted path would not appear, so this output suggests the mount is not in effect for this container, or the path differs from what you think. Run docker inspect api and check the Mounts section to see which paths are mounted and where the data lives on the host. Then examine the volume directly, for example by mounting it in a throwaway container.
Exercise
You need an interactive shell in the filesystem of a stopped container web whose entrypoint crashes immediately. Give the two commands and name one thing the result does not preserve.
Show answer
docker commit web web-debug:crash1 followed by docker run --rm -it --entrypoint sh web-debug:crash1. The result does not preserve running processes, memory, or the contents of volumes and bind mounts.