Docker 网络代理配置及防火墙设置指南

Docker 网络代理配置及防火墙设置指南

背景

在某些环境中,服务器无法直接访问外网,需要通过网络代理进行连接。虽然我们通常会在 /etc/environment/etc/profile 等系统配置文件中直接配置代理,但 Docker 命令无法使用这些配置。例如,在使用 docker pull 命令从外网拉取镜像时,可能会遇到如下错误:

复制代码
docker pull hello-world
Unable to find image 'hello-world:latest' locally
Pulling repository docker.io/library/hello-world
docker: Network timed out while trying to connect to https://index.docker.io/v1/repositories/library/hello-world/images. You may want to check your internet connection or if you are behind a proxy..
See 'docker run --help'.

解决方案

防火墙设置

在进行 Docker 代理配置之前,建议先检查防火墙设置,确保相关端口已开放。以下是在 CentOS 7 上查看、防火墙状态以及启用/停用防火墙的方法:

  1. 查看防火墙状态

    复制代码
    systemctl status firewalld

    示例输出:

  2. 关闭防火墙

    复制代码
    systemctl stop firewalld
  3. 开启防火墙

    复制代码
    systemctl start firewalld
  4. 禁用防火墙(防止开机自动启动):

    复制代码
    systemctl disable firewalld
  5. 启用防火墙(设置为开机自动启动):

    复制代码
    systemctl enable firewalld
  6. 检查已开放的端口

    复制代码
    firewall-cmd --list-ports

在进行 Docker 代理配置时,如果防火墙开启,确保相关代理端口(如 2375, 1230)已通过防火墙放行。

方案一:通过手动启动 Docker Daemon 设置代理

  1. 停止 Docker 服务:

    复制代码
    systemctl stop docker.service
  2. 手动启动 Docker Daemon,监听所有网络接口:

    复制代码
    nohup docker daemon -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock &

更多详情可参考 Docker Daemon Socket 选项

方案二:配置系统级代理(不推荐)

可以通过修改系统配置文件来设置代理,如 Ubuntu 的 /etc/default/docker 或 CentOS 的 /etc/sysconfig/docker 文件,但这种方法已不再推荐使用。具体配置如下:

bash 复制代码
HTTP_PROXY="http://[proxy-addr]:[proxy-port]/"
HTTPS_PROXY="https://[proxy-addr]:[proxy-port]/"
export HTTP_PROXY HTTPS_PROXY

方案三:持久化的 Docker 代理配置

这种方法将代理配置持久化,使其在 Docker 服务每次启动时生效。

  1. 创建 Docker 服务的 systemd 配置目录:

    复制代码
    mkdir -p /etc/systemd/system/docker.service.d
  2. 创建代理配置文件 /etc/systemd/system/docker.service.d/http-proxy.conf 并添加以下内容:

    ini 复制代码
    [Service]
    Environment="HTTP_PROXY=http://[proxy-addr]:[proxy-port]/" "HTTPS_PROXY=https://[proxy-addr]:[proxy-port]/"
  3. 如果有不需要使用代理访问的内部 Docker 镜像仓库,可以配置 NO_PROXY 变量:

    ini 复制代码
    [Service]
    Environment="HTTP_PROXY=http://[proxy-addr]:[proxy-port]/" "HTTPS_PROXY=https://[proxy-addr]:[proxy-port]/" "NO_PROXY=localhost,127.0.0.1,docker-registry.somecorporation.com"
  4. 重新加载配置:

    复制代码
    systemctl daemon-reload
  5. 重启 Docker 服务:

    复制代码
    systemctl restart docker

更多细节请参考 Docker systemd 配置指南

使用 SOCKS5 代理

要为 Docker 配置 SOCKS5 代理,可以按以下步骤操作:

  1. 编辑 Docker 服务文件 /usr/lib/systemd/system/docker.service

    ini 复制代码
    [Service]
    Environment="HTTP_PROXY=socks5://127.0.0.1:1230/"
    Environment="HTTPS_PROXY=socks5://127.0.0.1:1230/"
    Environment="NO_PROXY=localhost,127.0.0.1,m1empwb1.mirror.aliyuncs.com,docker.io,registry.cn-hangzhou.aliyuncs.com"
  2. 重新加载服务配置并重启 Docker:

    复制代码
    systemctl daemon-reload
    systemctl restart docker
  3. 验证代理配置:

    复制代码
    systemctl show --property=Environment docker

如果输出的内容中包含 127.0.0.1:1230 这样的地址,表示配置成功。

测试

可以通过 docker pull 命令测试代理配置是否生效:

复制代码
docker pull gcr.io/kubernetes-helm/tiller:v2.2.2

使用 ss -antp |grep EST |egrep '1080|1230' 命令查看连接状态,确保代理配置已生效。

相关推荐
lichenyang4531 天前
Docker 学习笔记(五):Docker Compose,用一个 YAML 启动前端、后端和 MongoDB
docker
lichenyang4531 天前
Docker 学习笔记(四):Dockerfile,把项目打成自己的镜像
docker·容器
lichenyang4531 天前
Docker 学习笔记(三):Docker 网络、bridge、子网和容器互通
docker·容器
lichenyang4531 天前
Docker 学习笔记(二):docker run 的参数到底在控制什么?
docker·容器
Patrick_Wilson6 天前
从「改个端口」到 502:Next.js on k8s 的容器端口、Service 映射与 env 覆盖
docker·kubernetes·next.js
Suroy6 天前
DockerView-Go:用 Go 写一个终端 Docker 监控工具,顺便做了个 Web 仪表盘
docker
云恒要逆袭6 天前
运行你的第一个Docker容器
后端·docker·容器
宋均浩7 天前
# Docker 镜像瘦身实战:从 1.2G 到 80MB 的五个优化步骤
ci/cd·docker
程序员老赵8 天前
10 分钟部署 OpenCode:Docker 一键安装,浏览器打开就能用 AI 写代码(附完整命令与排错)
docker·容器·ai编程
WangMingHua1118 天前
LM Studio Docker 部署——本地大模型一键启动
docker