CentOS 7 Docker 连接 Docker Hub 失败解决方案
摘要:本文记录 CentOS 7 系统中 Docker 无法连接 Docker Hub 的完整排查过程,最终通过配置 DaoCloud 镜像加速器解决问题。
一、问题现象
在 CentOS 7 系统上使用 Docker 搜索镜像时,出现连接被拒绝错误:
bash
[root@localhost ~]# docker search nginx
Error response from daemon: Get "https://index.docker.io/v1/search?q=nginx&n=25":
dial tcp 157.240.9.36:443: connect: connection refused

二、环境信息
- 操作系统:CentOS 7 (64位)
- Docker 版本 :(请读者自行查看
docker --version) - 网络环境:中国境内
三、排查过程
3.1 基础网络诊断
首先验证网络连通性:
bash
# 测试外网连通性 - 正常
[root@localhost ~]# curl -I https://www.baidu.com
HTTP/1.1 200 OK

bash
# 测试 Docker Hub 连接 - 失败
[root@localhost ~]# curl -4 -I https://index.docker.io/v1/
curl: (7) Failed connect to index.docker.io:443; 拒绝连接
# DNS 解析正常
[root@localhost ~]# nslookup index.docker.io
Name: index.docker.io
Address: 199.59.150.49
初步判断:DNS 解析正常,外网连接正常,但 Docker Hub 特定连接被拒绝。
3.2 检查防火墙
检查 firewalld 和 iptables 规则:
bash
# firewalld 状态
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Active: active (running) ...
# 查看防火墙规则
[root@localhost ~]# firewall-cmd --list-all
public (active)
services: dhcpv6-client ssh
ports:
结论:iptables 没有阻止 443 端口出站规则,防火墙不是根本原因。
3.3 配置镜像加速器(第一次尝试)
尝试配置常见的国内镜像源:
bash
cat > /etc/docker/daemon.json << 'EOF'
{
"registry-mirrors": [
"https://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn"
]
}
EOF
systemctl restart docker
验证配置:
bash
[root@localhost ~]# docker info | grep -A 5 "Registry Mirrors"
Registry Mirrors:
https://hub-mirror.c.163.com/
https://docker.mirrors.ustc.edu.cn/
结果 :配置已生效,但 docker search nginx 仍然连接被拒绝。
网易云和中科大镜像源目前已停止服务或限制访问。
四、最终解决方案
4.1 使用 DaoCloud 镜像加速器
更换为 DaoCloud 镜像:
bash
cat > /etc/docker/daemon.json << 'EOF'
{
"registry-mirrors": ["https://docker.m.daocloud.io"]
}
EOF
systemctl restart docker
4.2 验证结果
bash
[root@localhost ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL
nginx Official build of Nginx. 21186 [OK]
nginx/nginx-ingress NGINX and NGINX Plus Ingress Controllers fo... 114
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter for NGINX and NGIN... 50
nginx/unit This repository is retired, use the Docker o... 66
nginx/nginx-ingress-operator NGINX Ingress Operator for NGINX and NGINX P... 3
nginx/nginx-quic-qns NGINX QUIC interop 1
nginx/nginxaas-loadbalancer-kubernetes 1
nginx/unit-preview Unit preview features 0
bitnami/nginx Bitnami Secure Image for nginx 202
bitnamicharts/nginx Bitnami Helm chart for NGINX Open Source 3
ubuntu/nginx Nginx, a high-performance reverse proxy & we... 138
kasmweb/nginx An Nginx image based off nginx:alpine and in... 8
rancher/nginx 3
linuxserver/nginx An Nginx container, brought to you by LinuxS... 236
dtagdevsec/nginx T-Pot Nginx 0
✅ 成功! Docker 可以正常搜索和拉取镜像了。
五、总结
| 排查步骤 | 结果 |
|---|---|
| 网络连通性 | ✅ 正常 |
| DNS 解析 | ✅ 正常 |
| 防火墙 | ✅ 未阻止 |
| 网易云/中科大镜像 | ❌ 已失效 |
| DaoCloud 镜像 | ✅ 可用 |
最终配置
文件 /etc/docker/daemon.json:
json
{
"registry-mirrors": ["https://docker.m.daocloud.io"]
}
常用镜像源汇总(截至 2025-2026)
| 镜像源 | 地址 | 状态 |
|---|---|---|
| DaoCloud | https://docker.m.daocloud.io |
✅ 可用 |
| 阿里云 | https://<your-id>.mirror.aliyuncs.com |
✅ 需注册 |
| 网易云 | https://hub-mirror.c.163.com |
❌ 失效 |
| 中科大 | https://docker.mirrors.ustc.edu.cn |
❌ 失效 |
六、后续使用
bash
# 拉取镜像
docker pull nginx
# 运行容器
docker run -d -p 80:80 --name my-nginx nginx
# 查看运行状态
docker ps
提示 :如果 DaoCloud 镜像后续也失效,建议登录 阿里云容器镜像服务 获取个人专属加速地址,稳定性更高。