How to set proxy for all docker containers?

method one

To set up a proxy for all Docker containers, you can follow these steps:

  1. 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.dand /etc/systemd/system/docker.service.d/http-proxy.confadd 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-proxyand your-portwith your proxy server and port number, and NO_PROXYconfigure with a hostname or IP address that does not require a proxy to be used.

  1. 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.

reference

https://docs.docker.com/network/proxy/

Guess you like

Origin blog.csdn.net/bigbaojian/article/details/129699708