shell scripting

如何用shell脚本从docker hub删除超过x天的docker镜像

如何使用shell脚本从docker hub中删除docker images。

我想从docker hub删除一个私人docker hub账户中超过50天的所有图像。

有什么想法吗?在通过Jenkins或bash终端创建和运行这个shell脚本时,应该使用哪些工具。

我在考虑用卷曲来实现我的目标。

以下脚本将删除你的docker hub账户中所有超过50天的镜像。

#!/bin/bash
#Script 将删除您的 docker hub 帐户的所有存储库中超过 50 天的所有图像

set -e

# 设置用户名和密码
UNAME="YOUR_USERNAME"
UPASS="YOUR_PASSWORD"

# 获取令牌以便能够与 Docker Hub 对话
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${UNAME}'", "password": "'${UPASS}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)

# 获取用户可访问的命名空间列表(目前未使用)
#NAMESPACES=$(curl -s -H "授权:JWT ${TOKEN}" https://hub.docker.com/v2/repositories/namespaces/ | jq -r '.namespaces|.[]')

#echo $TOKEN
echo
# 获取该用户帐户的存储库列表
echo "List of Repositories in ${UNAME} Docker Hub account"
sleep 5
REPO_LIST=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/?page_size=10000 | jq -r '.results|.[]|.name')
echo $REPO_LIST
echo
# 建立所有图像和标签的列表
for i in ${REPO_LIST}
do
  # 获取 repo 的标签
  IMAGE_TAGS=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/?page_size=10000 | jq -r '.results|.[]|.name')

  # 从标签构建图像列表
  for j in ${IMAGE_TAGS}
  do
    # 将每个标签添加到列表中
    FULL_IMAGE_LIST="${FULL_IMAGE_LIST} ${UNAME}/${i}:${j}"
      
  done
done

# 所有 docker 镜像的输出列表
echo
echo "List of all docker images in ${UNAME} Docker Hub account"
sleep 10
for i in ${FULL_IMAGE_LIST}
do
  echo ${i}
done

sleep 10
echo
echo "Identifying and deleting images which are older than 50 days in ${UNAME} docker hub account"
sleep 10
# 笔记!!!如果您想对 Docker Hub 帐户的所有存储库执行操作,请取消注释以下行
#for i in ${REPO_LIST}

for i in randomRepo

#笔记!!!要删除特定存储库图像,请仅在 for 循环中包含那些存储库,如下所示 for 循环具有 repos mygninx 和 mykibana
#for i in  mynginx mykibana

do
  # 获取 repo 的标签
  echo
  echo "Looping Through $i repository in ${UNAME} account"
  IMAGE_TAGS=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/?page_size=10000 | jq -r '.results|.[]|.name')

  # 从标签构建图像列表
  for j in ${IMAGE_TAGS}
  do
      echo
      # 添加最后更新时间
    updated_time=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/${j}/?page_size=10000 | jq -r '.last_updated')
    echo $updated_time
    datetime=$updated_time
    timeago='50 days ago'

    dtSec=$(date --date "$datetime" +'%s')
    taSec=$(date --date "$timeago" +'%s')

    echo "INFO: dtSec=$dtSec, taSec=$taSec" 

           if [ $dtSec -lt $taSec ] 
           then
              echo "This image ${UNAME}/${i}:${j} is older than 50 days, deleting this  image"
              ## 请取消注释以下行以删除 docker hub 存储库的 docker hub 映像
              #curl -s -X DELETE -H "授权:JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/${j}/
           else
              echo "This image ${UNAME}/${i}:${j} is within 50 days time range, keep this image"
           fi      
  done
done

echo "Script execution ends"






#!/bin/bash
#Script 将删除您的 docker hub 帐户的所有存储库中早于“X”天的所有图像
set -e

# 设置您的用户名、密码和号码。以下行中的“X”天值。
#UNAME="YOUR_USERNAME"
#UPASS="YOUR_PASSWORD"
#X="YOUR_DAYS_VALUE"

# 从终端传递用户名、密码和“X”天值,如下行。
# ./docker-images-remove-script.sh <用户名> <密码> <30>
UNAME=$1
UPASS=$2
X=$3

# 获取令牌以便能够与 Docker Hub 对话
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${UNAME}'", "password": "'${UPASS}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)


#echo $TOKEN
echo
# 获取该用户帐户的存储库列表
echo "List of Repositories in '${UNAME}' Docker Hub account."
sleep 5
REPO_LIST=$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/?page_size=10000" | jq -r '.results|.[]|.name')
#echo "$REPO_LIST"
count=1
for rep in ${REPO_LIST}
do
     echo S.No: $count RepoName:  $rep
     count=`expr $count + 1`
done
echo
sleep 5



echo
echo "Identifying and deleting images which are older than $X days in '${UNAME}' docker hub account."
sleep 5

#笔记!!!要删除特定的存储库图像,请仅在 for-loop 中包含那些存储库,例如下面的 for-loop,其中包含 repos mysql 和 mymongo
#for i 在 mysql mymongo 中

for rep in ${REPO_LIST}
 

do
    # 得到总数。图像数量及其回购数量
    Images=$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/$UNAME/$rep/tags/")
    ImageCount=$(echo $Images | jq -r '.count')    
    echo "Total no of Images in '$UNAME/$rep' repository are: $ImageCount"
    pages=`expr $ImageCount / 100 + 1`
    echo "No pages to iterate are: $pages"    
    sleep 5
    for (( p=1; p<=$pages; p++ ))
    do         
        echo "Looping Through '$rep' repository in '${UNAME}' account."
        IMAGES=$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/${rep}/tags/?page_size=100&page=$p") 
        IMAGE_TAGS=$(echo $IMAGES | jq -r '.results|.[]|.name')
        count1=1

             # 从标签构建图像列表
             for tag in ${IMAGE_TAGS}
             do
                  
                  echo Iteration no. is: $p
                  echo "S.No: $count1. RepoName: '$rep' ImageTag: $tag"
                  count1=`expr $count1 + 1`
                  sleep 5
                  # 获取上次更新时间
                  updated_time=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/${rep}/tags/${tag}/?page_size=100 | jq -r '.last_updated')
                  echo "Image build date and time is : $updated_time"
                  datetime=$updated_time
                  timeago=''$X' days ago'
                  #echo $timeago

                  dtSec=$(date --date "$datetime" +"%Y%m%d")
                  taSec=$(date --date "$timeago"  +"%Y%m%d")
                   
                  dt_Sec=$(date --date "$datetime" +"%Y-%m-%d")
                  ta_Sec=$(date --date "$timeago"  +"%Y-%m-%d")
                  

                  echo "INFO: Date on which this image was build: $dt_Sec" 
                  echo "INFO: $X days earlier date from today is: $ta_Sec" 
                  sleep 5
                  if [ $dtSec -lt $taSec ] 
                  then
                        echo "This image '${UNAME}/${rep}:${tag}'  is older than $X days, deleting this  image."
                  #### 笔记!要删除图像,请取消注释下面的行。
                  #### curl -s -X DELETE -H "授权:JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/${rep}/tags/${tag }/
                  else
                        echo "This image '${UNAME}/${rep}:${tag}' is within $X days time range, keeping this image."
                  fi
                  echo
             done      
    done
echo
done

echo "Script execution ends here."