Docker rm Command
docker rm : Delete one or more containers.
Docker rm Syntax
docker rm [OPTIONS] CONTAINER [CONTAINER...]
OPTIONS Description:
- -f : Forced deletion of a running container by SIGKILL signal.
- -l : Removes network connections between containers, not the containers themselves.
- -v : Removes the volumes associated with a container.
Docker rm Examples
Forced deletion of containers db01, db02.
docker rm -f db01 db02
Remove the connection from container nginx01 to container db01, connection name db.
docker rm -l db
Delete the container nginx01, and delete the container’s mounted data volumes.
docker rm -v nginx01
Delete all discontinued containers.
docker rm $(docker ps -a -q)
Docker cleanup commands
Kill all running containers:
docker kill $(docker ps -a -q)
Delete all stopped containers:
docker rm $(docker ps -a -q)
Delete all mirrors that are not tagged with dangling:
docker rmi $(docker images -q -f dangling=true)
Delete the specified mirror by its id:
docker rmi <image id>
Delete all mirrors:
docker rmi $(docker images -q)
Create aliases for these commands
# ~/.bash_aliases
# Kill all running containers
alias dockerkill='docker kill (docker ps -a -q)'
# Delete all stopped containers
alias dockercleanc='docker rm(docker ps -a -q)'
# Delete all untagged mirrors
alias dockercleani='docker rmi $(docker images -q -f dangling=true)'
# Delete all stopped containers and untagged images
alias dockerclean='dockercleanc || true && dockercleani'