Common proxy configuration.

This is an automatically translated post by LLM. The original post is in Chinese. If you find any translation errors, please leave a comment to help me improve the translation. Thanks!

In the development process, due to well-known reasons, it is inevitable to use proxies. However, the ways in which various software uses proxies are different. This article provides a simple summary of proxy settings for commonly used software.

Shell

The most convenient way is to directly set the proxy in the command line. This way, most programs executed in the command line will use the proxy settings as follows:

1
2
export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890

You can also write the above commands into the ~/.bashrc file for automatic execution.

For ease of writing, the default port 7890 of the software CatHead is used here. Modify it according to the proxy server port when actually using.

Docker

Proxy usage in Docker mainly falls into the following scenarios:

  1. Docker itself downloads images through a proxy.
  2. When building images with docker build, commands executed use a proxy for access.
  3. When running containers with docker run, the containers internally use the host machine's proxy for access.

Next, we will explain the proxy configuration for these scenarios one by one.

1. Docker Proxy Configuration

When executing docker pull, it is done by the daemon dockerd. Therefore, the proxy needs to be configured in the environment of dockerd, which is controlled by systemd. Thus, it is actually a configuration of systemd.

1
2
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo touch /etc/systemd/system/docker.service.d/proxy.conf

In this proxy.conf file (can be in any *.conf form), add the following content:

1
2
3
4
[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"

Restart for changes to take effect:

1
2
sudo systemctl daemon-reload
sudo systemctl restart docker

2. Docker Build Proxy Configuration

1
2
3
4
5
docker build \
--build-arg http_proxy=http://proxy:7890 \
--build-arg https_proxy=http://proxy:7890 \
--network=host \
-t image_name:image_version \

3. Docker Run Proxy Configuration

Docker run uses host proxy.

Wget

wget is commonly used for file downloads and sometimes requires a proxy. There are three main ways wget accesses through a proxy:

  1. Directly set system environment variables.

  2. Modify the configuration file:

    Edit the configuration file:

    1
    vim ~/.wgetrc

    Write the following content:

    1
    2
    3
    http_proxy=http://127.0.0.1:7890
    https_proxy=http://127.0.0.1:7890
    use_proxy=on
  3. Use the -e parameter:

    1
    wget -e "http_proxy=http://127.0.0.1:7890" -e "https_proxy=http://127.0.0.1:7890" https://google.com

Microsoft Store

This needs to be discussed separately(calling out) because it not only does not use system proxies but also fails to connect to servers after enabling system proxies.

1
CheckNetIsolation.exe loopbackexempt -a -p=S-1-15-2-1609473798-1231923017-684268153-4268514328-882773646-2760585773-1760938157

References

  1. 如何优雅的给 Docker 配置网络代理 - CharyGao - 博客园 (cnblogs.com)