本文将介绍docker中的代理设置。

1. Docker daemon

Cannot download Docker images behind a proxy

In order to download container images from the outside world when running commands like docker pull busybox ,set the proxies by:

  • Create directory

mkdir /etc/systemd/system/docker.service.d

  • Create file

vi /etc/systemd/system/docker.service.d/http-proxy.conf

  • File content
1
2
3
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:80/"
Environment="HTTPS_PROXY=http://proxy.example.com:80/"

If you have internal Docker registries that you need to contact without proxying you can specify them via the NO_PROXY environment variable:

1
2
3
Environment="HTTP_PROXY=http://proxy.example.com:80/"
Environment="HTTPS_PROXY=http://proxy.example.com:80/"
Environment="NO_PROXY=localhost,127.0.0.0/8,docker-registry.somecorporation.com"

  • Flush changes

systemctl daemon-reload

  • Verify that the configuration has been loaded
1
2
3
$ systemctl show --property Environment docker
Environment=HTTP_PROXY=http://proxy.example.com:80/
Environment=HTTPS_PROXY=http://proxy.example.com:80/
  • Restart docker

systemctl restart docker

2. Running container

In order for a running container to access the internet you need to fix the dns names since Google’s are the default and they don’t work. We need to update the dns names by using daemon configuration file.

  • Create file

/etc/docker/daemon.json

  • File content
1
2
3
4
5
6
7
{
"dns":[
"*.*.*.*",
"*.*.*.*",
"*.*.*.*",
]
}
  • Reload daemon

systemctl daemon-reload

  • Restart docker

systemctl restart docker

  • Now you can run a container and pass the proxies like this

docker run --env http_proxy=http://proxy.example.com:80/ --env https_proxy=http://proxy.example.com:80/ -ti ubuntu bash

3. Building the container

  • You can use ENV in the docker file:
1
2
3
4
5
6

FROM ubuntu:16.04

ENV http_proxy http://proxy.example.com:80/
ENV https_proxy http://proxy.example.com:80/
...

参考资料:

  1. Ultimate Guide to Docker HTTP Proxy Configuration
  2. Configure Docker to use a proxy server