Kubernetes-docker garbage cleaning

1. Overall analysis

For Docker, there are objects such as images, containers, storage volumes, and networks . Therefore, these corresponding objects will be produced, and these objects will occupy disk space. When these objects are not being used, in order not to occupy additional disk space, these objects need to be cleaned up, that is, garbage clean up. After docker version 1.13, prune commands for various objects are provided, and docker system prune commands to clean up all object types are also provided . But in versions before docker 1.13, you need to provide other ways to clean up garbage.

2. Garbage cleaning

2.1 Garbage cleanup after docker v1.13

2.1.1 Container

When the container is stopped, the system will not know to delete the container unless the --rm field is set when the container is running . The stopped containers will still occupy the storage space of the disk. These stopped containers can be deleted through docker container prune .

$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y

When executing this command, it will prompt whether to continue by default. If the -f or -force field is set when executing the command, all stopped containers will be deleted directly. By default, all stopped containers will be deleted when this command is executed. You can also filter the containers to be deleted by setting the –filter field. For example, the following command only deletes containers that have been stopped for more than 24 hours.

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

2.1.2 Mirror

By executing the docker images prune command, you can clear all images that are no longer in use. By default, this command only clears the images whose status is dangling . Mirrors whose status is dangling are untagged and not referenced by any container.

$ docker image prune

WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y

If you want to remove all unused images, you can do this by setting the -a field:

$ docker image prune -a

WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y

When executing this command, it will prompt whether to continue by default. If the -f or -force field is set when the command is executed, the delete operation will be performed directly. You can filter the images to be deleted by setting the -filter field. For example, the following command only deletes images that have been stopped for more than 24 hours.

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

2.1.3 Storage volume

The storage volume can be used by one or more containers, and it also takes up disk space. In order to maintain data, storage volumes are never automatically deleted.

$ docker volume prune

WARNING! This will remove all volumes not used by at least one container.
Are you sure you want to continue? [y/N] y

When executing this command, it will prompt whether to continue by default. If the -f or -force field is set when the command is executed, the delete operation will be performed directly. By default, all unused storage volumes will be deleted when this command is executed. You can also filter the storage volumes to be deleted by setting the –filter field. For example, the following command only deletes the storage volume whose label value is keep.

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

2.1.4 Network

The docker network does not occupy disk space, but creates iptables rules, bridge network devices and routing tables. Therefore, when these resources are no longer used, they should be cleaned up.

$ docker network prune

WARNING! This will remove all networks not used by at least one container.
Are you sure you want to continue? [y/N] y

When executing this command, it will prompt whether to continue by default. If the -f or -force field is set when the command is executed, the delete operation will be performed directly. By default, all unused networks will be deleted when this command is executed. You can also filter the networks to be deleted by setting the -filter field. For example, the following command is only for networks that have been used for more than 24 hours.

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

2.1.5 Delete all objects

The docker system prune command can quickly delete all unused objects, including images, containers, networks, and storage volumes. Before docker 17.06.0, storage volumes will be cleaned up at the same time. After docker 17.06.1, you need to set the –volumes field to clean up storage volumes at the same time.

$ docker system prune

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all dangling images
        - all build cache
Are you sure you want to continue? [y/N] y

If you are using a version of docker after 17.06.1, you need to add the –volumes field after the command to clean up the contents of the storage volume.

$ docker system prune --volumes

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all volumes not used by at least one container
        - all dangling images
        - all build cache
Are you sure you want to continue? [y/N] y

2.2 Garbage cleaning before docker v1.13

2.2.1 Container

When the container is stopped, the system will not know to delete the container unless the --rm field is set when the container is running . The stopped containers will still occupy the storage space of the disk, and these stopped containers can be deleted through docker rm . The following command can clear all stopped containers.

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

2.2.2 Mirror

By executing the docker rmi command, all unused mirrors can be cleared. Generally, only mirrors whose status is dangling are cleared . Mirrors whose status is dangling are untagged and not referenced by any container.

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

2.2.3 Storage volume

The storage volume can be used by one or more containers, and it also takes up disk space. In order to preserve data, storage volumes are never automatically deleted.

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

 

Reference

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

2.《Prune unused Docker objects》地址:https://docs.docker.com/config/pruning/

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

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

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

Guess you like

Origin blog.csdn.net/youligg/article/details/111830405