How to remove old and unused Docker images

前端 未结 26 1348
北恋
北恋 2020-11-22 11:28

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 additio

相关标签:
26条回答
  • 2020-11-22 12:16

    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.

    0 讨论(0)
  • 2020-11-22 12:17

    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
    0 讨论(0)
  • 2020-11-22 12:17

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

    0 讨论(0)
  • 2020-11-22 12:18

    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
    
    0 讨论(0)
  • 2020-11-22 12:19

    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
    
    0 讨论(0)
  • 2020-11-22 12:21

    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

    0 讨论(0)
提交回复
热议问题