Docker开启网络代理

背景

  • 问题描述

安装外部公共仓的开源镜像时,比如sudo docker pull xxx,出现如下报错提示:

1
2
3
$ sudo docker pull mysql
Using default tag: 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)
  • 报错原因

需要通过配置proxy才能访问外网,docker不能直接依赖基础操作系统环境的proxy,因此需要独立配置属于docker的proxy才能访问外网。

docker proxy配置

  • 配置daemon.json文件
1
2
3
4
sudo mkdir -p /etc/docker
sudo cp /etc/docker/daemon.json /etc/docker/daemon.json.bak
sudo vim /etc/docker/daemon.json

  • 配置docker代理文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo vim /etc/systemd/system/docker.service.d/http-proxy.conf


# 配置内容
# [Service]
# 代理服务器地址
# Environment="HTTP_PROXY=http://账号:密码@proxy.xxx.com:8080/"
# Environment="HTTPS_PROXY=http://账号:密码@proxy.xxx.com:8080/"
# 代理黑名单
# Environment="HTTP_PROXY=http://账号:密码@proxy.xxx.com:8080/"
# Environment="NO_PROXY=decs.com,localhost,127.0.0.1"

# 示例
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:19527/"
Environment="HTTPS_PROXY=http://127.0.0.1:19527/"
# Environment="NO_PROXY=decs.com,localhost,127.0.0.1"
  • 重启docker,查看proxy是否配成功
1
2
3
4
sudo systemctl daemon-reload
sudo systemctl restart docker
# 查看proxy是否配置成功
sudo systemctl show --property=Environment docker