How do I remove docker ES1en images and none invalid images

  • 2020-06-23 02:28:02
  • OfStack

background

We all built docker registry on the server, and all the build images would be concentrated on one server, and over time the hard disk would fill up. This article focuses on two cleaning methods. 1 is < none > The tag mirror, 2 is to remove the docker physical mirror. Without further ado, let's take a look at the details.

docker < none > The mirror

Valid none mirror

In order to understand < none > Mirroring, we need to understand how the Docker mirroring system works and how layers is organized.

Run when I pull an stresser image docker images -a Command, and you'll see that I have one more out of thin air < none > : < none > The mirror.


➜ docker images -a
REPOSITORY  TAG   IMAGE ID  CREATED  SIZE
stresser   latest  68ee9b96793e 9 days ago  242MB
<none>   <none>  dbcff8952263 9 days ago  242MB

When I want to delete this < none > When mirroring, execute the following command, will prompt failure, I have to delete the stresser image to get rid of it < none > Image deletion.


➜ docker rmi dbcff8952263
Error response from daemon: conflict: unable to delete dbcff8952263 (cannot be forced) - image has dependent child images

➜ docker rmi 68ee9b96793e
Untagged: stresser:latest
Deleted: sha256:68ee9b96793e0a3b3a77ec713f1bf4eb19446bd13fb933557dc401e452ca04c4
Deleted: sha256:dbcff895226371eba2640c178414f5828aa5e6f417978b63ffa490d3865dc79a
Deleted: sha256:875a0b6d28d1f52fc980a0948055d3ec3a38158ff7aa6a1a2c19c4243b96a57a

➜ docker rmi dbcff8952263
Error response from daemon: No such image: dbcff8952263:latest

< none > : < none > What does a mirror image do? Let's look at Docker file system's composition, docker mirror is made up of many layers, each has a parent-child relationship between layer, all docker default are stored in the file system layer/var lib docker/graph directory, docker called the database layer, in this case stresser is composed of two layers (layer), we can in/var/lib/docker/find layer graph directory.

When we pull stresser When mirroring, the first download is dbcff8952263 < none > Parent layer, stresser:latest,stresser consists of two layers. We can enter the cat/car/lib docker/graph / ${containerId} / json, view image metadata to know detailed information (sample is other container information below)


{"id":"37dd4150474449629e8a7b576eed26cb8583d2fe5a3edf10fd84323dfd538678","parent":"5cf74bcb1bde2e2249824a682f45235954543a5d57081db22c96402342db49e9","created":"2017-04-06T16:28:35.51523979Z","container_config":{"Hostname":"","Domainname":"","User":"","Memory":0,"MemorySwap":0,"CpuShares":0,"Cpuset":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"PortSpecs":null,"ExposedPorts":null,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":["/bin/sh -c set -e; \u0009NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; \u0009found=''; \u0009for server in \u0009\u0009ha.pool.sks-keyservers.net \u0009\u0009hkp://keyserver.ubuntu.com:80 \u0009\u0009hkp://p80.pool.sks-keyservers.net:80 \u0009\u0009pgp.mit.edu \u0009; do \u0009\u0009echo \"Fetching GPG key $NGINX_GPGKEY from $server\"; \u0009\u0009apt-key adv --keyserver \"$server\" --keyserver-options timeout=10 --recv-keys \"$NGINX_GPGKEY\" \u0026\u0026 found=yes \u0026\u0026 break; \u0009done; \u0009test -z \"$found\" \u0026\u0026 echo \u003e\u00262 \"error: failed to fetch GPG key $NGINX_GPGKEY\" \u0026\u0026 exit 1; \u0009exit 0"],"Image":"","Volumes":null,"WorkingDir":"","Entrypoint":null,"NetworkDisabled":false,"MacAddress":"","OnBuild":null,"Labels":null},"author":"NGINX Docker Maintainers \"docker-maint@nginx.com\"","Size":4901}

Mirroring is a kind of intermediate mirror, we can use 'docker images-ES96en' to see that they do not cause disk space occupancy problems (since this is the parent layer of the mirror, which must exist), but can confuse our judgment.

Invalid none mirror

Another type < none > : < none > The mirror is dangling images, a type that causes disk space footprint problems.

Programming languages such as Java and Golang have a memory block that does not associate any code. The garbage collection systems of these languages reclaim this area first, returning it to heap memory, so this area of memory is useful for later memory allocations

docker's suspension (dangling) file system is similar to the above principle, it is not used and does not associate any images, so we need a mechanism to clean up these dangling images.

We have mentioned above that it works < none > Mirror images, they are a kind of middle layer, that doesn't work < none > How does a mirror image come into being? These dangling images are primarily our triggers docker build and docker pull Produced by command.

Use an example to explain:

Suppose we want to build a new image, the Dockerfile file is as follows


FORM Ubuntu:latest
RUN echo 'hello world'

When we construct docker build -t hello-world ./ , a new image will be generated.

However, a month later, Ubuntu released a new image, at which time we built another IMAGE of hello world, relying on the latest Ubuntu

The problem is, we refer to the old ES148en-ES149en image, which will become the dangling image without tags! Use the following command to clean up


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

docker does not have an automatic garbage collection mechanism, which could be improved in the future, but for now we will have to do it manually (just write a script).

Delete the image of the local hard disk

When there are a lot of our registry server tag label image, but not enough disk space and use it every time, we would want to delete the memory image to the server room, registry comes with API interface delete image, but even if we call, he is only a logic level to delete, soft delete (soft delete), just put into the system and the relationship between the mirror to remove 2, mirror 1 straight existed in our hard disk, we need 1 way completely delete physical memory space. There is a third party solution online: delete-docker-registry-image , then list the steps to take before stopping the registry service.

Installation:


curl https://raw.githubusercontent.com/burnettk/delete-docker-registry-image/master/delete_docker_registry_image.py | sudo tee /usr/local/bin/delete_docker_registry_image >/dev/null
sudo chmod a+x /usr/local/bin/delete_docker_registry_image

Setting environment variable: Data storage address:

The configuration of registry on my server is as follows:


 volumes:
 - /root/docker-data/registry/data:/var/lib/registry

root@iZ94ft8hgzqZ:~/docker-data/registry/data/docker/registry/v2/repositories# ls -al
total 108
drwxr-xr-x 27 root root 4096 Aug 21 18:29 .
drwxr-xr-x 4 root root 4096 Oct 30 2015 ..
drwxr-xr-x 5 root root 4096 Mar 27 2016 example
drwxr-xr-x 5 root root 4096 Mar 20 19:25 gitlab_ansible
drwxr-xr-x 5 root root 4096 Mar 22 2016 hlpays-job
drwxr-xr-x 5 root root 4096 Jan 26 2016 hlpays-oa
drwxr-xr-x 5 root root 4096 Mar 24 2016 hlpays-portal
drwxr-xr-x 5 root root 4096 Mar 27 18:32 ifex-crm

export REGISTRY_DATA_DIR=/root/docker-data/registry/data/docker/registry/v2

Logical deletion:

delete_docker_registry_image �image tickets �dry-run ; It's just a logical deletion. It doesn't help. Just demo 1...


root@iZ94ft8hgzqZ:~/docker-data/registry/data/docker/registry/v2/repositories# delete_docker_registry_image --image tickets --dry-run


INFO [2017-09-13 18:21:04,505] DRY_RUN: would have deleted /root/docker-data/registry/data/docker/registry/v2/blobs/sha256/27/27dedd9200ff607e76eb9d0e10beb103f53551e4ed39829d767cfbc208b79581
INFO [2017-09-13 18:21:04,506] DRY_RUN: would have deleted /root/docker-data/registry/data/docker/registry/v2/blobs/sha256/1b/1b2aade332a7133b1a03cae7695a3dcf9413dd017ff41f35a1bb1506becbacf3

Physical data deletion:

Delete the physical memory and you'll see that the hard drive is empty.


➜ docker rmi dbcff8952263
Error response from daemon: conflict: unable to delete dbcff8952263 (cannot be forced) - image has dependent child images

➜ docker rmi 68ee9b96793e
Untagged: stresser:latest
Deleted: sha256:68ee9b96793e0a3b3a77ec713f1bf4eb19446bd13fb933557dc401e452ca04c4
Deleted: sha256:dbcff895226371eba2640c178414f5828aa5e6f417978b63ffa490d3865dc79a
Deleted: sha256:875a0b6d28d1f52fc980a0948055d3ec3a38158ff7aa6a1a2c19c4243b96a57a

➜ docker rmi dbcff8952263
Error response from daemon: No such image: dbcff8952263:latest
0

Delete specified label:


➜ docker rmi dbcff8952263
Error response from daemon: conflict: unable to delete dbcff8952263 (cannot be forced) - image has dependent child images

➜ docker rmi 68ee9b96793e
Untagged: stresser:latest
Deleted: sha256:68ee9b96793e0a3b3a77ec713f1bf4eb19446bd13fb933557dc401e452ca04c4
Deleted: sha256:dbcff895226371eba2640c178414f5828aa5e6f417978b63ffa490d3865dc79a
Deleted: sha256:875a0b6d28d1f52fc980a0948055d3ec3a38158ff7aa6a1a2c19c4243b96a57a

➜ docker rmi dbcff8952263
Error response from daemon: No such image: dbcff8952263:latest
1

For other usage, please refer to the official website

conclusion


Related articles: