Docker is software for quickly deploying applications through containerization. However, with its active use, many objects accumulate, consuming valuable host resources: images, containers, volumes, and networks.
You can remove these objects through Docker Desktop, but it is much more convenient, especially when dealing with a significant number of objects, to use command-line tools. In this article, you will find tips for working with Docker and learn how to remove various objects using both the Docker Desktop client and command-line tools.
To interact with containers and change their current state, including removing them, go to the "Containers/Apps" tab in the Docker Desktop web interface, select the desired object, and apply the chosen action:
Now, let's look at how to remove these objects using command-line tools.
To remove containers, use the docker container rm
command or simply docker rm
. For clarity, we will use docker container rm
with the following syntax:
docker container rm [removal options] [object ID]
Options:
--force
or -f
: Force removal of the container (e.g., if running).
--link
or -l
: Remove the specified link (e.g., between two objects)*.
--volume
or -v
: Remove anonymous volumes associated with the container.
Containers are isolated from each other. One way to link them is via network ports. Using the --link
flag will remove this network link in Docker.
There are two commands in the command-line arsenal for obtaining information about containers: docker ps
and docker container ls
. They have the same options and syntax:
Options:
--all
or -a
: Display all containers. By default, only running containers are displayed.--filter
or -f
: Filter based on a set of flags.--format
: Format the output. You can display only the necessary information.--last
or -n
: Show the last n containers.--latest
or -l
: Show the most recent container.--no-trunc
: Do not truncate the output.--quiet
or -q
: Display only the container IDs.--size
or -s
: Display the total size.Using these parameters, you can create a list of containers you wish to remove, then pass the container IDs to the docker container rm
command. For example, to create a list of containers with the status created
or exited
, run this command to get such objects:
docker ps -a -f status=created -f status=exited
Now, pass the result to the removal command:
docker container rm $(docker ps -a -f status=created -f status=exited -q)
To remove running containers, you must first stop them. Of course, you can use the --force
option, but this may lead to data corruption with the application's working data. It is always better to first stop the containers with the docker stop
command. To remove all containers in Docker, you can simply use these two commands:
docker stop $(docker ps -a -q)
docker container rm $(docker ps -a -q)
There is a separate command to remove all stopped containers: docker container prune
.
Like containers, you can also remove Docker images within the client application. To do this, go to the "Images" tab:
To delete an image, click "Clean up…" in the upper right corner and select the images. If an image is currently in use, Docker will not allow you to delete it. Now, let's move on to the command-line tools.
There are two commands for removing Docker images: docker rmi
and docker image rm
. They are identical and work in much the same way as docker rm
. Here's their syntax:
docker rmi [remove options] [image IDs]
Options:
--force
or -f
: Forcefully remove the image.
--no-prune
: Do not remove untagged parent images.
To find the image IDs, we use the following command:
docker images [options] [REPOSITORY:[TAG]]
Options:
--all
or -a
: Show all images. By default, intermediate images are hidden.
--digests
: Show digests.
--filter
or -f
: Filter by flags.
--format
: Format the output.
--no-trunc
: Do not truncate the output.
--quiet
or -q
: Show only the image IDs.
The application of these commands is the same as in the previous section. First, we query the list of images we want and use it as input for the docker rmi
command. For example, to remove images that are not associated with any containers, we can use the dangling=true
flag. It is important to note that we will get untagged images.
docker images --filter dangling=true
After checking the list, we can safely remove it:
docker rmi $(docker images --filter dangling=true -q)
To remove all unused images, use the docker image prune
command.
A volume is a file system located outside the containers and stored on the host machine. To free up disk space occupied by volumes, go to the "Volumes" section, and in the top-right corner, select the corresponding icon:
To delete volumes from the command line, use the docker volume rm
command with the following syntax:
docker volume rm [options] [volume names]
This command is not very flexible with options and only provides the --force
or -f
flag for forced removal. You can only remove volumes if they are not associated with running containers. Forced removal of volumes linked to active containers is not recommended, as it may lead to data corruption.
To list volume names, use the docker volume ls
command with the following syntax:
docker volume ls [options]
Again, Docker is limited on options here, with only three available:
--filter
or -f
: Filter by flags.
--format
: Format the output.
--quiet
or -q
: Show only the volume names.
Volumes exist independently of containers, and after their deletion, they remain in the host's file system as unlinked volumes. Let's try deleting such volumes. Use the dangling=true
flag for this purpose:
docker volume ls -f dangling=true
Now, pass the results to the command for deletion:
docker volume rm $(docker volume ls -f dangling=true -q)
Alternatively, you can use another command to remove all unused volumes: docker volume prune
. However, before using this command, check the list to ensure it includes only the volumes you want to remove.
If you need to remove an unnamed volume, you can delete it with its associated container. For this, add the -v
flag when using docker rm
.
To remove networks, you need to use the docker network rm
command with the following syntax:
docker network rm [network names/IDs]
This command does not have any options. You can pass either names or IDs of the networks. To find the names and IDs of the networks, use the docker network ls
command:
docker network ls [options]
This command has four available options:
--filter
or -f
: Filter by flags.
--format
: Format the output.
--no-trunc
: Do not truncate the output.
--quiet
or -q
: Show only IDs.
Before deleting a network, you must remove any objects (containers) that are using it. To check which containers are using a specific network, use the following command:
docker ps -f network=[network ID]
Afterward, you can proceed to delete the network. For example, to delete networks with the driver=bridge
value, use the following commands:
docker network ls -f driver=bridge
docker network rm $(docker network ls -f driver=bridge -q)
Sometimes, you might need to remove everything and reinstall Docker to return an application to its initial state. Instead of deleting Docker entirely, you can execute a series of commands to clean up all objects and work with a fresh environment:
Stop and remove containers:
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
Remove images:
docker rmi $(docker images -a -q)
Remove volumes:
docker volume rm $(docker volume ls -a -q)
Remove networks:
docker network rm $(docker network ls -a -q)