method one
To set up a proxy for all Docker containers, you can follow these steps:
- To set up a proxy server on your Docker host, first create a systemd drop-in directory,
sudo mkdir -p /etc/systemd/system/docker.service.d
and/etc/systemd/system/docker.service.d/http-proxy.conf
add the following to the file to configure the proxy:
cat >> /etc/systemd/system/docker.service.d/http-proxy.conf << EOF
[Service]
Environment="HTTP_PROXY=http://your-proxy:your-port"
Environment="HTTPS_PROXY=http://your-proxy:your-port"
Environment="NO_PROXY=localhost,127.0.0.1,docker-registry.somecorporation.com"
EOF
Replace your-proxy
and your-port
with your proxy server and port number, and NO_PROXY
configure with a hostname or IP address that does not require a proxy to be used.
- Reload the Docker service for the new configuration to take effect:
sudo systemctl daemon-reload
sudo systemctl restart docker
This will restart the Docker service and start all Docker containers with the new proxy settings.
Verify that the configuration loaded and matches your changes, for example:
sudo systemctl show --property=Environment docker
Method Two
You can also use the docker run command to start a new container and include the --env or -e option in the start command to set the proxy environment variable for a specific container, for example:
docker run -e HTTP_PROXY=http://your-proxy:your-port -e HTTPS_PROXY=http://your-proxy:your-port alpine /bin/sh
This will start a new Alpine Linux based container and set the proxy environment variable for the container.
For existing containers, you can use the docker exec command to execute commands, for example:
docker exec -e HTTP_PROXY=http://your-proxy:your-port -e HTTPS_PROXY=http://your-proxy:your-port container-name /bin/sh
This starts a new shell session in the container named container-name and sets the proxy environment variable for that session.
Note that if your container application needs to use specific proxy settings, you may need to do further configuration inside the container. For example, you might need to add the proxy server address and port number to the application configuration file, or install specific proxy client software inside the container.