1. Overall analysis

For Docker, there are images, containers, volumes, and networks. Therefore, corresponding objects are produced, which take up disk space. When these objects are not in use, they need to be cleaned up, that is, garbage cleaned, in order not to occupy additional disk space. After Docker 1.13, the Prune command for various objects is provided, as well as the Docker System prune command for cleaning up all object types. But prior to Docker 1.13, you needed to provide other ways to do garbage cleaning.

2. Garbage removal

2.1 Garbage cleaning after Docker V1.13

2.1.1 container

When a container is stopped, the system does not know that the container is removed unless the -rm field is set when the container is run. Docker Container Prune can delete stopped containers that still occupy storage space on the disk.


     
  1. $ docker container prune

  2. WARNING! This will remove all stopped containers.

  3. Are you sure you want to continue? [y/N] y

Copy the code

When you run this command, you are prompted whether to continue by default. If the -f or -force field is specified in the command, all stopped containers are deleted. By default, this command deletes all stopped containers. You can also set the -filter field to filter the containers to be deleted. For example, the following command only deletes containers that have been stopped for more than 24 hours.


     
  1. $ docker container prune --filter "until=24h"

Copy the code

2.1.2 mirror

By running the Docker images prune command, you can remove all unwanted images. By default, this command deletes only the images in the dangling state. An image in the dangling state is an image that has not been tagged or referenced by any container.


     
  1. $ docker image prune

  2. WARNING! This will remove all dangling images.

  3. Are you sure you want to continue? [y/N] y

Copy the code

To remove all unused mirrors, set the -a field:


     
  1. $ docker image prune -a

  2. WARNING! This will remove all images without at least one container associated to them.

  3. Are you sure you want to continue? [y/N] y

Copy the code

When you run this command, you are prompted whether to continue by default. If the -f or -force field is specified in the command, the system deletes it directly. You can set the -filter field to filter the image to be deleted. For example, the following command only deletes images that have been stopped for more than 24 hours.


     
  1. $ docker image prune -a --filter "until=24h"

Copy the code

2.1.3 storage volume

A storage volume can be used by one or more containers or occupies disk space. To preserve data, volumes are never automatically deleted.


     
  1. $ docker volume prune

  2. WARNING! This will remove all volumes not used by at least one container.

  3. Are you sure you want to continue? [y/N] y

Copy the code

When you run this command, you are prompted whether to continue by default. If the -f or -force field is specified in the command, the system deletes it directly. By default, this command deletes all unused volumes. You can also set the -filter field to filter the volumes to be deleted. Example For example, the following command only deletes the storage volume whose label value is keep.


     
  1. $ docker volume prune --filter "label! =keep"

Copy the code

2.1.4 network

Docker networks do not occupy disk space, but create iptables rules, bridge network devices, and routing tables. Therefore, when it comes to how to stop using these resources, you should clean them up.


     
  1. $ docker network prune

  2. WARNING! This will remove all networks not used by at least one container.

  3. Are you sure you want to continue? [y/N] y

Copy the code

When you run this command, you are prompted whether to continue by default. If the -f or -force field is specified in the command, the system deletes it directly. By default, this command deletes all unused networks. You can also set the -filter field to filter the networks to be deleted. For example, the following command is only for networks that have been used for more than 24 hours.


     
  1. $ docker network prune --filter "until=24h"

Copy the code

2.1.5 Deleting All Objects

The Docker system prune command can quickly delete all unused objects, including images, containers, networks, and storage volumes. Prior to Docker 17.06.0, volumes were cleaned at the same time. After Docker 17.06.1, you need to set the -volumes field to clean up the storage volumes.


     
  1. $ docker system prune

  2. WARNING! This will remove:

  3.        - all stopped containers

  4.        - all networks not used by at least one container

  5.        - all dangling images

  6.        - all build cache

  7. Are you sure you want to continue? [y/N] y

Copy the code

If docker versions later than docker 17.06.1 are used, you need to add the -Volumes field after the command to clean up the contents of the storage volumes.


     
  1. $ docker system prune --volumes

  2. WARNING! This will remove:

  3.        - all stopped containers

  4.        - all networks not used by at least one container

  5.        - all volumes not used by at least one container

  6.        - all dangling images

  7.        - all build cache

  8. Are you sure you want to continue? [y/N] y

Copy the code

2.2 Docker garbage cleanup prior to v1.13

2.2.1 container

When a container is stopped, the system does not know that the container is removed unless the -rm field is set when the container is run. Stopped containers still occupy disk storage space. Docker RM can delete these stopped containers. All stopped containers can be cleared by using the following command.


     
  1. $ docker rm $(docker ps -a -q)

Copy the code

2.2.2 mirror

By running the docker rmi command, you can remove all unused images. Generally, only the image in the dangling state is deleted. An image in the dangling state is an image that has not been tagged or referenced by any container.


     
  1. $ docker rmi $(docker images -q -f "dangling=true")

Copy the code

2.2.3 storage volume

A storage volume can be used by one or more containers or occupies disk space. To preserve data, volumes are never deleted automatically.


     
  1. $ docker volume rm $(docker volume ls -q -f dangling=true)

Copy the code

The resources

1. The docker container prune address: https://docs.docker.com/engine/reference/commandline/container_prune/

2. The Prune unused Docker objects address: https://docs.docker.com/config/pruning/

3. The docker image prune address: https://docs.docker.com/engine/reference/commandline/image_prune/

4. The docker volume prune address: https://docs.docker.com/engine/reference/commandline/volume_prune/

5. The docker network prune address: https://docs.docker.com/engine/reference/commandline/network_prune/

About author: Ji Xiangyuan, Beijing Shenzhou Space Software Technology Co., LTD. The copyright of this article belongs to the author.

Recommended reading

  • Locate and troubleshoot Kubernetes application deployment problems

  • Recommend 30 top tools for microservices

  • 6 myths and 8 True scenarios about containers

  • 100 containers are recommended for surrounding items

  • K8S deploys MySQL high availability solution