Automatically Remove Containers
Use the --rm option to clean up short-lived containers automatically after they stop.
During Docker practice, stopped containers can pile up quickly.
Every time you run a container, Docker creates a container object. Even after the process stops, the stopped container may remain on your system so you can inspect logs or restart it.
That is useful sometimes, but for quick tests it creates clutter.
See stopped containers
Run:
docker ps -aThis shows both running and stopped containers.
If you practice often, you may see many old containers from previous commands.
Remove containers manually
You can remove a stopped container:
docker rm container-idOr by name:
docker rm container-nameIf the container is still running, stop it first:
docker stop container-name
docker rm container-nameUse --rm for temporary containers
The --rm option tells Docker to automatically remove the container when it stops.
docker run --rm hello-worldFor an interactive container:
docker run --rm -it ubuntu bashWhen you exit the shell, Docker removes the container automatically.
When --rm is useful
Use --rm for:
- Quick command tests
- Temporary shells
- Short experiments
- One-time utility containers
- Learning examples where you do not need logs later
It keeps your Docker environment clean.
When not to use --rm
Do not use --rm when you want to inspect the stopped container later.
For example, if an application crashes and you want to check logs, keeping the stopped container can be useful:
docker logs container-idIf you used --rm, the container is removed after it stops, so there is nothing left to inspect.
Clean up old stopped containers
To remove all stopped containers:
docker container pruneDocker will ask for confirmation before deleting them.
Simple practice rule
Use --rm when the container is temporary.
Avoid --rm when you are debugging.
Containers are cheap to create, so they are easy to forget. Build the habit of cleaning up while learning Docker.
Written By: Shiva Srivastava
How is this guide?
Last updated on
