preface

In the practice of Docker, if you do not pay attention to docker’s occupation of disk, you may find that docker has eaten up disk space a little bit one day. How to clean up docker images, containers and data volumes

Delete all unused or idle images, containers, data volumes, and networks

Docker provides a command to clean up any dangling resources (images, containers, data volumes, and networks).

docker system prune
Copy the code

If you want to remove any stopped containers and unused containers (more than dangling images), you can add the -a option to the command.

docker system prune -a
Copy the code

Remove the mirror

Deletes one or more specified mirrors

First use a combination of Docker images and the -A option to find the image you want to remove. This command lists all mirrors, including the middle mirror layer. When the image you want to delete is located, specify the image ID or tag and use docker RMI to delete the image.

1. Find the mirror list

docker images -a
Copy the code

2, remove,

docker rmi Image Image
Copy the code

Example Delete an idle mirror

Docker images consist of multiple image layers. A dangling mirror is a mirror layer that is no longer associated with any tagged mirror but still occupies disk space. Docker images -f dangling=true was used to see all dangling images. After confirmation, delete all the suspended images using the Docker Image Prune.

1. Find all hanging mirrors

docker images -f dangling=true

Copy the code

2. Delete the mirror

docker image prune

Copy the code

Delete a mirror using pattern matching

You can also use a combination of docker images and grep to find matching images. If the results match your expectations, you can pass the image’s ID as an argument to the Docker RMI command via awk. It is worth noting that these commands are not provided by Docker, so they are not guaranteed to work on all systems.

1. Find the mirror

docker images -a |  grep "pattern"

Copy the code

2, remove,

docker images -a | grep "some pattern" | awk '{print $3}' | xargs docker rmi
Copy the code

Deleting all Mirrors

Use Docker images-a to list all the images that exist on the system. If it is determined that you want to delete all mirrors, you can use the -q option to pass the ID of the mirrors as an argument to the Docker RMI command.

1. List all mirrors

docker images -a
Copy the code

2, remove,

docker rmi $(docker images -a -q)
Copy the code

Remove the container

Deletes one or more specified containers

First use docker ps -A to output all containers. We can then find the ID or name of the container we want to delete in the output container list.

1. Find all the containers

docker ps -a
Copy the code

2, remove,

docker rm ID_or_Name ID_or_Name
Copy the code

Delete the container on exit

If you know when the container is created that it doesn’t need to be kept after use, you can start it with docker run –rm. When started this way, the container will be deleted automatically when it exits.

docker run --rm image_name

Copy the code

Delete all exiting containers

A container can be found by docker ps-a. In addition, with the -f option, we can specify filtering conditions. For example, we can filter by the container’s running state (created, Restarting, RUNNING, paused, exited). If we want to see exited containers, we can filter by -f status=exited. After the second confirmation, you can use the -q option to pass the container ID to the Docker rm command for deletion.

1. Find the exit container

docker ps -a -f status=exited
Copy the code

2, remove,

docker rm $(docker ps -a -f status=exited -q)
Copy the code

Use multiple filters to remove containers

We can also use filters multiple times in the command. The output container list only needs to satisfy any of the filters. For example, if we want to remove containers whose status is created (created when the container is run with an invalid command) or exited, we can specify two filters:

1. Use a filter to find the list of containers

docker ps -a -f status=exited -f status=created

Copy the code

2, remove,

docker rm $(docker ps -a -f status=exited -f status=created -q)

Copy the code

Delete containers based on pattern matches

Similar to deleting images, we can find matching containers by combining docker ps and grep. If it is determined to be the container we want to delete, we can use awk and xargs to pass the container ID to the Docker rm command. It is worth noting that these commands are not provided by Docker itself, so they are not guaranteed to work on all systems.

1. Find a list of matching containers

docker ps -a | grep "pattern"
Copy the code

2, remove,

docker ps -a | grep "pattern" | awk '{print $1}' | xargs docker rm 
Copy the code

Stop and delete all containers

First use docker ps -A to output all containers. After the second confirmation, the container ID can be passed to docker Stop and Docker RM through the -q option to stop and delete all containers.

1. Find all the containers

docker ps -a
Copy the code

2, remove,

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
Copy the code

Deleting a Data Volume

Deletes one or more specified data volumes

Use the docker volume ls command to find the name of the data volume to delete. Then use docker Volume RM to delete one or more data volumes.

1. Locate the data volume to be deleted from the data volume list

docker volume ls
Copy the code

2, remove,

docker volume rm volume_name volume_name
Copy the code

Example Delete an unattached data volume

Because the data volume is independent from the container, the data volume is not automatically removed when the container is removed. When a data volume exists but is no longer connected to any container, it is called a pending data volume. Before deleting an dangling data volume, run the docker volume ls -f dangling=true command. Docker Volume Prune can be used to delete all data volumes if they are correct:

1. Locate the data volume to be deleted from the data volume list

docker volume ls -f dangling=true
Copy the code

2, remove,

docker volume prune
Copy the code

Delete the container and its data volumes

If you create an unnamed data volume, you can remove the associated unnamed data volume by setting the -v option when deleting the corresponding container. It is worth noting that this approach only applies to unnamed data volumes. When the container is deleted successfully, the ID of the deleted container is displayed, and the data volume is deleted without any indication. If the data volume is unnamed, it is automatically deleted silently. If the data volume is named, it is automatically retained in the system.

docker rm -v container_name
Copy the code

conclusion

This article introduces several common commands related to image, container and data volume deletion for docker operation and maintenance management to refer to. Docker System Prune, Docker RMI, Docker RM and Docker Volume RM can be found in the docker official documentation for more detailed introduction.

reference

  • How To Remove Docker Images, Containers, and Volumes

Pay attention to our