Ctrl + C 中断构建之后,中间层镜像和构建缓存会残留在本地,占用空间。可以这样清理:


1. 删除构建缓存(推荐方式)

docker builder prune
  • 会清掉未被任何镜像引用的构建缓存层

  • 如果你想更彻底,加 -a

    docker builder prune -a
    

    这会把所有缓存层都清掉,下次 docker build 会完全重建。


2. 删除悬空镜像(中断时产生的 <none> 镜像)

docker image prune
  • 删除无标签(dangling)的镜像。

  • 如果要更狠一点:

    docker image prune -a
    

    会删除所有没被容器使用的镜像(包括你没打标签但保留的镜像)。


3. 一步到位大清理(慎用)

如果你想把没用的容器、网络、镜像、缓存全清掉:

docker system prune -a
  • 会提示确认,删除范围大(包括没在跑的容器、没用的网络、无标签镜像、缓存)。

4. 检查空间占用

docker system df

能看到 images / containers / local volumes / build cache 的占用情况。


✅ 建议:

  • 如果只是打断了一次构建,docker builder prune 就够了。
  • 如果你怀疑有 <none> 镜像残留,再跑 docker image prune
  • 如果磁盘空间告急,可以用 docker system prune -a 来“大扫除”。

实际操作

 docker builder prune
WARNING! This will remove all dangling build cache. Are you sure you want to continue? [y/N] y
ID                                              RECLAIMABLE     SIZE            LAST ACCESSED
pklwhooalq0qn7ve1ha4b2r7y*                      true            9.365kB         12 minutes ago
y15m4nhdqb33yo19v029szk6d*                      true    2.887kB         12 minutes ago
i7dovdfsr6h6qektdhxxu6pjt*                      true    12.15GB         12 minutes ago
Total:  12.15GB
(05_ep_dev) root@k8s-master-pfsrv:/home/zhangbin/perfwork/01_ai/12_exp_v1.2# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐