docker pull/build 失败 设置国内镜像源
参考 : docker 错误 Error response from daemon,request canceled while waiting for connection
在写完一个服务构建时候写完docker 执行build 报错:ERROR: failed to solve: golang:1.21-alpine: failed to resolve source metadata for docker.io/library/golang:1.21-alpine: unexpected status from HEAD request to https://xx4bwyg2.mirror.aliyuncs.com/v2/library/golang/manifests/1.21-alpine?ns=docker.io: 403 Forbidden
有点搞, 第一行就报错了
看错误应该是网络问题,拉取镜像时候就报错了
为了验证猜想,在命令行随便执行一个pull命令docker pull mysql
拉取一个MySQL镜像试一试
果不其然出错了:
Error response from daemon: Get “https://registry-1.docker.io/v2/”: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
尝试了一下找到解决方案:
解决方案
1. 添加DNS解析地址:(可选,经尝试这一步不加也行)
sudo vim /etc/resolv.conf
添加:nameserver 114.114.114.114
2. 设置docker 国内镜像源
sudo vim daemon.json
在 镜像仓库中添加镜像源:(可以不必要这么多,这里只是列出了几个国内镜像源)
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://dockerproxy.com",
"https://docker.nju.edu.cn",
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn",
"http://mirrors.ustc.edu.cn",
"https://dockerhub.icu"
]
3. 重启docker
sudo systemctl daemon-reload
sudo systemctl restart docker
通过docker info
看到添加的镜像源就说明成功了