ChatGPT解决这个技术问题 Extra ChatGPT

How to remove old and unused Docker images

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.


m
mit

(original answer see below)

Update Sept. 2016: Docker 1.13: PR 26108 and commit 86de7c0 introduce a few new commands to help facilitate visualizing how much space the docker daemon data is taking on disk and allowing for easily cleaning up "unneeded" excess.

docker system prune will delete ALL dangling data (i.e. In order: containers stopped, volumes without containers and images with no containers). Even unused data, with -a option.

You also have:

docker container prune

docker image prune

docker network prune

docker volume prune

For unused images, use docker image prune -a (for removing dangling and ununsed images).
Warning: 'unused' means "images not referenced by any container": be careful before using -a.

As illustrated in A L's answer, docker system prune --all will remove all unused images not just dangling ones... which can be a bit too much.

Combining docker xxx prune with the --filter option can be a great way to limit the pruning (docker SDK API 1.28 minimum, so docker 17.04+)

The currently supported filters are:

until () - only remove containers, images, and networks created before given timestamp

label (label=, label==, label!=, or label!==) - only remove containers, images, networks, and volumes with (or without, in case label!=... is used) the specified labels.

See "Prune images" for an example.

Original answer (Sep. 2016)

I usually do:

docker rmi $(docker images --filter "dangling=true" -q --no-trunc)

I have an [alias for removing those dangling images: drmi]13

The dangling=true filter finds unused images

That way, any intermediate image no longer referenced by a labelled image is removed.

I do the same first for exited processes (containers)

alias drmae='docker rm $(docker ps -qa --no-trunc --filter "status=exited")'

As haridsv points out in the comments:

Technically, you should first clean up containers before cleaning up images, as this will catch more dangling images and less errors.

Jess Frazelle (jfrazelle) has the bashrc function:

dcleanup(){
    docker rm -v $(docker ps --filter status=exited -q 2>/dev/null) 2>/dev/null
    docker rmi $(docker images --filter dangling=true -q 2>/dev/null) 2>/dev/null
}

To remove old images, and not just "unreferenced-dangling" images, you can consider docker-gc:

A simple Docker container and image garbage collection script.

Containers that exited more than an hour ago are removed. Images that don't belong to any remaining container after that are removed.


Is there documentation on what "dangling=true" really means?
This script cannot remove some images pulled months ago
dcleanup is awesome!
@herm First, docker system prune removes much more than just images. Make sure to use docker image prune instead. And be very careful with -a: a docker system prune -a can have devastating effect (removing volumes as well). Finally, yes, -a removes unused images, I will edit the answer.
@stom : 'unused' means "images not referenced by any container, but dangling means not tagged at all (just an id).
S
Sergey Vyacheslavovich Brunov

Update the second (2017-07-08)

Refer (again) to VonC, using the even more recent system prune. The impatient can skip the prompt with the -f, --force option:

docker system prune -f

The impatient and reckless can additionally remove "unused images not just the dangling ones" with the -a, --all option:

docker system prune -af

https://docs.docker.com/engine/reference/commandline/system_prune/

Update

Refer to VonC's answer which uses the recently added prune commands. Here is the corresponding shell alias convenience:

alias docker-clean=' \
  docker container prune -f ; \
  docker image prune -f ; \
  docker network prune -f ; \
  docker volume prune -f '

Old answer

Delete stopped (exited) containers:

$ docker ps --no-trunc -aqf "status=exited" | xargs docker rm

Delete unused (dangling) images:

$ docker images --no-trunc -aqf "dangling=true" | xargs docker rmi

If you have exercised extreme caution with regard to irrevocable data loss, then you can delete unused (dangling) volumes (v1.9 and up):

$ docker volume ls -qf "dangling=true" | xargs docker volume rm

Here they are in a convenient shell alias:

alias docker-clean=' \
  docker ps --no-trunc -aqf "status=exited" | xargs docker rm ; \
  docker images --no-trunc -aqf "dangling=true" | xargs docker rmi ; \
  docker volume ls -qf "dangling=true" | xargs docker volume rm'

References

docker ps -f

docker rm

docker images -f

docker rmi

Docker v1.9.0 release notes

docker volume ls

docker volume rm


I'd exercise caution with the volume cleanup. Both automatically created container volumes and named volumes that aren't currently in use are listed together with the dangling=true.
@BMitch, you're absolutely correct; I've added a stern warning to the docker volume rm recipe. I'll welcome any suggestions you have.
I'd love docker to give us a different filter option for the named volumes. If I come up with a good workaround, I'll be sure to share.
yes, but unfortunately it doesn't separate the named volume from an anonymous container volume with a simple flag. The command I've been using is docker volume ls -qf dangling=true | egrep '^[a-z0-9]{64}$' | xargs --no-run-if-empty docker volume rm which will work as long as you never name your volumes with something similar to a guid. I may tweak this for the new filter syntax.
Removing unused (dangling) volumes really help us!
Z
Zhao Li

The other answers are great, specifically:

docker system prune # doesn't clean out old images
docker system prune --all # cleans out too much

But I needed something in the middle of the two commands so the filter option was what I needed:

docker image prune --all --filter "until=4320h" # delete images older than 6 months ago; 4320h = 24 hour/day * 30 days/month * 6 months

Hope that helps :)

For reference: https://docs.docker.com/config/pruning/#prune-images


Very underrated answer! Being able to prune with a cutoff date is extremely useful.
W
Wernight

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

+1 For the command to delete old docker images. It's a bit hacky, but the solution is original and works perfectly :)
This is nice however I think this only deletes docker images that are at least 4 months old. .CreatedSince uses weeks as the unit of time in the output even on images that are a lot of weeks old, e.g. 12 weeks.
This worked for me, nice and simple: docker images | grep ' months' | awk '{ print $3 }' | xargs --no-run-if-empty docker rmi -f
L
Lam Le

According to the doc, the following command will delete images older than 48 hours.

$ docker image prune --all --filter until=48h

Using filters is also possible to list every versions before a specified version: docker image ls --all --filter reference=monolito --filter before=monolito:0.1.8 and then apply a rmi command to delete. docker rmi $(docker image ls -q --all --filter reference=monolito --filter before=monolito:0.1.8)
P
Peter Mortensen

Assuming you have Docker 1.13 or higher you can just use the prune commands. For your question specifically for removing old images, you want the first one.

# Remove unused images
docker image prune

# Remove stopped containers.
docker container prune

# Remove unused volumes
docker volume prune

# Remove unused networks
docker network prune

# Command to run all prunes:
docker system prune

I would recommend not getting used to using the docker system prune command. I reckon users will accidentally remove things they don't mean to. Personally, I'm going to mainly be using the docker image prune and docker container prune commands.


you dont want to prune unused networks do you? like, if all containers are stopped, and I delete those networks, how will they containers work if I start them. Do networks get created along with docker run ?
@meffect I completely agree and god spot that I had left network pruning out. I have included that and added a part at the end stating that I wouldn't recommend using docker system prune but the individual prunes.
P
Peter Mortensen

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

M
Mahmoud Zalt

This worked for me:

docker rmi $(docker images | grep "^<none>" | awk "{print $3}")

E
Ell Neal

I recently wrote a script to solve this on one of my servers:

#!/bin/bash

# Remove all the dangling images
DANGLING_IMAGES=$(docker images -qf "dangling=true")
if [[ -n $DANGLING_IMAGES ]]; then
    docker rmi "$DANGLING_IMAGES"
fi

# Get all the images currently in use
USED_IMAGES=($( \
    docker ps -a --format '{{.Image}}' | \
    sort -u | \
    uniq | \
    awk -F ':' '$2{print $1":"$2}!$2{print $1":latest"}' \
))

# Get all the images currently available
ALL_IMAGES=($( \
    docker images --format '{{.Repository}}:{{.Tag}}' | \
    sort -u \
))

# Remove the unused images
for i in "${ALL_IMAGES[@]}"; do
    UNUSED=true
    for j in "${USED_IMAGES[@]}"; do
        if [[ "$i" == "$j" ]]; then
            UNUSED=false
        fi
    done
    if [[ "$UNUSED" == true ]]; then
        docker rmi "$i"
    fi
done

P
Peter Mortensen

Here is a script to clean up Docker images and reclaim the space.

#!/bin/bash -x
## Removing stopped container
docker ps -a | grep Exited | awk '{print $1}' | xargs docker rm

## If you do not want to remove all container you can have filter for days and weeks old like below
#docker ps -a | grep Exited | grep "days ago" | awk '{print $1}' | xargs docker rm
#docker ps -a | grep Exited | grep "weeks ago" | awk '{print $1}' | xargs docker rm

## Removing Dangling images
## There are the layers images which are being created during building a Docker image. This is a great way to recover the spaces used by old and unused layers.

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

## Removing images of perticular pattern For example
## Here I am removing images which has a SNAPSHOT with it.

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

## Removing weeks old images

docker images | grep "weeks ago" | awk '{print $3}' | xargs docker rmi

## Similarly you can remove days, months old images too.

Original script

https://github.com/vishalvsh1/docker-image-cleanup

Usually Docker keeps all temporary files related to image building and layers at

/var/lib/docker

This path is local to the system, usually at THE root partition, "/".

You can mount a bigger disk space and move the content of /var/lib/docker to the new mount location and make a symbolic link.

This way, even if Docker images occupy space, it will not affect your system as it will be using some other mount location.

Original post: Manage Docker images on local disk


P
Peter Mortensen

I'm using this command:

export BEFORE_DATETIME=$(date --date='10 weeks ago' +"%Y-%m-%dT%H:%M:%S.%NZ")
docker images -q | while read IMAGE_ID; do
    export IMAGE_CTIME=$(docker inspect --format='{{.Created}}' --type=image ${IMAGE_ID})
    if [[ "${BEFORE_DATETIME}" > "${IMAGE_CTIME}" ]]; then
        echo "Removing ${IMAGE_ID}, ${BEFORE_DATETIME} is earlier then ${IMAGE_CTIME}"
        docker rmi -f ${IMAGE_ID};
    fi;
done

This will remove all images whose creation time is greater than 10 weeks ago.


I think you swapped IMAGE_CTIME and BEFORE_DATETIME in that echo command
P
Peter Mortensen

If you want to remove images pulled X months ago, you can try the below example which remove images created three months ago:

three_months_old_images=`docker images | grep -vi "<none>" | tr -s ' ' | cut -d" " -f3,4,5,6 | grep "3 months ago" | cut -d" " -f1`
docker rmi $three_months_old_images

This is not correct. This removes images created 3 months ago, not images pulled 3 months ago (if you pull them from a remote source, they can already be 3 months old right away).
This helped me to create more filters, based on different criterias
L
Logovskii Dmitrii

To prune all images and volumes as well
docker system prune -af --volumes


V
Vojtech Vitek

docker system prune -a

(You'll be asked to confirm the command. Use -f to force run, if you know what you're doing.)


This is dangerous, see other comments about docker system prune removing even named volumes with -a.
P
Peter Mortensen

@VonC already gave a very nice answer, but for completeness here is a little script I have been using---and which also nukes any errand Docker processes should you have some:

#!/bin/bash

imgs=$(docker images | awk '/<none>/ { print $3 }')
if [ "${imgs}" != "" ]; then
   echo docker rmi ${imgs}
   docker rmi ${imgs}
else
   echo "No images to remove"
fi

procs=$(docker ps -a -q --no-trunc)
if [ "${procs}" != "" ]; then
   echo docker rm ${procs}
   docker rm ${procs}
else
   echo "No processes to purge"
fi

Works great but can still get Error response from daemon: You cannot remove a running container. Added docker kill $(docker ps -q) before line 3 to address
Why not use $(docker images -q) instead of $(docker images | awk '/<none>/ { print $3 }')?
@SeF: If I do docker images -q I get a vector of image ids, nothing else. If I do what I do I get more -- allowing me to filter on <none> as I do here. Makes sense?
o
ospider

To remove tagged images which have not container running, you will have to use a little script:

#!/bin/bash

# remove not running containers
docker rm $(docker ps -f "status=exited" -q)

declare -A used_images

# collect images which has running container
for image in $(docker ps | awk 'NR>1 {print $2;}'); do
    id=$(docker inspect --format="{{.Id}}" $image);
    used_images[$id]=$image;
done

# loop over images, delete those without a container
for id in $(docker images --no-trunc -q); do
    if [ -z ${used_images[$id]} ]; then
        echo "images is NOT in use: $id"
        docker rmi $id
    else
        echo "images is in use:     ${used_images[$id]}"
    fi
done

A
Antony.H

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; }')


g
gaoithe

How to remove a tagged image

docker rmi the tag first docker rmi the image. # that can be done in one docker rmi call e.g.: # docker rmi

(this works Nov 2016, Docker version 1.12.2)

e.g.

$ docker images 
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
usrxx/the-application   16112805            011fd5bf45a2        12 hours ago        5.753 GB
usryy/the-application   vx.xx.xx            5af809583b9c        3 days ago          5.743 GB
usrzz/the-application   vx.xx.xx            eef00ce9b81f        10 days ago         5.747 GB
usrAA/the-application   vx.xx.xx            422ba91c71bb        3 weeks ago         5.722 GB
usrBB/the-application   v1.00.18            a877aec95006        3 months ago        5.589 GB

$ docker rmi usrxx/the-application:16112805 && docker rmi 011fd5bf45a2
$ docker rmi usryy/the-application:vx.xx.xx && docker rmi 5af809583b9c
$ docker rmi usrzz/the-application:vx.xx.xx eef00ce9b81f
$ docker rmi usrAA/the-application:vx.xx.xx 422ba91c71bb
$ docker rmi usrBB/the-application:v1.00.18 a877aec95006

e.g. Scripted remove anything older than 2 weeks.

IMAGESINFO=$(docker images --no-trunc --format '{{.ID}} {{.Repository}} {{.Tag}} {{.CreatedSince}}' |grep -E " (weeks|months|years)")
TAGS=$(echo "$IMAGESINFO" | awk '{ print $2 ":" $3 }' )
IDS=$(echo "$IMAGESINFO" | awk '{ print $1 }' )
echo remove old images TAGS=$TAGS IDS=$IDS
for t in $TAGS; do docker rmi $t; done
for i in $IDS; do docker rmi $i; done

H
Hamit YILDIRIM
docker rm $(docker ps -faq)
docker rmi $(docker ps -faq)

-f force

-a all

-q in the mode


r
rubo77

Occasionally I have run into issues where Docker will allocate and continue to use disk space, even when the space is not allocated to any particular image or existing container. The latest way I generated this issue accidentally was using "docker-engine" centos build instead of "docker" in RHEL 7.1. What seems to happen is sometimes the container clean-ups are not completed successfully and then the space is never reused. When the 80GB drive I allocated as / was filled with /var/lib/docker files I had to come up with a creative way to resolve the issue.

Here is what I came up with. First to resolve the disk full error:

Stop docker: systemctl stop docker Allocated a new drive mounted as say /mnt/docker . Move all the files in /var/lib/docker to /mnt/docker . I used the command: rsync -aPHSx --remove-source-files /var/lib/docker/ /mnt/docker/ Mount the new drive to /var/lib/docker.

At this point I no longer had a disk full error, but I was still wasting a huge amount of space. The next steps are to take care of that.

Start Docker: systemctl start docker Save the all the images: docker save $(docker images |sed -e '/^/d' -e '/^REPOSITORY/d' -e 's,[ ][ ]*,:,' -e 's,[ ].*,,') > /root/docker.img Uninstall docker. Erase everything in /var/lib/docker: rm -rf /var/lib/docker/[cdintv]* Reinstall docker Enable docker: systemctl enable docker Start docker: systemctl start docker Restore images: docker load < /root/docker.img Start any persistent containers you need running.

This dropped my disk usage from 67 GB for docker to 6 GB for docker.

I do not recommend this for everyday use. But it is useful to run when it looks like docker has lost track of used disk space do to software errors, or unexpected reboots.


Didn't you forget to mention to unmount /mnt/docker ?
m
mainframer
docker rm `docker ps -aq`

or

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

I think this answer is dangerous because those commands remove containers. Firstly, OP was asking how to remove images, not containers. And more importantly, those commands may cause to loss data due to people may have some valuable data in exited containers.
You should describe potentially unwanted results of applying these commands on production server.
this removes containers, not images.
P
Peter Mortensen

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


L
Le Khiem

First, run docker images to see list of images and copy IMAGE HASH ID into clipboard.

Run docker rmi -f <Image>

Remember option -f is force deleting.


m
mirekphd

If you build these pruned images yourself (from some other, older base images) please be careful with the accepted solutions above based on docker image prune, as the command is blunt and will try to remove also all dependencies required by your latest images (the command should be probably renamed to docker image*s* prune).

The solution I came up for my docker image build pipelines (where there are daily builds and tags=dates are in the YYYYMMDD format) is this:

# carefully narrow down the image to be deleted (to avoid removing useful static stuff like base images)
my_deleted_image=mirekphd/ml-cpu-py37-vsc-cust

# define the monitored image (tested for obsolescence), which will be usually the same as deleted one, unless deleting some very infrequently built image which requires a separate "clock"
monitored_image=mirekphd/ml-cache

# calculate the oldest acceptable tag (date)
date_week_ago=$(date -d "last week" '+%Y%m%d')

# get the IDs of obsolete tags of our deleted image
# note we use monitored_image to test for obsolescence
my_deleted_image_obsolete_tag_ids=$(docker images --filter="before=$monitored_image:$date_week_ago" | grep $my_deleted_image | awk '{print $3}')

# remove the obsolete tags of the deleted image
# (note it typically has to be forced using -f switch)
docker rmi -f $my_deleted_image_obsolete_tag_ids

A
Alexey Melezhik

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.


P
Philip Mutua

If you have a lot of them, it can be really tedious to remove them, but lucky for us Docker has a few commands to help us eliminate dangling images. In older versions of Docker (and this still works today), you can delete dangling images on their own by running docker rmi -f $(docker images -f "dangling=true" -q) .


B
Bhargav Panth

I usually do docker rm -f $(docker ps -a -q) and docker system prune to purge all dangling containers.


y
yash bhangare

The below command will help to remove all unused and old images from local repository ==> docker system prune --all