Docker Basics - How To Remove Docker Images, Containers, and Volumes

Docker Basics – How To Remove Docker Images, Containers, and Volumes

Where do Docker images, containers, and volumes go when they’re done with? 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 remove Docker images, containers, and volumes that are no longer needed? Let’s explore how to remove Docker images, containers, and volumes in this guide to Docker basics.

docker logo

 

Removing Docker Images

You can use either of two commands to remove a running image: docker rmi or docker commit. Both of these commands take a reference (either an ID or a tag) as their first argument and will delete everything associated with that image (except for any volumes). If you have multiple containers from that image running, be aware that when you issue either command 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 images via docker-compose rm [container] [service]. This gives you time to stop your dependent services so they don’t try to use resources no longer available in their restart script. Removing containers is similar to removing images. You will need to specify both an ID or a name and a force flag (-f) if there are active processes inside it. The syntax is similar but a bit more complicated due to differences between docker ps and docker rm. Since there could be active processes, there is always risk involved in forcibly removing anything from your system. So make sure you know what exactly it is that you’re doing before attempting things like rm -rf. Finally, we come down to deleting volumes.

Removing Docker Images

 

Removing Docker Containers

Once you are done using a Docker container, you can remove it with docker rm.

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

Make sure to stop your container before removing it.

Removing Docker Images: A Docker image is made up of read-only layers that contain your application’s 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 being removed.

Removing volumes: Volumes are just directories within containers that are mounted into one or more places on host machines for persistence or portability. After a volume has been created, you can use docker volume rm to remove it. Use the following command to remove a data volume called dbdata : docker volume rm dbdata. This will permanently erase any data contained in that directory inside your running database. Running all three together: If you want to clean out 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. WARNING! This will destroy all locally created images, containers and volumes not currently running.

 

Removing Docker Volumes

So you’ve spun up a container using a volume from your host machine, but now you’d like to remove that volume? There are a few ways to do it. The most direct way is to list all of your volumes with docker volume ls, then remove them one at a time with docker volume rm. Alternatively, you can use docker system prune which will remove all unused data from your containers. That’s pretty straightforward when you have only one or two volumes. If you have dozens or hundreds of containers in play though, it becomes something more akin to an archaeological dig. Fortunately there’s another alternative! Docker swarm gives users access to swarm-wide services called ‘reasons.’ These are basically triggered that execute when specific events occur on your cluster. You can setup multiple reasons for your swarm cluster to automatically cleanup old volumes. You’ll need to be running Swarm Mode with at least version 1.12 for reasons to work, however. 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 inside of Docker directly: Simplicity : A mounted directory will always exist no matter how many times you restart your server or delete containers.

Written by

Matthieu Robin Hidora
Matthieu ROBIN
25/03/2022

Matthieu Robin is CEO at Hidora, an experienced strategic leader, a former system administrator who had managed and configured more environments manually than anyone on the planet and after understanding that it could be done in several clicks established Hidora SA. Follow him on Twitter.

Receive our news

Subscribe to our monthly newsletter to stay informed