Configure proxy for Docker
For Docker Engine
Reference: https://docs.docker.com/engine/daemon/proxy/#systemd-unit-file
Create a systemd drop-in directory for the docker service:
sudo mkdir -p /etc/systemd/system/docker.service.d
Create and edit a configuration file for proxy:
sudo nano /etc/systemd/system/docker.service.d/http-proxy.conf
With the following content:
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:3128"
Environment="HTTPS_PROXY=http://proxy.example.com:3128"
Environment="NO_PROXY=localhost,127.0.0.0/8,::1"
Flush changes:
sudo systemctl daemon-reload
Restart Docker:
sudo systemctl restart docker
Verify that the configuration is applied:
sudo systemctl show --property=Environment docker
The configuration should appear in the output. Example:
Environment=HTTP_PROXY=http://proxy.example.com:3128 HTTPS_PROXY=http://proxy.example.com:3128 NO_PROXY=localhost,127.0.0.0/8,::1
For a Docker container
Reference: https://forums.docker.com/t/how-to-reach-localhost-on-host-from-docker-container/113321
Accessing a proxy server running on the host machine
In the docker-compose file:
services:
container:
...
environment:
...
HTTP_PROXY: http://proxy.example.com:3128
HTTPS_PROXY: http://proxy.example.com:3128
NO_PROXY: "localhost,127.0.0.0/8,::1"
extra_hosts:
- "host.docker.internal:host-gateway"
...
NoteFirewall rules may apply to the traffic between Docker’s virtual Ethernet adapter and the host machine’s Ethernet adapter. It might be a good idea to check the firewall configurations.
Restart the stack:
docker compose up -d
Accessing a proxy server elsewhere
The extra_hosts field is no longer needed. Simply configure the environment. Example:
services:
container:
...
environment:
...
HTTP_PROXY: http://host.docker.internal:3128
HTTPS_PROXY: http://host.docker.internal:3128
NO_PROXY: "localhost,127.0.0.0/8,::1"
...