How to remove old and unused Docker images

2018-12-31 23:12发布

When running Docker for a long time, there are a lot of images in system. How can I remove all unused Docker images at once safety to free up the storage?

In addition, I also want to remove images pulled months ago, which have the correct TAG.

So, I'm not asking for removing untagged images only. I'm searching for a way to remove general unused images, which includes both untagged and other images such as pulled months ago with correct TAG.

标签: docker
20条回答
荒废的爱情
2楼-- · 2018-12-31 23:26

Remove old containers weeks ago.

docker rm $(docker ps -a | grep "weeks" | awk '{ print $1; }')

Remove old images weeks ago. Be careful. This will remove base images which was created weeks ago but which your new images might be using.

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

查看更多
呛了眼睛熬了心
3楼-- · 2018-12-31 23:27

There is sparrow plugin docker-remove-dangling-images you can use to clean up stopped containers and unused (dangling) images:

$ sparrow plg run docker-remove-dangling-images

It works both for Linux and Windows OS.

查看更多
长期被迫恋爱
4楼-- · 2018-12-31 23:29

To remove old tagged images that are more than a month old:

$ docker images --no-trunc --format '{{.ID}} {{.CreatedSince}}' \
    | grep ' months' | awk '{ print $1 }' \
    | xargs --no-run-if-empty docker rmi

Note that it'll fail to remove images that are used by a container, referenced in a repository, has dependent child images... which is probably what you want. Else just add -f flag.

Example of /etc/cron.daily/docker-gc script:

#!/bin/sh -e

# Delete all stopped containers (including data-only containers).
docker ps -a -q --no-trunc --filter "status=exited" | xargs --no-run-if-empty docker rm -v

# Delete all tagged images more than a month old
# (will fail to remove images still used).
docker images --no-trunc --format '{{.ID}} {{.CreatedSince}}' | grep ' months' | awk '{ print $1 }' | xargs --no-run-if-empty docker rmi || true

# Delete all 'untagged/dangling' (<none>) images
# Those are used for Docker caching mechanism.
docker images -q --no-trunc --filter dangling=true | xargs --no-run-if-empty docker rmi

# Delete all dangling volumes.
docker volume ls -qf dangling=true | xargs --no-run-if-empty docker volume rm
查看更多
情到深处是孤独
5楼-- · 2018-12-31 23:29

Until now (Docker version 1.12) we are using the following command to delete all the running containers. Also, if we want to delete the volumes, we can do that manually using its respective tag -v in the following command.

Delete all Exited Containers

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

Delete all Stopped Containers

docker rm $(docker ps -a -q)

Delete All Running and Stopped Containers

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

Remove all containers, without any criteria

docker container rm $(docker container ps -aq)

But, in version 1.13 and above, for complete system and cleanup, we can directly user the following command:

docker system prune

All unused containers, images, networks and volumes will get deleted. We can also do this using the following commands that clean up the individual components:

docker container prune
docker image prune
docker network prune
docker volume prune
查看更多
与风俱净
6楼-- · 2018-12-31 23:29
docker rm `docker ps -aq`

or

docker rm $(docker ps -q -f status=exited)
查看更多
素衣白纱
7楼-- · 2018-12-31 23:31

If you wish to automatically/periodically clean up exited containers and remove images and volumes that aren't in use by a running container you can download the image meltwater/docker-cleanup.

Just run:

docker run -d -v /var/run/docker.sock:/var/run/docker.sock:rw  -v /var/lib/docker:/var/lib/docker:rw --restart=unless-stopped meltwater/docker-cleanup:latest

It runs every 30 minutes by default. You can however set the delay time by using this flag in seconds (DELAY_TIME=1800 option).

More details: https://github.com/meltwater/docker-cleanup/blob/master/README.md

查看更多
登录 后发表回答