How to cleanup Docker
Docker doesn’t remove unused objects such as containers, images, volumes, and networks unless you explicitly tell it to do so. How To Remove them?
Removing All Unused Objects
Section titled “Removing All Unused Objects”Remove stopped containers, all dangling images, and all unused networks:
docker system prune
WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all dangling build cache
Are you sure you want to continue? [y/N]
You can also include all unused volumes, then add --volumes
:
docker system prune --volumes
Docker Containers
Section titled “Docker Containers”docker container ls -a
Remove selected container
Section titled “Remove selected container”docker container rm [CONTAINER ID]
Remove all stopped containers
Section titled “Remove all stopped containers”To remove all stopped containers use:
docker container prune
You can also list what will be removed:
docker container ls -a --filter status=exited --filter status=created
Stop and remove all containers
Section titled “Stop and remove all containers”docker container stop $(docker container ls -aq)docker container rm $(docker container ls -aq)
Docker Images
Section titled “Docker Images”You can list them:
docker image ls
Remove selected image
Section titled “Remove selected image”docker image rm [IMAGE ID]
Remove dangling images
Section titled “Remove dangling images”A dangling image is an image that is not tagged and is not used by any container. You can remove them by:
docker image prune
Remove all unused images
Section titled “Remove all unused images”To remove all images which are not referenced by any existing container,
not just the dangling ones, use the prune
command with the -a
flag:
docker image prune -a
Docker Networks
Section titled “Docker Networks”You can list them with:
docker network ls
Remove selected network
Section titled “Remove selected network”docker network rm [NETWORK ID]
Remove all unused networks
Section titled “Remove all unused networks”Use the docker network prune
command to remove all unused networks.
Remove all networks that are created more than 12 hours ago:
docker network prune -a --filter "until=12h"
Docker Volumes
Section titled “Docker Volumes”docker volume ls
Remove selected Volume
Section titled “Remove selected Volume”docker volume rm [VOLUME NAME]
Remove all unused volumes
Section titled “Remove all unused volumes”docker volume prune