CentOS 7 Docker 连接 Docker Hub 失败解决方案

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 镜像后续也失效,建议登录 阿里云容器镜像服务 获取个人专属加速地址,稳定性更高。

相关推荐
爱学习的程序媛12 小时前
Docker常用指令速查手册
运维·docker·容器
在荒野的梦想12 小时前
Docker + K8s 部署若依微服务 | 从 0 到 1 实战指南(Dockerfile + Harbor + Helm)
docker·微服务·kubernetes
Y3ai21 小时前
Windows 11 Docker Desktop 保姆级安装使用教程
windows·docker·容器
星辰徐哥21 小时前
大模型工程化部署:Docker Compose批量部署
运维·docker·容器
biubiubiu07061 天前
Docker 快速通关
docker·容器·eureka
两点王爷1 天前
docker 创建和使用存储卷相关内容
java·docker·容器
知秋贺1 天前
如何在ubuntu24中,使用docker 运行ros2 humble版本
运维·docker·容器
yanwumuxi1 天前
Windows本地部署Dify(Docker)
人工智能·docker·语言模型
浪潮IT馆1 天前
WSL2 + Docker Desktop 部署 Dify
运维·docker·容器
芥子沫1 天前
可独立部署的健身饮食管理推荐:wger,Docker一键安装部署教程
docker·容器·开源·健身