Nono.MA

Docker run delete on exit

LAST UPDATED FEBRUARY 6, 2024

For a Docker container started with the docker run command to be removed on exit, you need to use the --rm flag.

docker run -it --rm ubuntu /bin/bash

If you forgot to run your container with --rm, you can list running containers and close them.

List Docker containers

# The -a flag shows exited containers as well.
docker container ls -a
# CONTAINER ID  IMAGE   COMMAND      CREATED      STATUS      NAMES
# c88ccae63e88  ubuntu  "/bin/bash"  20 secs ago  Exited (0)  pop_low

Delete a stopped container by ID.

docker rm c88ccae63e88 # or docker container rm c88ccae63e88
# c88ccae63e88

You can't remove a running up container

Here's the error you'll get if you try to delete a running container. You have to stop the container before removing it or force remove it.

docker container rm db39950ab550
Error response from daemon: You cannot remove a running container db39950ab55019fcb0b7eadaacb777f9babd6f8198c1082e5621980055c0eaa9. Stop the container before attempting removal or force remove

Force remove a running container

# The -f flag force-removes running containers.
docker rm -f db39950ab550 # or docker container rm -f db39950ab550
# db39950ab550

BlogCodeDocker