4

如何快速刪除所有已經無用的 Docker 資源 (容器,容器映像,網路)

 1 year ago
source link: https://blog.miniasp.com/post/2022/11/18/docker-image-prune-notes
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

如何快速刪除所有已經無用的 Docker 資源 (容器,容器映像,網路)

每隔一段時間,我本機的 Docker 容器映像就會佔用我不少硬碟空間,所以我會定時清理,但這個清理動作不常執行,所以指令都會記不住。這篇文章我打算來整理幾個常見的命令,方便我日後查詢命令。

docker prune
  • 列出所有無用的容器映像 (List all dangling images)

    所謂 dangling images 是指那些在每一次 docker build 的過程中遺留下來的容器映像。如果你已經先 build 過一次,使用 demo:latest 這個 tag,然後再 build 一次,再次使用 demo:latest 這個 tag 的話,那麼前一次的 image 並不會被刪掉,而只會被 Untag 而已,而這些 Untag 的 images 就是 dangling images

    以下命令可以列出所有 Untag 的容器映像:

    docker images --filter "dangling=true"
    

    這裡的 --filter 很好用,建議可以看 docker images 官方文件查看更多用法!

    以下命令可以列出所有 Untag 的容器映像,但只顯示 Image ID 而已:

    docker images --filter "dangling=true" -q
    
    image
  • 快速移除所有無用的容器映像 (Remove all dangling images)

    以下命令很神奇的剛好同時適用於 BashPowerShell,請不要跑在 Command Prompt 命令提示字元喔!

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

    這個命令其實有點複雜,一般人記不起來,其實你也可以用以下命令,做到完全一樣的事,好記多了!👍

    docker image prune
    
    docker image prune
  • 移除所有沒用到的容器映像 (Remove all unused images)

    如果你執行 docker pull nginx 拉取 nginx 容器映像回來,但是尚未使用這個 image 執行任何容器(Container),這些不能稱為 dangling images,而是 unused images (未使用中的容器映像) 而已,如果你想一口氣清楚所有這類 images 的話,可以執行以下命令:

    docker image prune -a
    
    docker image prune -a
  • 移除所有沒用到的 Docker 物件 (Docker System Prune)

    以下這個命令不僅僅刪除容器映像而已,而是連同「停用的容器」(stopped containers)、「無用的網路」(dangling networks)、「無用的容器映像」(dangling images)、「無用的建置快取」(dangling build cache) 全部都刪除!

    docker system prune -a
    

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK