LinuxCommandLibrary

docker-container-rm

Remove Docker containers

TLDR

View documentation for the original command

$ tldr docker rm
copy

SYNOPSIS

docker rm [OPTIONS] CONTAINER [CONTAINER...]

PARAMETERS

-f, --force
    Force the removal of a running container. Uses SIGKILL to stop the container first.

-l, --link
    Remove the specified link. (Primarily for legacy container linking, not common for general docker rm use).

-v, --volumes
    Remove the anonymous volumes associated with the container. Named volumes are not affected.

DESCRIPTION

The docker rm command is used to remove one or more stopped Docker containers from a host. By default, it will only remove stopped containers. To remove a running container, the
--force (or -f) option must be used, which sends a SIGKILL signal to the container before attempting removal.
This command is crucial for freeing up system resources, such as disk space and memory, consumed by containers that are no longer needed. When a container is removed, its writable layer is deleted. However, volumes attached to the container are not removed by default. To also remove anonymous volumes associated with the container, the
--volumes (or -v) option can be specified. Named volumes and host-mounted volumes require separate commands for removal.

CAVEATS

Cannot remove a running container without the --force (-f) option. Using -f will terminate the container immediately without giving it a chance to gracefully shut down.
By default, named volumes and host-mounted volumes are not removed when a container is deleted, even with the -v option. These must be managed separately using docker volume rm.
Associated networks and images used by the container are not removed by docker rm. These must be cleaned up independently using docker network rm or docker rmi respectively.
Removing a container does not remove its parent image or any child containers it might have spawned. The container's history and configuration are lost upon removal.

REMOVING ALL STOPPED CONTAINERS

To remove all stopped containers, a common pattern involves combining docker ps -aq (which lists all container IDs, including stopped ones, in quiet mode) with docker rm. For example:
docker rm $(docker ps -aq)
This command should be used with caution as it will attempt to remove all containers, whether stopped or not, potentially leading to errors for running ones unless -f is added to the docker rm command.

DISTINCTION FROM DOCKER STOP/KILL

While docker stop and docker kill are used to halt a running container, docker rm is specifically for deleting the container's instance from the Docker daemon. A stopped or killed container still exists on the system and consumes disk space; it needs to be removed to free up those resources. Think of it as 'shutting down' vs. 'deleting the machine'.

HISTORY

The docker rm command has been a fundamental part of the Docker CLI since its early days, reflecting the crucial need for container lifecycle management and resource cleanup. As Docker evolved from a niche technology to a mainstream platform for containerization, the ability to efficiently remove containers became paramount for maintaining clean development environments, managing CI/CD pipelines, and ensuring robust production systems. Its design emphasizes explicit control: containers are not automatically removed to prevent accidental data loss, placing the responsibility on the user to manage cleanup. The addition of options like -v and the development of broader cleanup commands like docker system prune showcase the growing maturity of Docker's resource management capabilities over time.

SEE ALSO

docker ps(1), docker stop(1), docker kill(1), docker rmi(1), docker volume rm(1), docker system prune(1)

Copied to clipboard