LinuxCommandLibrary

docker-rm

Remove Docker containers

TLDR

Remove containers

$ docker rm [container1 container2 ...]
copy

Force remove a container
$ docker rm [[-f|--force]] [container1 container2 ...]
copy

Remove a container and its volumes
$ docker rm [[-v|--volumes]] [container]
copy

Display help
$ docker rm [[-h|--help]]
copy

SYNOPSIS

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

PARAMETERS

--force, -f
    Force the removal of a running container. Docker will first stop the container before deleting it.

--volumes, -v
    Remove any anonymous volumes associated with the container. This does not remove named volumes or host-mounted volumes.

--help
    Display help information for the docker rm command.

DESCRIPTION

The docker rm command is used to delete one or more containers from a Docker host.

This command is fundamental for managing container lifecycle and freeing up system resources. When a container is removed, its writable layer is permanently deleted, and any resources it consumed, such as network interfaces and process IDs, are released.

By default, docker rm can only remove stopped containers. Attempting to remove a running container without additional options will result in an error. To forcefully remove a running container, the --force (or -f) option must be used, which first attempts to stop the container before removing it.

Users can specify containers by their ID or name. Multiple containers can be removed simultaneously by listing their IDs or names separated by spaces. It's crucial to understand that docker rm does not automatically remove data volumes (named volumes or host-mounted volumes) associated with the container; only anonymous volumes can be removed using the --volumes (or -v) option. This distinction helps prevent accidental data loss.

CAVEATS

Caveats and Limitations:

* Removing a container with docker rm permanently deletes its writable layer and associated configurations.
* By default, docker rm cannot remove a running container. The --force (-f) option is required to stop and then remove a running container.
* The --volumes (-v) option only removes anonymous volumes. Named volumes or host-mounted volumes must be managed and removed separately using docker volume rm.
* Ensure no critical data resides solely within the container's writable layer before removal, as it will be lost. Persistent data should always be stored in volumes.

CLEANUP STRATEGY

It's common practice to use docker rm in conjunction with docker ps -aq to remove all stopped containers:
docker rm $(docker ps -aq). This command removes all containers that are not currently running.

EXIT STATUS

The docker rm command typically exits with a status of 0 on success. A non-zero exit status indicates an error during the removal process.

HISTORY

The docker rm command has been a core component of the Docker CLI since its early days, essential for managing the lifecycle of containers. Its functionality has remained largely consistent, reflecting its fundamental role in freeing up resources and cleaning the Docker environment. As Docker evolved, the emphasis shifted towards more robust volume management (named volumes vs. anonymous volumes) and network management, but the rm command's purpose of container deletion remained central.

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