docker镜像拉取指南-测试可用镜像源

admin 2024年6月19日17:10:42评论33 views字数 5803阅读19分20秒阅读模式

确实有需求

身边问dockerhub镜像站被墙的朋友越来越多,有同事找我手工导出镜像去load了

docker镜像拉取指南-测试可用镜像源

本文先盘一盘还能用的镜像站,目前看来阿里云的镜像加速器是可以使用的,但是据说该镜像站存在未同步最新源镜像问题

ps:阿里云可以在官网中申请自己账号的专属镜像加速器

https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

测试公开加速器

这个仓库维护了一些国内镜像站的列表

https://gist.github.com/y0ngb1n/7e8f16af3242c7815e7ca2f0833d3ea6

docker镜像拉取指南-测试可用镜像源

直接写入到docker配置文件中测试

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": [
        "https://docker.m.daocloud.io",
        "https://dockerhub.azk8s.cn",
        "https://mirror.ccs.tencentyun.com",
        "https://dockerproxy.com",
        "https://mirror.baidubce.com",
        "https://docker.mirrors.ustc.edu.cn",
        "https://docker.nju.edu.cn",
        "https://mirror.iscas.ac.cn"
    ]
}
EOF
kill -SIGHUP $(pidof dockerd)

docker info看看是否替换成功

root@vps:~# docker info | grep http
 Registry: https://index.docker.io/v1/
  https://docker.m.daocloud.io/
  https://dockerhub.azk8s.cn/
  https://mirror.ccs.tencentyun.com/
  https://dockerproxy.com/
  https://mirror.baidubce.com/
  https://docker.mirrors.ustc.edu.cn/
  https://docker.nju.edu.cn/
  https://mirror.iscas.ac.cn/
WARNING: No swap limit support
root@vps:~

这样就是替换成功了,docker pull centos 测试一下就可以知道镜像站能不能用了

脚本测试

手工测试有点不优雅,这里抠到一个开发者写的测试脚本,脚本下载了特定的一个镜像层library/centos:latest来测量每个镜像源的下载速度

https://raw.githubusercontent.com/lework/script/master/shell/test/docker_hub_speed_test.sh

稍微修改了一下脚本代码,加入别的镜像源、for循环任务放到后台避免阻塞进度,略微缩短一点测试时间

#!/bin/env bash

# lework
# Docker Hub mirror site speed test.

######################################################################################################
# environment configuration
######################################################################################################

# Colors
RED='�33[0;31m'
GREEN='�33[0;32m'
YELLOW='�33[0;33m'
BLUE='�33[0;36m'
PLAIN='�33[0m'

image_name="library/centos"
image_tag="latest"

declare -A mirrors
mirrors=(
[azure]="http://dockerhub.azk8s.cn"
[tencent]="https://mirror.ccs.tencentyun.com"
[daocloud]="http://f1361db2.m.daocloud.io"
[dao]="https://docker.m.daocloud.io"
[proxy]="https://dockerproxy.com"
[baidu]="https://mirror.baidubce.com"
[nju]="https://docker.nju.edu.cn"
[iscas]="https://mirror.iscas.ac.cn"
[netease]="http://hub-mirror.c.163.com"
[ustc]="https://docker.mirrors.ustc.edu.cn"
[aliyun]="https://2h3po24q.mirror.aliyuncs.com"
[qiniu]="https://reg-mirror.qiniu.com"
)

######################################################################################################
# function
######################################################################################################

function shutdown() {
tput cnorm # reset cursor
}

function error() {
ps -ef | grep "$0" | awk '{print $2}' | xargs kill -9 '{}' >/dev/null 2>&1
}

trap shutdown EXIT
trap error ERR 2 3

function spinner() {
# make sure we use non-unicode character type locale 
# (that way it works for any locale as long as the font supports the characters)
local LC_CTYPE=C
speed_test "$@" &
sleep 1

local pid=$(ps -ef | grep -E '[w]get.*-4O /dev/null -T300' | awk '{print $2}'# Process Id of the previous running command

local spin='-|/'
local charwidth=1

local i=0
tput civis # cursor invisible
while kill -0 $pid 2>/dev/null; do
local i=$(((i + $charwidth) % ${#spin}))
printf "%s" "${spin:$i:$charwidth}"

echo -en "�33[1D"
sleep .1
done
tput cnorm
wait
}

speed_test() {
local output=$(LANG=C wget ${3:+"--header="}"$3" -4O /dev/null -T300 "$1" 2>&1)
local speed=$(printf '%s' "$output" | awk '//dev/null/ {speed=$3 $4} END {gsub(/(|)/,"",speed); print speed}')
local ipaddress=$(printf '%s' "$output" | awk -F'|' '/Connecting to .*|([^|]+)|/ {print $2}'tail -1)
local time=$(printf '%s' "$output" | awk -F= '/100% / {print $2}')
local size=$(printf '%s' "$output" | awk '/Length:/ {s=$3} END {gsub(/(|)/,"",s); print s}')
printf "${YELLOW}%-14s${GREEN}%-20s${BLUE}%-14s${PLAIN}%-20s${RED}%-14s${PLAIN}n" "$2" "${ipaddress}" "${size}" "${time}" "${speed}"
}

######################################################################################################
# main 
######################################################################################################

if  [ ! -e '/usr/bin/wget' ]; then
echo "Error: wget command not found. You must be install wget command at first."
exit 1
fi

if  [ ! -e '/usr/bin/curl' ]; then
echo "Error: curl command not found. You must be install curl command at first."
exit 1
fi

clear
echo -e "nnDocker Hub mirror site speed test"

echo -e "n[Mirror Site]"
for mirror in ${!mirrors[*]}do
printf "${PLAIN}%-14s${GREEN}%-20s${PLAIN}n" ${mirror} ":  ${mirrors[$mirror]}"
done
printf "${PLAIN}%-14s${GREEN}%-20s${PLAIN}n" "docker" ":  https://registry-1.docker.io"

echo -e "n[Test]"
echo -e "Test Image        : ${YELLOW}${image_name}:${image_tag}${PLAIN}"

docker_token=$(curl -fsSL "https://auth.docker.io/token?service=registry.docker.io&scope=repository:${image_name}:pull"  | awk '-F"' '{print $4}')
image_manifests=$(curl -fsSL -H "Authorization: Bearer ${docker_token}" "https://registry-1.docker.io/v2/${image_name}/manifests/${image_tag}" | awk -F'"' '/"blobSum":/ {print $4}')
image_layer=$(echo $image_manifests | tr ' ' 'n' | sort -u| head -1)
echo -e "Download layer    : ${YELLOW}${image_layer}${PLAIN}n"

printf "%-14s%-20s%-14s%-20s%-14sn" "Site Name" "IPv4 address" "File Size" "Download Time" "Download Speed"

spinner "https://registry-1.docker.io/v2/${image_name}/blobs/${image_layer}" "docker" "Authorization: Bearer $docker_token" &
for mirror in ${!mirrors[*]}do
{
if [ "${#image_layer}" == "0" ]; then
image_manifests=$(curl -s "${mirror}/v2/library/${image_name}/manifests/${image_tag}" | awk -F'"' '/"blobSum":/ {print $4}')
image_layer=$( echo $resp | tr ' ' 'n' | sort -u | head -1)
fi
spinner "${mirrors[$mirror]}/v2/${image_name}/blobs/${image_layer}" ${mirror}
} &
done
wait

echo

得到可用镜像加速器

docker镜像拉取指南-测试可用镜像源

看跑完的结果可以得知当前有两个可用

https://docker.m.daocloud.io
https://2h3po24q.mirror.aliyuncs.com

测试确实没问题

docker镜像拉取指南-测试可用镜像源

原文始发于微信公众号(安全光圈):docker镜像拉取指南-测试可用镜像源

免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2024年6月19日17:10:42
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   docker镜像拉取指南-测试可用镜像源https://cn-sec.com/archives/2864805.html
                  免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉.

发表评论

匿名网友 填写信息