Setting up a Proxy for Docker
To set up a proxy for Docker, you need to configure the Docker Daemon and the Docker Client to use the proxy server. This is particularly useful in environments where direct internet access is restricted.
You will need to configure
- Docker Client - to set proxy environment variables for containers started by the Docker CLI.
- Docker Daemon - to set proxy environment variables for the Docker service itself (for pulling images, etc.).
Configure the Docker Client
If you don’t want to configure the proxy for Docker client, you can also set the proxy configuration on the command line when running or building containers, like this:
docker build --build-arg HTTP_PROXY=http://your-proxy-server:port .
docker run -e HTTP_PROXY=http://your-proxy-server:port <image>But configuring it in the Docker client configuration file is more convenient for regular use.
Please don’t use ENV in Dockerfile to set proxy for Docker client, as it will bake the proxy settings into the image, which may not be desirable. Use the methods mentioned above instead.
Basically, you need to create or modify the config.json file located in the Docker configuration directory (usually ~/.docker/ on Linux and macOS, and %USERPROFILE%\.docker\ on Windows).
The following configuration sets proxy environment variables for Docker containers started by the Docker CLI (client), but does not configure proxy settings for the Docker Daemon itself.
{
"proxies": {
"default": {
"httpProxy": "http://your-proxy-server:port",
"httpsProxy": "http://your-proxy-server:port",
"noProxy": "localhost,.local,127.0.0.1,.example.org,*.yourdomain.com"
}
}
}After you create or modify the file, when you run Docker commands that start containers, the specified proxy settings will be applied to those containers.
docker run <image> sh -c 'env | grep -i _proxy'
# You should see output similar to:
http_proxy=http://your-proxy-server:port
https_proxy=http://your-proxy-server:port
no_proxy=localhost,.local,127.0.0.1,.example.org,*.yourdomain.comConfigure the Docker Daemon
You can refer https://docs.docker.com/engine/daemon/proxy/Â for more details like Rootless mode, etc.
If you want to pull images or interact with registries through a proxy, you need to configure the Docker Daemon to use the proxy server. Normally, Docker Daemon will run as a systemd service on Linux.
-
Create a directory for Docker service overrides
sudo mkdir -p /etc/systemd/system/docker.service.d -
Create a configuration file (
/etc/systemd/system/docker.service.d/http-proxy.conf) for the proxy settings./etc/systemd/system/docker.service.d/http-proxy.conf[Service] Environment="HTTP_PROXY=http://your-proxy-server:port" Environment="HTTPS_PROXY=http://your-proxy-server:port" Environment="NO_PROXY=localhost,.local,127.0.0.1,.example.org,*.yourdomain.com" -
Reload the systemd daemon and restart Docker
sudo systemctl daemon-reload sudo systemctl restart docker # or sudo systemctl restart docker.service -
Verify the proxy settings are applied to the Docker Daemon
sudo systemctl show --property=Environment docker
You can also set proxy settings in the /etc/docker/daemon.json file by adding the following configuration, but this method is less common as compared to using systemd service overrides:
{
"proxies": {
"http-proxy": "http://your-proxy-server:port",
"https-proxy": "http://your-proxy-server:port",
"no-proxy": "localhost,.local,127.0.0.1,.example.org,*.yourdomain.com"
}
}After modifying the file, restart the Docker service to apply the changes:
sudo systemctl restart docker