3v-Hosting Blog
How To Remove Docker Images, Containers, and Volumes
9 min read
Since its appearance back in 2013, Docker has revolutionized the way we can develop, deliver, and run our own and other applications using containerization technology. We recently wrote about how to install Docker on a server with Ubuntu22.04 OS and use it. However, if we have been working with Docker for a long time, our system can accumulate a significant number of Docker images, containers, and volumes that we no longer need. These remnants can take up valuable disk space and can potentially cause conflicts. Therefore, it is extremely important to know how to effectively delete these Docker artifacts, which is what we will discuss in this article.
What are Docker Images, Containers, and Volumes
Before we start looking at how to delete images, containers, and volumes, it's important to understand what each of the components we'll be dealing with are:
Docker Images: These are read-only templates that contain your application code and pointers to all of its dependencies. Images are used to create Docker containers. You can download them from the Docker repository, or share your own images containing your application.
Docker Containers: Containers are instances of Docker images in a running state, running, that is, during their execution. They are small, isolated environments with their own set of software in which applications run.
Docker Volumes: Volumes are used to persist data generated and used by Docker containers even after they are shut down. You can think of a volume as persistent memory, a space on your server's hard drive that is dedicated exclusively to a particular container and that can be easily moved from one place on the disk to another, or even between different servers when moving an entire application. So if you think of a container as a small, separate server, then a volume is its hard drive.
Now that we've defined the basic concepts, we can get down to practice.
Removing Docker Images
Docker images can quickly accumulate on your system, especially if you frequently download and/or create new images. Here's how to manage and remove them:
Listing Docker Images
Before removing images, it's helpful to see what images are currently on your system. Use the following command to list all Docker images:
docker images
This command will list the images along with their repositories, tags, and sizes. For example:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 77af4d6b9913 19 hours ago 1.089 GB
committ latest b6fa739cedf5 19 hours ago 1.089 GB
<none> <none> 78a85c484f71 19 hours ago 1.089 GB
docker latest 30557a29d5ab 20 hours ago 1.089 GB
<none> <none> 5ed6274db6ce 24 hours ago 1.089 GB
postgres 9 746b819f315e 4 days ago 213.4 MB
postgres 9.3 746b819f315e 4 days ago 213.4 MB
postgres 9.3.5 746b819f315e 4 days ago 213.4 MB
postgres latest 746b819f315e 4 days ago 213.4 MB
Removing a specific Docker image
To remove a specific Docker image, you need the image ID or the repository name and tag. Use the following command to remove the image:
docker rmi <image_id>
Replace <image_id> with the actual image ID. You can also use the repository name and tag:
docker rmi <repository>:<tag>
For example:
docker rmi ubuntu:latest
Forcing removal of Docker images
Sometimes you may encounter a situation where an image is in use by a stopped and running container, which prevents it from being removed. In such cases, you can use the -f flag to forcefully remove the image:
docker rmi -f <image_id>
Removing all Docker images
IMPORTANT! Be careful with the following command, as it will remove all images from your system.
To remove all Docker images from your system, use the following command:
docker rmi $(docker images -q)
The docker images -q command in parentheses lists all image IDs, which are then passed as a parameter to the docker rmi command to remove them.
Other popular articles in our Blog:
- What is LVM and how to create LVM on Ubuntu
- Maximizing performance: 10G servers and their applications
- Process Juggling - Using the Linux ps Command with Examples
- How to open a port in UFW
Removing Docker containers
Containers can also accumulate, especially if you frequently create (run images) and stop containers. Before removing any container, it is recommended to check the list of existing containers so as not to remove anything unnecessary.
List Docker containers
To list all Docker containers, use the following command:
docker ps
For example:
$ docker ps --no-trunc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ca5534a51dd04bbcebe9b23ba05f389466cf0c190f1f8f182d7eea92a9671d00 ubuntu:22.04 bash 17 seconds ago Up 16 seconds 3300-3310/tcp webapp
9ca9747b233100676a48cc7806131586213fa5dab86dd1972d6a8732e3a84a4d crosbymichael/redis:latest /redis-server --dir 33 minutes ago Up 33 minutes 6379/tcp redis,webapp/db
As you may have noticed, this command can have parameters. For example, if you add the -a (--all) parameter to this command, then this command will display all containers, including stopped ones
Removing a specific Docker container
To remove a specific container, you will need the container ID or name. Use the following command:
docker rm <container_id>
Replace <container_id> with the actual container ID or name.
Forcibly removing Docker containers
If a container is running and you want to remove it, you must first stop it, or use the -f flag to forcefully remove it:
docker rm -f <container_id>
Removing all stopped Docker containers
To remove all stopped containers, use the following command:
docker container prune
This command will ask for confirmation before removing all stopped containers.
Removing Docker Volumes
Volumes are used to store data, and over time, after deleting old containers, you may accumulate unused volumes. And to prevent them from clogging up precious disk space, you can also delete them. But first, just like with images and containers, we need to understand what volumes currently exist in the system.
Listing Docker Volumes
To get a list of all Docker volumes, use the following command:
docker volume ls
This command will display a list of volumes along with their drivers and names. For example:
$ docker volume ls
DRIVER VOLUME NAME
local my_chat_app
local john_game_app
Removing a specific Docker volume
To remove a specific volume, you need the volume name. Use the following command:
docker volume rm <volume_name>
Replace <volume_name> with the actual volume name.
Force removal of Docker volumes
To force removal of a volume, you can use the -f flag:
docker volume rm -f <volume_name>
Removing all unused Docker volumes
Sometimes it is convenient to remove all unused volumes. To do this, use the following command:
docker volume prune
This command will ask for confirmation before removing all unused volumes.
Advanced Cleanup Methods
Docker provides many additional, convenient commands and methods that will help you manage your Docker environment more effectively and clean it up as needed.
Removing Dangling Images
For example, you can remove so-called Dangling images, which are images that are not referenced by any container. To do this, use the following command:
docker image prune
Automating Docker Cleanup
Manual cleanup can be tedious, especially if you have a lot of Docker resources or manage a lot of servers. You can automate the cleanup process with a script. Here is an example of a Bash script to clean up images, containers, and Docker volumes:
#!/bin/bash
# Remove all stopped containers
docker container prune -f
# Remove all unused volumes
docker volume prune -f
# Remove all dangling images
docker image prune -f
# Remove all unused images
docker image prune -af
echo "Docker cleanup completed."
Save this script as docker_cleanup.sh, make it executable, and run it periodically to keep your Docker environment clean:
chmod +x docker_cleanup.sh
./docker_cleanup.sh
Conclusion
Efficient management of Docker images, containers, and volumes is essential to maintaining a clean and functional Docker environment, especially if you have tens or hundreds of Docker servers under your management. By regularly removing unused and unnecessary resources, you can free up disk space and avoid potential conflicts between applications in the future.
Armed with these basic commands, you can always tailor the cleaning process to your specific needs, as Docker is a very flexible tool that allows you to easily customize its use.
But it can also consume a lot of resources, so always remember that a clean Docker environment not only saves disk space, but also improves performance and reduces the risk of problems caused by stale or unused resources. So always take the time to regularly review and clean out unnecessary images, containers, and volumes.