SoFunction
Updated on 2025-04-09

How to delete dangling images by docker

docker deletes hanging images

Preface

Since the company has just performed front-end separation operations and used docker to operate and maintain deployment, the deployment efficiency has been greatly improved.

but:

Since the official environment cannot directly link the Internet and the company's docker hub, it can only be used at a timedocker save + docker load, after a long time, I found that there are a large number of old images in the local server, which is very unfavorable for our export, so I wanted to write a command to delete those unused historical images. The content of this article comes from this.

Hanging mirror image

During the build image process, we may generate some temporaryDon't have a tagOrThat is, there is no tag or name mirrorTheir names are usually<none>

Docker official method

$ docker rmi $(docker images -f "dangling=true" -q)
  • -f:expressfilter,filter
  • -q: means that only the mirror ID is displayed;

Customization method

Delete the mirror through a pipeline, for example:

$ docker images | grep none | awk 'BEGIN{ print "docker rmi \\" } {imageId=$3; print imageId" \\" } END{ print "" }' | sh

The meaning of this command is to first execute the docker image command, and then filter out the included ones in the query results.noneThe data of the matched results are finally printed firstdocker rmi \, then print the third column of the data and add it to each row\String, at the end of the matching result,

The results after execution are as follows:

$ docker images | grep none | awk 'BEGIN{ print "docker rmi \\" } {imageId=$3; print imageId" \\" } END{ print "" }' 
docker rmi \
6dbd6f7d4324 \
ded29c866e3e \
6a2d7905d3fa \
fa539c873a18 \
1fa6ec26a6d9 \

Hahahaha, I can say this command is purely for learningawkThere are actually a slightly simpler way to use it, such as:

$ docker rmi $(docker images | grep none | awk '{print $3}')

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.