Docker 配置国内镜像源

由于 GFW 的原因,在下载镜像的时候,经常会出现下载失败的情况,此时就可以使用国内的镜像源。

什么是镜像源:简单来说就是某个组织(学校、公司、甚至是个人)先通过某种手段将国外的镜像下载下来,然后上传到国内的网站,这样我们在国内就可以通过这个网站下载到镜像源

起因

笔者有一次在构建镜像的时候,发现下载镜像报错了:

failed to do request: Get "https://production.cloudflare.docker.com/registry-v2/docker/registry/v2/blobs/sha256/e7/e7d39d4d8569a6203be5b7a118d4d92526b267087023a49ee0868f7c50190191/data?verify=1717770949-vcXzP%2BxUA2JIB7lugP3KRzgJpZA%3D": dial tcp 108.160.165.53:443: i/o timeout

这就是因为 GFW,下载国外镜像的时候超时。

DockerHub 官方镜像源(https://hub.docker.com),很早就开始无法正常访问了。

阿里云镜像

登录阿里云官网,然后点击右上角的控制台:

image

然后找到镜像服务:

image

然后找到镜像加速器地址并配置:

image

可以看到,文档里写了通过修改 daemon 配置文件/etc/docker/daemon.json 来使用加速器:

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://<yourID>.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

该段代码的作用:

  1. 先建立文件夹 /etc/docker
  2. 在配置文件里添加镜像地址(注意替换为自己的)
  3. 重启 Docker

执行完后,可以看看该文件的内容是否正确:

cat /etc/docker/daemon.json 
{
  "registry-mirrors": ["https://<yourID>.aliyuncs.com"]
}

还可以通过 docker info 命令来查看镜像信息:

$ docker info
# 省略其他内容...
Registry Mirrors:
https://<yourID>.aliyuncs.com

封禁...

最近,不少国内的镜像因为监管原因下架了,不知道以后 Docker 怎么使用了,唉。

例如,2024-6-6 上海交大发出的公告

随后,南京大学中国科技大学的镜像源(http://docker.mirrors.ustc.edu.cn)也宣布关闭:

目前整理到能用的镜像源如下(不知道以后会不会被封):

{
  "registry-mirrors": [
    "https://registry.docker-cn.com",
    "http://hub-mirror.c.163.com",
    "https://dockerhub.azk8s.cn",
    "https://mirror.ccs.tencentyun.com",
    "https://registry.cn-hangzhou.aliyuncs.com",
    "https://docker.mirrors.ustc.edu.cn",
    "https://docker.m.daocloud.io",   
    "https://noohub.ru", 
    "https://huecker.io",
    "https://dockerhub.timeweb.cloud" 
  ]
}

部分镜像源说明:

(完)

posted @ 2024-09-05 09:44  peterjxl  阅读(8478)  评论(1)    收藏  举报