Hello friends!
How are you all doing? Hope you have spent a nice week. Here I come with another Docker article, which I need regularly to clean my hard disk. You can find my other docker article here.
Docker usually doesn’t remove unused objects such as containers, images, volumes, and networks unless you explicitly tell it to do so. After regularly work with Docker, it will accumulate a large number of unused objects that consume significant disk space. Also, it will clutter the output produced by the Docker commands.
This article helps me keep the system organized, and to free disk space by removing unused Docker containers, images, volumes, and networks.
Removing All Unused Objects
The “docker system prune” command will remove all stopped containers, all dangling images, and all unused networks:
docker system prune
You’ll be prompted to continue as follows. But you can use “-f” or “–force” flag to bypass the prompt.
WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all build cache Are you sure you want to continue? [y/N]
Removing Docker Containers
Docker containers are not automatically removed when you stop them unless you start the container using the “–rm” flag.
Remove one or more containers
You can remove one or more Docker images with the “docker container rm” command followed using the ID of the containers.
You can get a list of all containers with the “-a” flag to the “docker container ls” command:
docker container ls -a
The output might look something like as follows,
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d9212de8c12b nginx:alpine "nginx -g 'daemon of…" 18 hours ago Up 18 minutes 0.0.0.0:80->80/tcp sp_webserver 047f08efe812 phpmyadmin/phpmyadmin "/docker-entrypoint.…" 18 hours ago Exited (0) 8 hours ago sp_phpmyadmin dbe7329d03e5 wordpress:fpm-alpine "docker-entrypoint.s…" 18 hours ago Up 18 minutes 9000/tcp sp_wordpress b8adab6441c9 mysql:latest "docker-entrypoint.s…" 18 hours ago Up 18 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp db
Pass the “CONTAINER ID” to the “docker container rm” command to remove the container. For example, to remove the first two containers listed in the output above run:
docker container rm d9212de8c12b 047f08efe812
If you get an error similar to the following, it means that the container is running. You’ll need to stop the container before removing it.
Error response from daemon: You cannot remove a running container d9212de8c12b42a8bd0029df061cb74dc12cb174530b2036987575b83442b47. Stop the container before attempting removal or force remove.
Remove all stopped containers
To remove all stopped containers use the “docker container prune” command as follows:
docker container prune
You’ll be prompted to continue as follows, but you can use the “-f” or “–force” flag to bypass the prompt.
WARNING! This will remove all stopped containers. Are you sure you want to continue? [y/N]
To get a list of all non-running (stopped) containers that will be removed using the following command:
docker container ls -a --filter status=exited --filter status=created
Remove containers using filters
The “docker container prune” command allows you to remove containers based on condition using the filtering flag “–filter”.
Currently, docker support “until” and “label” filter. You can use more than one filter by using multiple “–filter” flags.
For example, to remove all images that are created more than 1 hours ago, run:
docker container prune --filter "until=1h"
Stop and remove all containers
You can get a list of all Docker containers ID on your system using the “docker container ls -aq” command. To stop all running containers use the “docker container stop” command followed by a list of all containers ID.
docker container stop $(docker container ls -aq)
Once all containers are stopped, you can remove them using the “docker container rm” command followed by a list of all containers ID.
docker container rm $(docker container ls -aq)
Removing Docker Images
Remove one or more images
You can remove Docker images using “docker image rm” command by passing the image’s ID. You can get the list of docker images using “docker images ls” command to find the ID of the images you want to remove.
docker image ls
The output might be look something like this:
REPOSITORY TAG IMAGE ID CREATED SIZE poserponlinexyz_webserver latest a8ec97d79f7f 43 hours ago 647MB mariadb 10.3 3a88f7086520 5 days ago 343MB poserponlinexyz_mysql latest 3a88f7086520 5 days ago 343MB wordpress fpm-alpine 539515b2a025 5 days ago 196MB
Once you’ve got the images you want to remove, pass their IMAGE ID to the “docker image rm” command. For example, to remove the first two images listed in the output above run:
docker image rm a8ec97d79f7f 3a88f7086520
If you get any error similar to the one showing below, it means that an existing container uses the image. To remove the image, you need to remove the container first.
Error response from daemon: conflict: unable to delete a8ec97d79f7f (cannot be forced) - image is being used by running container 029319840d36
Remove dangling images
A dangling image is an image that is not tagged and is not used by any container. Docker provides a command “docker image prune” that can be used to remove dangled and unused images.
docker image prune
You’ll be prompted to continue, use the “-f” or “–force” flag to bypass the prompt.
WARNING! This will remove all dangling images. Are you sure you want to continue? [y/N]
When removing dangling images, if the images build by you are not tagged, they will be removed too.
Remove all unused images
To remove all images which are not referenced by any existing container, not just the dangling ones, use the “docker image prune” command with the “-a” flag:
docker image prune -a
WARNING! This will remove all images without at least one container associated to them. Are you sure you want to continue? [y/N]
Remove images using filters
You can also remove docker images based on a certain condition using the filtering flag “–filter”.
Currently, supported filters are “until” and “label”. You can use more than one filter by using multiple “–filter” flags.
For example, to remove all images that are created more than 1 hours ago, you would run:
docker image prune -a --filter "until=1h"
Removing Docker Volumes
Remove one or more volumes
You can remove one or more Docker volumes by passing the volume name to “docker volume rm” command. To get the list of docker volume’s name use “docker volume ls” command.
docker volume ls
The output might look something like this:
DRIVER VOLUME NAME local 8eb93baf3a7d63dd5439117d5dc647a2489771fb81cb3f3a6704fcd43f11e360 local 9fb0b3051bb106d3522badf6afdda95a136b7afa879244d2b18850a411749af0 local 75acded3bb444d0b59a22dadd0a6c8709b3e54c892115e4cec08cea2f258fe68
Once you’ve found the “VOLUME NAME” of the volumes you want to remove, pass them to the “docker volume rm” command. For example, to remove the first volume listed in the output above, run:
docker volume rm 8eb93baf3a7d63dd5439117d5dc647a2489771fb81cb3f3a6704fcd43f11e360
If you get an error similar to the one showing below, it means that an existing container uses the volume. To remove the volume, you need to remove the container first.
Error response from daemon: remove 8eb93baf3a7d63dd5439117d5dc647a2489771fb81cb3f3a6704fcd43f11e360: volume is in use - [232c6430d9551e4e2fc5c5112d20502b2be5261a2c3311baa70290d72a64feb7]
Remove all unused volumes
To remove all unused volumes use the “docker image prune” command:
docker volume prune
You’ll be prompted to continue, use the “-f” or “–force” flag to bypass the prompt.
WARNING! This will remove all local volumes not used by at least one container. Are you sure you want to continue? [y/N]
Removing Docker Networks
Remove one or more networks
You can use “docker network rm” command to remove one or more Docker networks by passing the Network ID. You will get a list of Docker network using “docker network ls” command.
docker network ls
The output might look something like this:
NETWORK ID NAME DRIVER SCOPE a026f27f6f1c bridge bridge local f492b8befe67 host host local 9e289ef9d748 none null local 94687f754d53 pos_default bridge local
Once you’ve got the networks you want to remove, pass their NETWORK ID to the “docker network rm” command. For example to remove the network with the name “pos_default” run:
docker network rm 94687f754d53
If you get an error similar to the one shown below, it means that an existing container uses the network. To remove the network you need to remove the container first.
Error response from daemon: error while removing network: network poserponlinexyz_default id 94687f754d533bddecf834a98849133ee8de628c4f2458b939983cf92f017e9c has active endpoints
Remove all unused network
Use the “docker network prune” command to remove all unused networks.
docker network prune
You’ll be prompted to continue, use the “-f” or “–force” flag to bypass the prompt.
WARNING! This will remove all networks not used by at least one container. Are you sure you want to continue? [y/N]
Remove networks using filters
With the “docker network prune” command you can remove networks based on condition using the filtering flag “–filter”.
Currently, supported filters are “until” and “label”. You can use more than one filter by using multiple --filter
flags.
For example, to remove all networks that are created more than 1 hours ago, run:
docker network prune -a --filter "until=1h"
Conclusion
In this article, I have referred some of the common commands for removing Docker containers, images, volumes, and networks. To know more, check out the official Docker documentation.
If you have any questions, please leave a comment below.
Thanks for your time. See you soon!