Dockerd 代理
在执行docker pull时,是由守护进程dockerd来执行。因此,代理需要配在dockerd的环境中。而这个环境,则是受systemd所管控,因此实际是systemd的配置。
1 2 3 |
sudo mkdir -p /etc/systemd/system/docker.service.d sudo touch /etc/systemd/system/docker.service.d/proxy.conf |
在这个proxy.conf文件(可以是任意*.conf的形式)中,添加以下内容:
1 2 3 4 5 |
[Service] Environment="HTTP_PROXY=http://proxy.example.com:8080/" Environment="HTTPS_PROXY=http://proxy.example.com:8080/" Environment="NO_PROXY=localhost,127.0.0.1,.example.com" |
Docker 容器配置代理
配置容器代理一般分为两种,一种是全局配置,另一种是仅为某个容器配置。
- 全局配置
修改或创建 ~/.docker/config.json
修改内容如下
新建文件 ~/.docker/config.json
1 2 3 4 5 6 7 8 9 10 11 |
{ "proxies": { "default": { "httpProxy": "http://173.39.112.117:80", "httpsProxy": "http://173.39.112.117:80" } } } |
为了确保生效,重启下 docker
1 2 |
systemctl restart docker |
创建container,进入container
1 2 |
docker exec -it qinglong bash |
使用curl命令查看是否代理成功
1 2 |
curl cip.cc |
- 局部修改
方法1:在 docker run 命令添加参数
1 2 3 4 5 6 |
--env HTTP_PROXY="http://173.39.112.117:80" \ --env HTTPS_PROXY="http://173.39.112.117:80" \ --env http_proxy="http://173.39.112.117:80" \ --env https_proxy="http://173.39.112.117:80" \ -p 80:80 |
0