How to remove old and unused Docker images

前端 未结 26 1830
北恋
北恋 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: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

提交回复
热议问题