Skip to content

Latest commit

 

History

History
160 lines (102 loc) · 6.43 KB

20230706_04.md

File metadata and controls

160 lines (102 loc) · 6.43 KB

[转] Docker 深度清除镜像缓存 docker system prune -a --force docker builder prune

作者

digoal

日期

2023-07-06

标签

PostgreSQL , PolarDB , docker


背景

docker build 失败后, 如何清理cache?

https://blog.csdn.net/qq_31977125/article/details/103176917

git clone --depth 1 https://github.com/michelp/pgsodium  
cd pgsodium/  
  
less Dockerfile  
  
Dockerfile文件内容如下:    
  
...      
FROM ubuntu:latest  
ARG version  
ARG DEBIAN_FRONTEND=noninteractive  
...     
# get postgres source and compile with debug and no optimization  
RUN git clone --branch REL_${version}_STABLE https://github.com/postgres/postgres.git --depth=1 &&  
...     
     
  
docker build:    

sudo docker build -t pgsodium:latest --build-arg version=15 .  
  
  
docker build 失败:   
  
 => ERROR [ 9/26] RUN curl -s -L https://download.libsodium.org/libsodium/releases/libsodium-1.0.18.tar.gz | tar zxvf - && cd libsodium-1.0.18 && ./configure && make check && make -j 4 install     9.8s  
------  
 > [ 9/26] RUN curl -s -L https://download.libsodium.org/libsodium/releases/libsodium-1.0.18.tar.gz | tar zxvf - && cd libsodium-1.0.18 && ./configure && make check && make -j 4 install:  
#12 9.824   
#12 9.824 gzip: stdin: unexpected end of file  
#12 9.825 tar: Child returned status 1  
#12 9.825 tar: Error is not recoverable: exiting now  
------  
executor failed running [/bin/sh -c curl -s -L https://download.libsodium.org/libsodium/releases/libsodium-1.0.18.tar.gz | tar zxvf - && cd libsodium-1.0.18 && ./configure && make check && make -j 4 install]: exit code: 2  

docker build 失败后, 如何清理cache?

原文:

https://juejin.cn/post/7041119023286730782

一般情况下,运维清理镜像是通过命令 docker rmi 删除镜像的。但是这条命令不会删除docker build命令产生的缓存文件。这个时候需要使用 docker system 的系列命令来做相关处理。

docker system --help  
  
# 输出  
Usage:  docker system COMMAND  
  
Manage Docker  
  
Commands:  
  df          Show docker disk usage  
  events      Get real time events from the server  
  info        Display system-wide information  
  prune       Remove unused data  
  
Run 'docker system COMMAND --help' for more information on a command.  

操作流程

例如,我们先使用命令查看一下缓存情况:

IT-C02YW2EFLVDL:pgsodium digoal$ docker system df  
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE  
Images          1         1         1.272GB   0B (0%)  
Containers      1         1         12.43MB   0B (0%)  
Local Volumes   34        0         143.1MB   143.1MB (100%)  
Build Cache     41        0         2.528GB   2.528GB  

可以发现,存在大量使用 docker build 命令时产生的镜像缓存 (Build Cache) ,下面使用命令 prune 将其彻底清理。

docker system prune --help  
  
# 输出  
Flag shorthand -h has been deprecated, please use --help  
  
Usage:  docker system prune [OPTIONS]  
  
Remove unused data  
  
Options:  
  -a, --all             Remove all unused images not just dangling ones  
      --filter filter   Provide filter values (e.g. 'label=<key>=<value>')  
  -f, --force           Do not prompt for confirmation  
      --volumes         Prune volumes  

执行命令

docker system prune -a --force  

对应 -a 删除全部未使用的镜像,-f--force 不经过确认强行删除。

再检查一下缓存情况,使用命令

IT-C02YW2EFLVDL:pgsodium digoal$ docker system df  
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE  
Images          1         1         1.272GB   0B (0%)  
Containers      1         1         12.43MB   0B (0%)  
Local Volumes   34        0         143.1MB   143.1MB (100%)  
Build Cache     0         0         0B        0B  

Build Cache 已被完全清除,达成目的。

后续优化策略:

  • 运维可以将本条命令写入定时任务,比如每周清除一次,减轻磁盘压力。
  • 可以使用 --filter 指定变量,定向清除对应 label 的镜像缓存,可以参考官方文档。

digoal's wechat