Docker Basics - How do I delete Docker images, containers and volumes? image

Docker Basics - How do I delete Docker images, containers and volumes?

Where do Docker images, containers and volumes go when they are no longer in use? Should you delete them all when you're done? Or should you leave them behind in case you want to start over? How can you delete Docker images, containers and volumes that you no longer need? Let's look at how to delete Docker images, containers and volumes in this guide to Docker basics.

docker logo

Removing Docker images

You can use either of the following two commands to delete a running image: docker rmi or docker commit. Both of these commands take a reference (either an ID or a label) as their first argument and will delete everything associated with that image (except for volumes). If you have multiple containers in this image running, be aware that when you issue one of these commands, all of those containers will be stopped. When they restart, they will start from scratch using a new base image. In most cases, you want to remove the images via docker-compose rm [container] [service]. This gives you time to stop your dependent services from trying to use resources that are no longer available in their restart script. Removing containers is similar to removing images. You will need to specify both an ID or name and a force_(-f_) flag if there are active processes inside. The syntax is similar but a bit more complicated due to the differences between docker ps and docker rm. As there may be active processes, there is always a risk of forcibly removing something from your system. So make sure you know exactly what you are doing before attempting operations like rm -rf. Finally, we come to deleting volumes.

Removing Docker Images

Removing Docker containers

Once you have finished using a Docker container, you can remove it with docker rm.

The following command will remove a container called myfirstcontainer: docker rm myfirstcontainer.

Be sure to stop your container before removing it.

Removing Docker images: A Docker image consists of read-only layers that contain your application files. You can use docker rmi to delete images after they have been pushed to a registry. The following command removes an image named redis_1: docker rmi redis_1. Remember that an image must be stopped before it is deleted.

Deleting volumes: Volumes are just directories in containers that are mounted in one or more locations on host machines for persistence or portability. After a volume has been created, you can use docker volume rm to delete it. Use the following command to delete a data volume called dbdata: docker volume rm dbdata. This will permanently delete all the data contained in this directory inside your running database. Run all three together: If you want to clean up everything related to a project - images, containers and volumes - you can run all three commands at once as shown below: sudo docker system prune -all. CAUTION: This will destroy all locally created images, containers and volumes that are not running.

Removing Docker volumes

You have created a container using a volume on your host machine, but now you want to delete that volume? There are several ways to do this. The most direct way is to list all your volumes with docker volume ls, then delete them one by one with docker volume rm. You can also use docker system prune which will delete all unused data from your containers. This is quite simple when you have only one or two volumes. But if you have dozens or hundreds of containers in play, it becomes more like an archaeological dig. Fortunately, there is another solution! The Docker swarm gives users access to swarm services called “reasons”. These are essentially triggers that run when specific events occur on your cluster. You can set up multiple reasons for your swarm cluster to automatically clean up old volumes. However, you must be using Swarm mode with at least version 1.12 for the reasons to work. Why doesn’t everyone just use volume mounts? All things being equal, mounting a local directory as a volume has many advantages over creating persistent storage directly in Docker: Simplicity: A mounted directory will always exist, no matter how many times you restart your server or delete containers.

profile picture

Written By

Matthieu ROBIN


Matthieu Robin is the CEO of Hidora, an experienced strategic leader, a former system administrator who has managed and configured more environments manually than anyone else on the planet and after realising that it could be done with a few clicks created Hidora SA. Follow him on Twitter.

Start your free trial

No credit card required. Free 14 day trial.

We only use your personal data to create your account, promise!

Choose your currency


Read more articles


Receive our news