在docker容器中运行bash脚本到windows 10

在docker容器中运行bash脚本到windows 10,windows,bash,docker,docker-registry,Windows,Bash,Docker,Docker Registry,我想自动Docker注册表清理和使用本手册 我使用Windows 10,Docker desktop for Windows。 我从这里下载图片 我正在通过Docker composer运行Docker 但我无法在Windows中运行脚本注册表_cleanup.sh #!/bin/bash # # automatically Cleanup old docker images and tags # # Configs : ${REGISTRY_URL:=http://127.0.0.1:50

我想自动Docker注册表清理和使用本手册

我使用Windows 10,Docker desktop for Windows。 我从这里下载图片 我正在通过Docker composer运行Docker

但我无法在Windows中运行脚本注册表_cleanup.sh

#!/bin/bash
#
# automatically Cleanup old docker images and tags
#

# Configs

: ${REGISTRY_URL:=http://127.0.0.1:5000}
: ${REGISTRY_DIR:=./data}
: ${MAX_AGE_SECONDS:=$((30 * 24 * 3600))} # 30 days
: ${DOCKER_REGISTRY_NAME:=registry_web}
: ${DOCKER_REGISTRY_CONFIG:=/etc/docker/registry/config.yml}
: ${DRY_RUN:=false}

EXCLUDE_TAGS="^(\*|master|develop|latest|stable|(v|[0-9]\.)[0-9]+(\.[0-9]+)*)$"
REPO_DIR=${REGISTRY_DIR}/docker/registry/v2/repositories

# In doubt fall back to dry mode
[[ $DRY_RUN != "false" ]] && DRY_RUN=true


_curl() {
  curl -fsS ${CURL_INSECURE_ARG} "$@"
}

# parse yyyymmddHHMMSS string into unix timestamp
datetime_to_timestamp() {
  echo "$1" | awk 'BEGIN {OFS=""} { print substr($1,0,4), "-", substr($1,5,2), "-", substr($1,7,2), " ", substr($1,9,2), ":", substr($1,11,2), ":", substr($1,13,2) }'
}

run_garbage() {
  echo "Running garbage-collect command ..."
  local dry_run_arg=
  $DRY_RUN && dry_run_arg=--dry-run
  docker exec -i $DOCKER_REGISTRY_NAME /bin/registry garbage-collect $DOCKER_REGISTRY_CONFIG $dry_run_arg > /dev/null
}

remove_old_tags() {
  echo "Start Remove Old Tags ..."
  local repo_path image_path
  TAG_COUNT=0

  for repo_path in $REPO_DIR/*; do
    local repo=$(basename $repo_path)
    echo "Current repo: $repo"

    for image_path in $repo_path/*; do
      local image=$(basename $image_path)
      remove_image_tags "$repo" "$image"
    done
    echo
  done
}

remove_image_tags() {
  local repo=$1
  local image=$2

  echo "- Cleanup image $repo/$image"

  local tag_path
  for tag_path in $REPO_DIR/$repo/$image/_manifests/tags/*; do
    local tag=$(basename $tag_path)

    # Do not clenup execluded tags
    if ! [[ $tag =~ $EXCLUDE_TAGS ]]; then
      # get timestamp from tag folder
      local timestamp=$(date -d @$(stat -c %Y $tag_path) +%Y%m%d%H%M%S)

      # parse yyyymmddHHMMSS string into unix timestamp
      timestamp=$(date -d "$(datetime_to_timestamp "$timestamp")" +%s)
      local now=$(date +%s)

      # check if the tag is old enough to delete
      if ((now - timestamp > $MAX_AGE_SECONDS)); then
        if $DRY_RUN; then
          echo "To be Deleted >>  rm -rf ${tag_path}"
        else
          echo "Deleted: $tag"
          TAG_COUNT=$((TAG_COUNT+1))
          rm -rf ${tag_path}
        fi
      fi
    fi
  done
}

delete_manifests_without_tags(){
  cd ${REPO_DIR}

  local manifests_without_tags=$(
    comm -23 <(
      find . -type f -path "./*/*/_manifests/revisions/sha256/*/link" |
      grep -v "\/signatures\/sha256\/" |
      awk -F/ '{print $(NF-1)}' |
      sort -u
    ) <(
      find . -type f -path './*/*/_manifests/tags/*/current/link' |
      xargs sed 's/^sha256://' |
      sort -u
    )
  )

  CURRENT_COUNT=0
  FAILED_COUNT=0
  TOTAL_COUNT=$(echo ${manifests_without_tags} | wc -w | tr -d ' ')

  if [ ${TOTAL_COUNT} -gt 0 ]; then
    echo -n "Found ${TOTAL_COUNT} manifests. "
    if $DRY_RUN; then
      echo "Run without --dry-run to clean up"
    else
      echo "Starting to clean up"
    fi

    local manifest
    for manifest in ${manifests_without_tags}; do
      local repos=$(
        find . -path "./*/*/_manifests/revisions/sha256/${manifest}/link" |
        sed 's#^./\(.*\)/_manifest.*$#\1#'
      )

      for repo in $repos; do
        if $DRY_RUN; then
          echo "Would have run: _curl -X DELETE ${REGISTRY_URL}/v2/${repo}/manifests/sha256:${manifest} > /dev/null"
        else
          if _curl -X DELETE ${REGISTRY_URL}/v2/${repo}/manifests/sha256:${manifest} > /dev/null; then
            CURRENT_COUNT=$((CURRENT_COUNT+1))
          else
            FAILED_COUNT=$((FAILED_COUNT+1))
          fi
        fi
      done
    done
  else
    echo "No manifests without tags found. Nothing to do."
  fi
}

print_summary(){
  if $DRY_RUN; then
    echo "DRY_RUN over"
  else
    echo "Job done"
    echo "Removed ${TAG_COUNT} tags."
    echo "Removed ${CURRENT_COUNT} of ${TOTAL_COUNT} manifests."

    [ ${FAILED_COUNT} -gt 0 ] && echo "${FAILED_COUNT} manifests failed. Check for curl errors in the output above."

    echo "Disk usage before and after:"
    echo "${DF_BEFORE}"
    echo
    echo "${DF_AFTER}"
  fi
}

start_cleanup(){
  $DRY_RUN && echo "Running in dry-run mode. Will not make any changes"

  #Check registry dir
  if [ ! -d ${REPO_DIR} ]; then
    echo "REPO_DIR doesn't exist. REPO_DIR=${REPO_DIR}"
    exit 1
  fi

  #correct registry url (remove trailing slash)
  REGISTRY_URL=${REGISTRY_URL%/}

  #run curl with --insecure?
  [ "$CURL_INSECURE" == "true" ] && CURL_INSECURE_ARG=--insecure

  #verify registry url
  if ! _curl -m 3 ${REGISTRY_URL}/v2/ > /dev/null; then
    echo "Could not contact registry at ${REGISTRY_URL} - quitting"
    exit 1
  fi

  DF_BEFORE=$(df -Ph)

  remove_old_tags
  delete_manifests_without_tags
  run_garbage

  DF_AFTER=$(df -Ph)
  print_summary
}

start_cleanup
CURL\u unsecure=true DRY\u RUN=true./registry\u cleanup.sh CURL\u unsecure=true DRY\u RUN=true bash registry\u cleanup.sh

可以运行此脚本Docker CLI或将其更改为在Windows中运行

#!/bin/bash
#
# automatically Cleanup old docker images and tags
#

# Configs

: ${REGISTRY_URL:=http://127.0.0.1:5000}
: ${REGISTRY_DIR:=./data}
: ${MAX_AGE_SECONDS:=$((30 * 24 * 3600))} # 30 days
: ${DOCKER_REGISTRY_NAME:=registry_web}
: ${DOCKER_REGISTRY_CONFIG:=/etc/docker/registry/config.yml}
: ${DRY_RUN:=false}

EXCLUDE_TAGS="^(\*|master|develop|latest|stable|(v|[0-9]\.)[0-9]+(\.[0-9]+)*)$"
REPO_DIR=${REGISTRY_DIR}/docker/registry/v2/repositories

# In doubt fall back to dry mode
[[ $DRY_RUN != "false" ]] && DRY_RUN=true


_curl() {
  curl -fsS ${CURL_INSECURE_ARG} "$@"
}

# parse yyyymmddHHMMSS string into unix timestamp
datetime_to_timestamp() {
  echo "$1" | awk 'BEGIN {OFS=""} { print substr($1,0,4), "-", substr($1,5,2), "-", substr($1,7,2), " ", substr($1,9,2), ":", substr($1,11,2), ":", substr($1,13,2) }'
}

run_garbage() {
  echo "Running garbage-collect command ..."
  local dry_run_arg=
  $DRY_RUN && dry_run_arg=--dry-run
  docker exec -i $DOCKER_REGISTRY_NAME /bin/registry garbage-collect $DOCKER_REGISTRY_CONFIG $dry_run_arg > /dev/null
}

remove_old_tags() {
  echo "Start Remove Old Tags ..."
  local repo_path image_path
  TAG_COUNT=0

  for repo_path in $REPO_DIR/*; do
    local repo=$(basename $repo_path)
    echo "Current repo: $repo"

    for image_path in $repo_path/*; do
      local image=$(basename $image_path)
      remove_image_tags "$repo" "$image"
    done
    echo
  done
}

remove_image_tags() {
  local repo=$1
  local image=$2

  echo "- Cleanup image $repo/$image"

  local tag_path
  for tag_path in $REPO_DIR/$repo/$image/_manifests/tags/*; do
    local tag=$(basename $tag_path)

    # Do not clenup execluded tags
    if ! [[ $tag =~ $EXCLUDE_TAGS ]]; then
      # get timestamp from tag folder
      local timestamp=$(date -d @$(stat -c %Y $tag_path) +%Y%m%d%H%M%S)

      # parse yyyymmddHHMMSS string into unix timestamp
      timestamp=$(date -d "$(datetime_to_timestamp "$timestamp")" +%s)
      local now=$(date +%s)

      # check if the tag is old enough to delete
      if ((now - timestamp > $MAX_AGE_SECONDS)); then
        if $DRY_RUN; then
          echo "To be Deleted >>  rm -rf ${tag_path}"
        else
          echo "Deleted: $tag"
          TAG_COUNT=$((TAG_COUNT+1))
          rm -rf ${tag_path}
        fi
      fi
    fi
  done
}

delete_manifests_without_tags(){
  cd ${REPO_DIR}

  local manifests_without_tags=$(
    comm -23 <(
      find . -type f -path "./*/*/_manifests/revisions/sha256/*/link" |
      grep -v "\/signatures\/sha256\/" |
      awk -F/ '{print $(NF-1)}' |
      sort -u
    ) <(
      find . -type f -path './*/*/_manifests/tags/*/current/link' |
      xargs sed 's/^sha256://' |
      sort -u
    )
  )

  CURRENT_COUNT=0
  FAILED_COUNT=0
  TOTAL_COUNT=$(echo ${manifests_without_tags} | wc -w | tr -d ' ')

  if [ ${TOTAL_COUNT} -gt 0 ]; then
    echo -n "Found ${TOTAL_COUNT} manifests. "
    if $DRY_RUN; then
      echo "Run without --dry-run to clean up"
    else
      echo "Starting to clean up"
    fi

    local manifest
    for manifest in ${manifests_without_tags}; do
      local repos=$(
        find . -path "./*/*/_manifests/revisions/sha256/${manifest}/link" |
        sed 's#^./\(.*\)/_manifest.*$#\1#'
      )

      for repo in $repos; do
        if $DRY_RUN; then
          echo "Would have run: _curl -X DELETE ${REGISTRY_URL}/v2/${repo}/manifests/sha256:${manifest} > /dev/null"
        else
          if _curl -X DELETE ${REGISTRY_URL}/v2/${repo}/manifests/sha256:${manifest} > /dev/null; then
            CURRENT_COUNT=$((CURRENT_COUNT+1))
          else
            FAILED_COUNT=$((FAILED_COUNT+1))
          fi
        fi
      done
    done
  else
    echo "No manifests without tags found. Nothing to do."
  fi
}

print_summary(){
  if $DRY_RUN; then
    echo "DRY_RUN over"
  else
    echo "Job done"
    echo "Removed ${TAG_COUNT} tags."
    echo "Removed ${CURRENT_COUNT} of ${TOTAL_COUNT} manifests."

    [ ${FAILED_COUNT} -gt 0 ] && echo "${FAILED_COUNT} manifests failed. Check for curl errors in the output above."

    echo "Disk usage before and after:"
    echo "${DF_BEFORE}"
    echo
    echo "${DF_AFTER}"
  fi
}

start_cleanup(){
  $DRY_RUN && echo "Running in dry-run mode. Will not make any changes"

  #Check registry dir
  if [ ! -d ${REPO_DIR} ]; then
    echo "REPO_DIR doesn't exist. REPO_DIR=${REPO_DIR}"
    exit 1
  fi

  #correct registry url (remove trailing slash)
  REGISTRY_URL=${REGISTRY_URL%/}

  #run curl with --insecure?
  [ "$CURL_INSECURE" == "true" ] && CURL_INSECURE_ARG=--insecure

  #verify registry url
  if ! _curl -m 3 ${REGISTRY_URL}/v2/ > /dev/null; then
    echo "Could not contact registry at ${REGISTRY_URL} - quitting"
    exit 1
  fi

  DF_BEFORE=$(df -Ph)

  remove_old_tags
  delete_manifests_without_tags
  run_garbage

  DF_AFTER=$(df -Ph)
  print_summary
}

start_cleanup
#/bin/bash
#
#自动清理旧的docker图像和标记
#
#配置
:${REGISTRY\u URL:=http://127.0.0.1:5000}
:${REGISTRY_DIR:=./data}
:${MAX_AGE_SECONDS:=$((30*24*3600))}30天
:${DOCKER\u REGISTRY\u NAME:=注册表\u web}
:${DOCKER\u REGISTRY\u CONFIG:=/etc/DOCKER/REGISTRY/CONFIG.yml}
:${DRY_RUN:=false}
排除|标记=“^(\*|主控|开发|最新|稳定|(v |[0-9]\.)[0-9]+(\.[0-9]+)*)$”
REPO_DIR=${REGISTRY_DIR}/docker/REGISTRY/v2/repositories
#如有疑问,请返回干燥模式
[[$DRY\u RUN!=“false”]&&DRY\u RUN=true
_curl(){
curl-fsS${curl\u unsecure\u ARG}“$@”
}
#将yyyymmddHHMMSS字符串解析为unix时间戳
datetime_至_时间戳(){
echo“$1”| awk'BEGIN{OFS=”“}{print substr($1,0,4),“-”,substr($1,5,2),“-”,substr($1,7,2),”,substr($1,9,2),“:”,substr($1,11,2),“:”,substr($1,13,2)}
}
运行_垃圾(){
echo“正在运行垃圾收集命令…”
本地干燥运行参数=
$DRY_RUN&&DRY_RUN_arg=--干运行
docker exec-i$docker\u REGISTRY\u NAME/bin/REGISTRY垃圾收集$docker\u REGISTRY\u CONFIG$dry\u run\u arg>/dev/null
}
删除旧标签(){
echo“开始删除旧标签…”
本地回购路径映像路径
标记计数=0
对于$repo_DIR/*;do中的回购路径
本地回购=$(基本名称$repo\u路径)
回显“当前回购:$repo”
对于$repo_path/*;do中的图像_path
本地映像=$(basename$image\u路径)
删除_image_标记“$repo”“$image”
完成
回声
完成
}
删除图像标签(){
本地回购=1美元
本地图像=$2
echo“-清理图像$repo/$image”
本地标记路径
对于$REPO_DIR/$REPO/$image/_清单/tags/*中的tag_路径;do
本地标记=$(basename$标记\路径)
#不要取消已执行的标记
如果![$tag=~$EXCLUDE_TAGS]];那么
#从标记文件夹获取时间戳
本地时间戳=$(日期-d@$(stat-c%Y$tag_path)+%Y%m%d%H%m%S)
#将yyyymmddHHMMSS字符串解析为unix时间戳
时间戳=$(日期-d“$(日期时间戳到时间戳“$时间戳”)”+%s)
本地现在=$(日期+%s)
#检查标记是否足够旧,可以删除
如果((现在-时间戳>$MAX_AGE_SECONDS));那么
如果$DRY_运行,则
echo“待删除>>rm-rf${tag_path}”
其他的
echo“已删除:$tag”
标记计数=$((标记计数+1))
rm-rf${tag_path}
fi
fi
fi
完成
}
删除不带标签的清单(){
cd${REPO_DIR}
不带标签的本地清单=$(
通信-23/dev/null;然后
当前计数=$((当前计数+1))
其他的
失败的计数=$((失败的计数+1))
fi
fi
完成
完成
其他的
echo“未找到没有标记的清单。无需执行任何操作。”
fi
}
打印摘要(){
如果$DRY_运行,则
echo“干车碾过”
其他的
回应“工作完成”
echo“已删除${TAG_COUNT}标记。”
echo“删除了${TOTAL_COUNT}清单中的${CURRENT_COUNT}”
[${FAILED_COUNT}-gt 0]&&echo“${FAILED_COUNT}清单失败。请检查上述输出中的curl错误。”
echo“之前和之后的磁盘使用情况:”
回显“${DF_BEFORE}”
回声
回显“${DF_AFTER}”
fi
}
开始清理(){
$DRY_RUN&&echo“在干运行模式下运行。不会进行任何更改”
#检查注册表目录
如果[!-d${REPO_DIR}];那么
echo“REPO_DIR不存在。REPO_DIR=${REPO_DIR}”
出口1
fi
#正确的注册表url(删除尾部斜杠)
注册表\u URL=${REGISTRY\u URL%/}
#不安全的卷曲?
[“$CURL\u unsecure”==“true”]&&CURL\u unsecure\u ARG=--unsecure
#验证注册表url
如果!\u curl-m3${REGISTRY\u URL}/v2/>/dev/null;那么
echo“无法联系位于${registry\u URL}的注册表-正在退出”
出口1
fi
DF_BEFORE=$(DF-Ph)
删除旧的标签
删除不带\u标记的\u清单\u
乱扔垃圾
DF_AFTER=$(DF-Ph)
打印摘要
}
开始清理

看起来您正试图直接从Windows运行bash脚本。要运行bash脚本,您需要安装Ubuntu/WSL for Windows 10,然后在该环境中安装docker。然后您可以设置此脚本并在WSL级别运行它。