Docker笔记:如何清理镜像和卷数据

1、docker镜像清理和卷数据清理脚本

镜像清理脚本:docker_images_clean.sh

#!/bin/bash
dockerdir=/var/lib/docker
imagemetadir=${dockerdir}/aufs/diff
imagedatadir=${dockerdir}/graph
dryrun=false
allvolumes={}
 
allvolumes=`docker images -a | awk '{print $3}'`
allvolumes+=`docker ps -a | awk '{print $1}'`
 
function delete_imagefile() {
  targetdir=$1
  echo
  if [[ ! -d ${targetdir} ]]; then
        echo "Directory ${targetdir} does not exist, skipping."
        return
  fi
  echo "Delete unused image directories from $targetdir"
  echo $targetdir
  dirfile=`ls $targetdir`
  #for dir in $(ls -d ${targetdir}/* 2>/dev/null)
  for dir in $dirfile
  do
        dir=$(basename $dir)
     dir=${dir:0:12}
        if [[ "${dir}" =~ [0-9a-f]{12} ]]; then
                if [[ ${allvolumes[@]} =~ "${dir}" ]]; then
                        echo In use ${dir}
                else
                        if [ "${dryrun}" = false ]; then
                                echo Deleting ${dir}
                                rm -rf ${targetdir}/${dir}
                        else
                                echo Would have deleted ${dir}
                        fi
                fi
        else
                echo Not a volume ${dir}
        fi
  done
}
 
delete_imagefile $imagedatadir
delete_imagefile $imagemetadir

卷数据清理脚本:docker_volumes_clean.sh

用法:

./docker_volumes_clean.sh /var/lib/docker/vfs/dir --dry-run
#! /bin/bash
 
set -eo pipefail
 
#usage: sudo ./docker-cleanup-volumes.sh [--dry-run]
 
dockerdir=/var/lib/docker
volumesdir=${dockerdir}/volumes
vfsdir=${dockerdir}/vfs/dir
allvolumes=()
dryrun=false
 
function delete_volumes() {
  targetdir=$1
  echo
  if [[ ! -d ${targetdir} ]]; then
        echo "Directory ${targetdir} does not exist, skipping."
        return
  fi
  echo "Delete unused volume directories from $targetdir"
  for dir in $(ls -d ${targetdir}/* 2>/dev/null)
  do
        dir=$(basename $dir)
        if [[ "${dir}" =~ [0-9a-f]{64} ]]; then
                if [[ ${allvolumes[@]} =~ "${dir}" ]]; then
                        echo In use ${dir}
                else
                        if [ "${dryrun}" = false ]; then
                                echo Deleting ${dir}
                                rm -rf ${targetdir}/${dir}
                        else
                                echo Would have deleted ${dir}
                        fi
                fi
        else
                echo Not a volume ${dir}
        fi
  done
}
 
if [ $UID != 0 ]; then
    echo "You need to be root to use this script."
    exit 1
fi
 
docker_bin=$(which docker.io || which docker)
if [ -z "$docker_bin" ] ; then
    echo "Please install docker. You can install docker by running \"wget -qO- https://get.docker.io/ | sh\"."
    exit 1
fi
 
if [ "$1" = "--dry-run" ]; then
        dryrun=true
else if [ -n "$1" ]; then
        echo "Cleanup docker volumes: remove unused volumes."
        echo "Usage: ${0##*/} [--dry-run]"
        echo "   --dry-run: dry run: display what would get removed."
        exit 1
fi
fi
 
# Make sure that we can talk to docker daemon. If we cannot, we fail here.
docker info >/dev/null
 
#All volumes from all containers
for container in `${docker_bin} ps -a -q --no-trunc`; do
        #add container id to list of volumes, don't think these
        #ever exists in the volumesdir but just to be safe
        allvolumes+=${container}
        #add all volumes from this container to the list of volumes
        for vid in `${docker_bin} inspect --format='{{range $vol, $path := .Volumes}}{{$path}}{{"\n"}}{{end}}' ${container}`; do
                if [[ ${vid} == ${vfsdir}* && "${vid##*/}" =~ [0-9a-f]{64} ]]; then
                        allvolumes+=("${vid##*/}")
                else
                        #check if it's a bindmount, these have a config.json file in the ${volumesdir} but no files in ${vfsdir}
                        for bmv in `grep --include config.json -Rl "\"IsBindMount\":true" ${volumesdir} | xargs grep -l "\"Path\":\"${vid}\""`; do
                                bmv="$(basename "$(dirname "${bmv}")")"
                                allvolumes+=("${bmv}")
                                #there should be only one config for the bindmount, delete any duplicate for the same bindmount.
                                break
                        done
                fi
        done
done
 
delete_volumes ${volumesdir}
delete_volumes ${vfsdir}

参考
https://blog.csdn.net/raindaywhu/article/details/52946740
https://stackoverflow.com/questions/44901297/how-to-clean-up-var-lib-docker-vfs-directory

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值