This article describes how to delete objects, Images, Containers, Volumes, and Networks in Docker.
We will explain in detail how to use the options of the
Docker rm command and other useful commands that are difficult to understand from the official documentation alone.
If you are new to Docker, please refer to this article as a cheat sheet.
Purging All Unused or Dangling Images, Containers, Volumes, and Networks
Ever wanted to get rid of unnecessary things with Docker? If you want to delete a Docker object, that's often the case. In that case, the command
Docker prune is effective. The prune command allows you to delete unused Docker objects (containers, images, networks, volumes) all at once.
The following is an example.
docker system prune
At this time, by using the
—filter option, it is possible to narrow down and delete the containers that have been stopped for a long time. The following is an example of deleting a container that has been stopped for more than 24 hours.
More information on filtering can be found in the following official documentation: https://docs.docker.com/engine/reference/commandline/system_prune/
docker system prune -a --filter "until = 24h"
At this time, the
-a option can clear the build cache and the intermediate image.
Removing Docker Containers
This section describes the command to delete a Docker container.
Removing one or more containers by id or name
When deleting one Docker container, you can delete it by specifying the container ID or name. The following is an example.
docker rm 4c01db0b339c
You can also delete multiple containers together by specifying the IDs or names of multiple containers, as shown below.
docker rm 4c01db0b339c d7886598dbe2
Removing all stopped containers
If your Docker version is 1.13 or higher, you can easily remove all stopped containers using the
docker container prune command. The following is an example.
docker container prune
Remove a container upon exit
Do you use a lot of temporary Docker containers? In that case, you can use the
--rm option of
docker run to automatically delete the container when it exits. It cannot be used together with the
-d option (detach mode).
docker run —rm image_name
Stop and remove all containers
The following command is convenient if you want to stop/remove all the containers, including the running container.
docker stop $ (docker ps -a -q) docker rm $ (docker ps -a -q)
In this command,
docker ps -a -q is used to display a list of IDs of all Docker containers, and
docker rm is used to delete them.
Removing Docker Images
Removing one or more images by id
docker rmi command to delete a Docker image. As below:
docker rmi image_id
Also, since this command can be specified multiple times like deleting a container, You can delete multiple images at the same time as shown below.
docker rmi image_id_1 image_id_2
Removing images by tag
docker rmi command, you can usually delete only by specifying the image ID, but you can delete it by selecting the tag name using the
docker rmi -f tag_name
Removing dangling images
Like deleting a container, deleting an image can quickly delete all stopped containers by specifying
prune in the command. The following is an example.
docker image prune
Removing all unused images
docker image prune command, you can use the
-a option to delete only unused images from existing containers.
docker image prune -a
Removing all images
To remove all images, whether you are using them or not, enter the following command:
docker rmi $ (docker images -a -q)
It removes all images by displaying all Docker images with the
-a option of the
docker images and displaying the IDs of all the images with the
Removing Docker Volumes
Removing one or more volumes
To delete a Volume, use the
docker volume rm command as shown below.
docker volume rm volume_name
As with the deletion of other objects, multiple deletions are possible.
docker volume rm volume_name1 volume_name2
At this time, if the error shows
volume is in use, the container using volume may not be deleted yet. In that case, you can delete it by deleting the corresponding Docker container.
Removing all unused volumes
You can delete unused volumes with the
docker volume prune command.
docker volume prune
—filter You can also use a flag to limit the range of deletions. For example, the following command only deletes volumes labeled
docker volume prune —filter "label = test"
Removing Docker Networks
Removing one or more networks by id or name
To remove Docker's Network, use the
docker network rm command. In this case, specify either the name or id of the network.
docker network rm network_name_or_id
This command can also be deleted multiple times and is specified as follows.
docker network rm 3695c422697f my-network
Also, if you specify multiple networks, this command attempts to delete each network in turn. If deleting one network fails, proceed to the next and try to delete that network. Success or failure is reported with each deletion.
Removing all unused network
If you want to remove an unused network, use the
docker network prune command like any other object.
docker network prune
You can specify the range to be deleted by using the
--filter option. For example, the following is an example of deleting only the network that has passed 24 hours.
docker network prune --filter "until = 24h"
Finally, I will show you how to delete the object created by docker-compose when you want to clean the development environment and recreate it from scratch.
Below is an example of a command to clean all containers, images, volumes, networks, and undefined containers created with docker-compose.
docker-compose down --rmi all -v --remove-orphans
docker-compose down is the exact opposite of
docker-compose up. By default, it deletes created objects such as containers and networks. In addition, each option means the following.
-- rmi allRemove all images
-vRemove the named volumes declared in the volumes section of docker-compose.yml and the anonymous volumes attached to the container
--remove-orphansRemove containers not defined in docker-compose.yml
Therefore, this command example removes containers, images, volumes, networks, and undefined containers.
I've introduced you to various Docker removal commands. Recent Docker is very convenient because you can delete unnecessary objects by using the
prune command. You can learn more about the commands introduced so far from the official documentation below.