Remove Docker containers without losing needed data
Quick answer
docker rm removes a container object and its writable layer. Stopping a container does not do that. Before removal, decide whether its local data is disposable. Removing a container normally leaves the image available for new containers.
Before you run commands
Use an approved disposable Linux-container Docker lab. Confirm the effective context and permissions; use a reviewed, locally preloaded busybox:1.37 image whose digest/platform the lab owner has recorded. This tag is mutable. Do not download an image automatically; --pull=never requires it locally. The new containers use --network none, with no host mounts or published ports; the CLI still connects to the selected engine. Verify the approved image tag still resolves to the recorded image before creating containers. Check docker ps -a for all names used below. If any is occupied, choose fresh names consistently. Run commands one at a time and stop on any unexpected failure; never act on resources you did not create. Expected results are predictions, not measured execution.
How it works
This fixture creates a synthetic file at /tmp/bm-note on the container writable layer. test -f checks whether the file exists, and the shell prints new or existing. The first run creates it; starting the same exited container again sees the file. Remove the container only after this comparison, then create a fresh one using the same image. The fresh container has no file from the previous writable layer. No host paths or volumes are mounted. The approved image must not declare a volume at /tmp or /tmp/bm-note; otherwise the file may live outside the writable layer. Verify the image configuration before this exercise.
docker run --pull=never --network none --name bm02-data busybox:1.37 sh -c 'if test -f /tmp/bm-note; then echo existing; else echo new; touch /tmp/bm-note; fi'docker start -a bm02-datadocker rm bm02-datadocker run --rm --pull=never --network none busybox:1.37 sh -c 'if test -f /tmp/bm-note; then echo existing; else echo new; fi'Expected application messages in order: new, existing, new. The removal command also prints the removed name.
Common mistake
Do not use force removal or broad prune commands for normal cleanup. Stop an active lab container first and inspect the intended target. Volume and bind-mount data have separate lifecycles; do not assume removal safely deletes or backs them up. The -v option to rm can remove associated anonymous volumes and is outside this exercise.
Try it yourself
Explain which operation preserves the file and which discards it. Describe what you would check before removing a real database container (do not perform the removal).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Remove Docker containers without losing needed data — knowledge check
Apply the lifecycle concepts and interpret the lab behavior.
Which operation discards the container writable layer?
What should the second start of the fixture print?
Before deleting a real data-bearing container, what is most important?