How to setup proxy for 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 | [Service] |
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
3Environment="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 | $ systemctl show --property Environment docker |
- 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 | { |
- 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 |
|
参考资料: