Auto-Remove Containers
Part of the Introduction to Docker section of Coddy's Terminal journey — lesson 10 of 40.
Every time you run a container, Docker keeps a record of it even after it exits. For one-off commands you usually do not want that clutter. The --rm flag tells Docker to delete the container automatically once it finishes:
docker run --rm alpine echo "clean up after me"The output is exactly the same as without the flag, but no leftover container is kept around. Flags like --rm go between run and the image name:
docker run [FLAGS] IMAGE COMMANDChallenge
EasyRun a container from alpine that prints done with echo, and have Docker remove the container automatically afterward with --rm.
Cheat sheet
Use --rm to automatically remove a container after it exits (no leftover records):
docker run --rm alpine echo "clean up after me"Flags go between run and the image name:
docker run [FLAGS] IMAGE COMMANDTry it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction to Docker
3Running Containers
Running A CommandReal ExecutionDifferent DistrosAuto-Remove ContainersRecap - Container Runner