Closed
Description
Sometimes we need to use HTTP/HTTPS proxy in three different scenarios:
-
For dockerd
When we want to use HTTP/HTTPS proxy for “docker pull” command, we should tell the dockerd service the proxy information, which managed by systemd. So we should change the systemd configuration for dockerd. See official documentation.
- Create a systemd drop-in directory for the docker service
sudo mkdir -p /etc/systemd/system/docker.service.d
- Create a file named /etc/systemd/system/docker.service.d/http-proxy.conf that adds the HTTP_PROXY environment variable:
[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"
- Flush changes and restart Docker
sudo systemctl daemon-reload sudo systemctl restart docker
-
For containers
When we want to use HTTP/HTTPS proxy in docker containers, we have to choices to tell the containers the proxy information. See official documentation.
-
Configure the docker client
- On the docker client, create or edit the file ~/.docker/config.json and add JSON similar to the following example
{ "proxies": { "default": { "httpProxy": "http://proxy.example.com:8080/", "httpsProxy": "http://proxy.example.com:8080/", "noProxy": "localhost,127.0.0.1,.example.com" } } }
-
Use environment variables
- Set the environment variables manually
Variable docker run example HTTP_PROXY --env HTTP_PROXY="http://proxy.example.com:8080/" HTTPS_PROXY --env HTTPS_PROXY="http://proxy.example.com:8080/" NO_PROXY --env NO_PROXY="localhost,127.0.0.1,.example.com"
-
-
For build phase
When we want to use HTTP/HTTPS proxy in build phase, we also have two choices to tell docker build client the proxy information. See official document 1 and 2
- Use --build-arg environment variables for docker build command
docker build \ --build-arg "HTTP_PROXY=http://proxy.example.com:8080/" \ --build-arg "HTTPS_PROXY=http://proxy.example.com:8080/" \ --build-arg "NO_PROXY=localhost,127.0.0.1,.example.com" .
- Set the environment variables in Dockerfile
Variable Dockerfile example HTTP_PROXY ENV HTTP_PROXY="http://proxy.example.com:8080/" HTTPS_PROXY ENV HTTPS_PROXY="http://proxy.example.com:8080/" NO_PROXY ENV NO_PROXY="localhost,127.0.0.1,.example.com"
Metadata
Metadata
Assignees
Labels
No labels