确实有需求
身边问dockerhub镜像站被墙的朋友越来越多,有同事找我手工导出镜像去load了
本文先盘一盘还能用的镜像站,目前看来阿里云的镜像加速器是可以使用的,但是据说该镜像站存在未同步最新源镜像问题
ps:阿里云可以在官网中申请自己账号的专属镜像加速器
https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
测试公开加速器
这个仓库维护了一些国内镜像站的列表
https://gist.github.com/y0ngb1n/7e8f16af3242c7815e7ca2f0833d3ea6
直接写入到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
得到可用镜像加速器
看跑完的结果可以得知当前有两个可用
https://docker.m.daocloud.io
https://2h3po24q.mirror.aliyuncs.com
测试确实没问题
原文始发于微信公众号(安全光圈):docker镜像拉取指南-测试可用镜像源
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论