Introduction
Working with Docker can get messy, especially if you are working with it daily. If you’re like me, you are working with multiple applications and creating multiple volumes for each application. It would be nice if there was a simple way to clean up docker. Below are the commands needed to easily clean up unused docker containers, images, volumes, and networks.
Clean up unused Docker Containers
When you stop a docker container it sits on the host system just in case you want to use it again. You can choose to delete these containers individually or you can run the following command in your terminal to delete them all at once.
docker container prune
After you hit the enter key, you’ll get the following warning:
WARNING! This will remove all stopped containers. Are you sure you want to continue? [y/N]
Typing y
and hitting enter will proceed with removing all your unused docker containers. You’ll then get a nice notification that informs you of how much space you saved by pruning your containers.
Clean up unused Docker Images
In some situations, unused images can pile up quicker than unused containers. This is especially true if you are using Ouroboros or Watchtower to automatically update your running containers and have them set to leave the previous images so you can revert back to one of those images if an update breaks one of your running applications. Images can also pile up quickly if you’ve been browsing the Docker Hub or experimenting with the latest docker images from Linuxserver.io.
To delete all of your unused images at once, you can run the following command in terminal:
docker image prune
When you run this command you’ll be asked to verify that you want to delete all of your dangling images.
WARNING! This will remove all dangling images. Are you sure you want to continue? [y/N]
Type y
and hit enter, and you’ll be greeted with a satisfying message on how much space you just saved.
Clean up unused Docker Volumes
If you are storing your persistent volume data on your host machine instead of a network file share then the unused volumes on your system can take up a lot of space. Although some volumes are created manually, some containers automatically create volumes when they initially startup. If you delete those containers, the volumes will remain on the system. You can clean up docker volumes by running the following command in your terminal:
docker volume prune
Once you hit enter, you’ll be asked if you are sure…
WARNING! This will remove all local volumes not used by at least one container. Are you sure you want to continue? [y/N]
Type y
and hit enter and you’re good to go!
Clean up unused Docker Networks
Unused docker networks do not take up much space on your system but can annoy those who like to keep a tidy docker host. If you want to display all of your docker networks, type the following command:
docker network ls
Once you hit enter you’ll be presented with all of the networks created on your docker host.
NETWORK ID NAME DRIVER SCOPE 5aefeb6a2076 Main bridge local 0dd9f0b8e326 bridge bridge local e7ce4bb22108 docker_gwbridge bridge local f19e16a3c5c6 host host local osinb3qa6ylq ingress overlay swarm bf21813c5f46 none null local
In order to clean up docker networks that are not in use by a container then type the following command:
docker network prune
You’ll be asked if you would like to continue:
WARNING! This will remove all networks not used by at least one container. Are you sure you want to continue? [y/N]
Type y
and docker will remove all of the networks that you are not using.
Cleaning up everything at once
The quickest way to clean up docker will clean up all of your unused containers, volumes, images and networks at once. You can clean up your entire host system by typing out the following command in your terminal:
docker system prune
When you hit enter, you’ll be presented with the following warning:
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]
Typing y
and hitting enter will remove every unused item in your docker host.
Success!
Running these commands occasionally is a great way to make sure you are not wasting valuable resources on your docker host system. These commands are also the easiest way to remove unused items in bulk. If you are looking for more docker commands, visit 25 basic docker commands for beginners.