docker-container-remove
Remove existing Docker containers
TLDR
View documentation for the original command
SYNOPSIS
docker rm [OPTIONS] CONTAINER [CONTAINER...]
PARAMETERS
-f, --force
Force the removal of a running container (sends a SIGKILL signal).
-l, --link
Remove the specified link (legacy feature for container linking).
-v, --volumes
Remove anonymous volumes associated with the container. Named volumes are not removed by default.
DESCRIPTION
docker rm is a fundamental command for managing the Docker environment. It is used to remove one or more stopped Docker containers from the host system. This action is crucial for reclaiming disk space and maintaining a clean Docker setup, especially in development or testing environments where containers are frequently created and discarded.
By default, docker rm can only remove containers that are not running. If a container is active, it must first be stopped using docker stop or docker kill, or alternatively, the -f or --force option can be used to forcefully remove a running container, which sends a SIGKILL signal. When a container is removed, its writable layer is deleted, but the base image it was built from remains available on the system. It's important to note that docker rm does not automatically remove volumes or networks associated with the container unless specified with the -v option for anonymous volumes.
CAVEATS
Running Containers: Cannot remove a running container without the -f or --force option. Forcing removal can lead to data loss if the application within the container hasn't shut down gracefully.
Named Volumes: The -v option only removes anonymous volumes. Named volumes, which persist data even after the container is removed, must be explicitly removed using docker volume rm.
Images: docker rm removes only the container instance, not the image it was created from. Images require docker rmi for removal.
Dependencies: Containers might be dependent on other containers (e.g., through networking). Removing a dependent container might affect others.
REMOVING ALL EXITED CONTAINERS
To quickly remove all stopped containers, one can combine docker rm with docker ps -aq. The command is docker rm $(docker ps -aq). This is a common pattern for clearing development environments.
AUTOMATIC REMOVAL ON EXIT
For temporary containers, the --rm flag can be used when starting a container (e.g., docker run --rm ...). This automatically removes the container once it exits, eliminating the need for a separate docker rm command.
HISTORY
The docker rm command has been a core part of the Docker CLI since its early days, essential for managing container lifecycles and disk space. Its functionality has remained largely consistent, reflecting its straightforward purpose of deleting container instances. While Docker's networking and volume management have evolved significantly, docker rm continues to provide the fundamental cleanup mechanism for individual containers, often used in conjunction with other commands for comprehensive system maintenance.