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:32

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.

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

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
查看更多
浮光初槿花落
4楼-- · 2018-12-31 23:36

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
查看更多
高级女魔头
5楼-- · 2018-12-31 23:40

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

查看更多
ら面具成の殇う
6楼-- · 2018-12-31 23:40

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:

  1. Stop docker: systemctl stop docker
  2. Allocated a new drive mounted as say /mnt/docker .
  3. 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/
  4. 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.

  1. Start Docker: systemctl start docker

  2. Save the all the images: docker save $(docker images |sed -e '/^/d' -e '/^REPOSITORY/d' -e 's,[ ][ ],:,' -e 's,[ ].,,') > /root/docker.img

  3. Uninstall docker.

  4. Erase everything in /var/lib/docker: rm -rf /var/lib/docker/[cdintv]*

  5. Reinstall docker

  6. Enable docker: systemctl enable docker

  7. Start docker: systemctl start docker

  8. Restore images: docker load < /root/docker.img

  9. 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.

查看更多
路过你的时光
7楼-- · 2018-12-31 23:42

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

查看更多
登录 后发表回答