Sub topic 9
Removing Containers Automatically with --rm
This chapter assumes you can start containers with docker run, list them with docker ps -a, and remove one by hand with docker rm. You should also know that stopping a container leaves it on disk in the exited state.
Why stopped containers pile up
A container that exits is not deleted. Docker keeps its writable layer, its logs, and its exit status so you can inspect them later. That is useful for a service you want to debug. It is clutter for a one-off command.
Run a few quick commands and look at the result:
docker run alpine echo "one"
docker run alpine echo "two"
docker ps -a --filter "status=exited"Each run leaves an exited container with a random name. Scripts, CI jobs, and quick experiments can create hundreds of these.
The --rm flag
The --rm flag tells the Docker daemon to remove the container when it exits. It is an option of docker run.
docker run --rm alpine echo "hello"
docker ps -a --filter "status=exited"The first command prints hello. The second shows no container left behind. The container went through created, running, and exited, and then Docker removed it.
What the daemon does on exit
Docker handles --rm in the daemon, not in your terminal. When the main process of the container ends, the daemon removes the container. This has a few consequences:
- It happens whatever the exit code is. A failed command (non-zero exit) is removed too.
- It also happens after
docker stopordocker kill, because those end the main process. - It also happens in detached mode (
-d). The container disappears when its process finishes, even though your terminal never attached to it.
Combining --rm with other options
You can use --rm with the options you already know:
docker run -d --rm --name web nginx:alpine
docker ps
docker stop web
docker ps -aThe container named web runs in the background. After docker stop web, it is gone from docker ps -a. You cannot run docker start web afterwards, because there is nothing left to start. You would need a new docker run.
The name is free again after removal, so you can reuse --name web in the next run. Without --rm, a second docker run --name web fails while the stopped container still holds the name.
When to use it
| Situation | Use --rm? | Reason |
|---|---|---|
| One-off command or script step | Yes | Nothing to inspect later |
| Quick test of an image | Yes | Avoids leftover containers |
| CI job that collects output elsewhere | Yes | Logs go to the CI system |
| Long-running service you may debug | Usually no | You may need `docker logs` after a crash |
| Container you plan to stop and start again | No | Removal makes `docker start` impossible |
What --rm does to data
Removal by --rm follows the same rules as docker rm -v. The writable layer is deleted, so anything the container wrote inside its own filesystem is lost. Anonymous volumes attached to the container are deleted too. Named volumes and bind mounts are kept.
docker run --rm -v appdata:/data alpine sh -c 'echo saved > /data/note.txt'
docker run --rm -v appdata:/data alpine cat /data/note.txtThe second container prints saved. Both containers are gone, but the named volume appdata survives and holds the file. If you had used -v /data with no name, the volume would be anonymous and Docker would delete it with the container.
Common mistakes
- Using
--rmand then tryingdocker start. The container no longer exists. Run it again withdocker run. - Expecting logs after exit.
docker logsneeds the container to exist. Send output to a log driver, a file on a mounted volume, or your CI system if you need it afterwards. - Storing results in the container filesystem. Write them to a named volume or bind mount instead.
- Confusing
--rmwithdocker container prune. The flag acts per container, at exit. Prune cleans up containers that are already stopped. A later chapter covers it.
Exercise
You run docker run -d --rm --name job alpine sleep 60. After the container finishes, you run docker start job. What happens, and why?
Show answer
The command fails with an error saying there is no such container. The --rm flag made the daemon remove the container when sleep 60 exited, so the name job no longer refers to anything. You must create a new container with docker run.
Exercise
A container started with --rm writes a report to /out/report.txt. How do you keep the report after the container exits?
Show answer
Mount a named volume or a bind mount at /out, for example -v reports:/out. The container is removed, but named volumes and bind mounts are not. Data in the container's own writable layer, and in anonymous volumes, is deleted.