Docker 拉取配置教程:解决镜像拉取连接超时问题
bash
docker pull vllm/vllm-openai:latest
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)
一、配置 Docker 通过代理服务器拉取镜像
1. 创建并配置代理配置文件
Docker 服务使用 systemd 管理,若需通过 HTTP 代理拉取镜像,需创建或修改配置文件:
bash
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo vim /etc/systemd/system/docker.service.d/http-proxy.conf
文件内容如下:
ini
[Service]
Environment="HTTP_PROXY=http://proxyclient:xxx@xxx1:8080"
Environment="HTTPS_PROXY=http://proxyclient:xxx@xxx:8080"
Environment="NO_PROXY=localhost,127.0.0.1,.example.com"
说明:
HTTP_PROXY和HTTPS_PROXY:设置代理服务器地址及认证信息;NO_PROXY:设置不走代理的地址列表,逗号分隔,支持 IP、域名、通配符。
2. 重载配置并重启 Docker 服务
bash
sudo systemctl daemon-reload
sudo systemctl restart docker
⚠️ 注意:修改完代理配置后,必须执行
daemon-reload,否则配置不会生效。
二、验证代理配置是否生效
1. 检查环境变量是否加载
bash
systemctl show --property Environment docker
输出示例:
Environment=HTTP_PROXY=http://proxyclient:xxx@xxx:8080
HTTPS_PROXY=http://proxyclient:xxx@xxx:8080 NO_PROXY=localhost,127.0.0.1,.example.com
2. 使用 docker info 查看代理设置
bash
docker info | grep Proxy
输出示例:
HTTP Proxy: http://xxxxx:xxxxx@xxx:8080
HTTPS Proxy: http://xxxxx:xxxxx@xxx:8080
No Proxy: localhost,127.0.0.1,.example.com
三、配置 Docker 镜像加速器
1. 修改 Docker 守护进程配置文件
bash
sudo vim /etc/docker/daemon.json
添加镜像加速器地址(如阿里云):
json
{
"registry-mirrors": ["https://qrdh9sev.mirror.aliyuncs.com"],
"runtimes": {
"nvidia": {
"args": [],
"path": "nvidia-container-runtime"
}
}
}
or
json
{
"runtimes": {
"nvidia": {
"args": [],
"path": "nvidia-container-runtime"
}
},
"registry-mirrors": [
"https://docker.xuanyuan.me",
"https://docker.1ms.run",
"https://pee6w651.mirror.aliyuncs.com",
"https://registry.docker-cn.com",
"https://do.nark.eu.org",
"https://dc.j8.work",
"https://docker.m.daocloud.io",
"https://dockerproxy.com",
"https://docker.mirrors.ustc.edu.cn",
"https://docker.nju.edu.cn",
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com",
"https://ml91x5p1.mirror.aliyuncs.com"
]
}
说明:
registry-mirrors:设置 Docker Hub 的镜像加速地址;- 可根据实际使用的服务提供商替换为其他加速地址。
2. 重启 Docker 服务
bash
sudo systemctl daemon-reload
sudo systemctl restart docker
四、测试
bash
docker pull vllm/vllm-openai:latest
latest: Pulling from vllm/vllm-openai
8f84a9f2102e: Pulling fs layer
b95112eaf283: Pulling fs layer
030ef8250936: Pulling fs layer
72ac9ccfda38: Waiting
73389fbd088f: Pull complete
0264850675f7: Pull complete
de1d03310308: Pull complete
78b596bcad08: Waiting
6a16eaeb9de9: Waiting
latest: Pulling from vllm/vllm-openai
8f84a9f2102e: Pulling fs layer
b95112eaf283: Pulling fs layer
030ef8250936: Pulling fs layer
72ac9ccfda38: Pulling fs layer
73389fbd088f: Pull complete
78b596bcad08: Pull complete
6a16eaeb9de9: Pull complete
Digest: sha256:014a95f21c9edf6abe0aea6b07353f96baa4ec291c427bb1176dc7c93a85845c
Status: Downloaded newer image for vllm/vllm-openai:latest
docker.io/vllm/vllm-openai:latest
五、排查 Docker 连接问题的基本步骤
1. 检查 Docker 守护进程状态
bash
systemctl status docker
若服务未运行,尝试重启:
bash
sudo systemctl start docker
2. 查看 Docker 日志
bash
journalctl -u docker.service -n 100
或:
bash
sudo docker info
查找类似错误信息:
Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection
3. 确认网络设置
- 确认服务器可访问
https://registry-1.docker.io; - 检查 DNS 解析是否正常;
- 使用
curl测试代理:
bash
curl -x http://proxyclient:xxx@xxx:8080 https://registry-1.docker.io/v2/
4. 检查 Docker 配置文件语法
bash
sudo dockerd --test --config-file /etc/docker/daemon.json
若报错,说明配置文件语法错误。
六、总结与建议
| 问题 | 解决方案 |
|---|---|
| Docker 拉取超时 | 检查代理配置、镜像加速器、网络连接 |
| Docker 守护进程异常 | 检查日志、重启服务、验证配置 |
vllm 拉取失败 |
确认镜像地址、标签是否正确;尝试使用代理或镜像加速器 |
| Docker 无法连接 | 检查 docker.sock 权限、守护进程状态 |
建议:
- 使用镜像加速器 + 代理配置双保险;
- 大镜像建议在夜间或低峰期拉取;
- 定期检查 Docker 配置文件与服务状态。
七、进一步排查命令
bash
systemctl status docker
journalctl -u docker.service -b
如需进一步优化或排查问题,可结合 journalctl -xe 查看详细日志。