Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to set the network proxy in the Docker container

Shulou Source: shulou.com Published: 2022-06-01 17:30:10 10月05日 Update

How to set up a network proxy in the Docker container? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Set environment variabl

Setting the container environment variable is also the most direct way. When you start the container, you pass the environment variables into the container by setting the flag of-- env.

Dockerfile sample Docker run example HTTP_PROXYENV HTTP_PROXY "http://127.0.0.1:3001"--env HTTP_PROXY=" http://127.0.0.1:3001"HTTPS_PROXYENV HTTPS_PROXY "https://127.0.0.1:3001"--env HTTPS_PROXY=" https://127.0.0.1:3001"FTP_PROXYENV FTP_PROXY "ftp://127.0.0.1:3001"--env FTP_ PROXY= "ftp://127.0.0.1:3001"NO_PROXYENV NO_PROXY" * .test.example.com .example2.com "--env NO_PROXY=" * .test.example.com, .example2.com "

The advantage of this approach is straightforward, but the disadvantage is that every time you start the container, you need to display the settings, so you will have trouble compiling some projects. For example, the kubevirt project will start the container to download the go project in the code, and you need to use a proxy, so you must manually find the location of the startup container, and the display plus the above environment variables increases a lot of work. To solve this situation, you can configure the Docker client above Docker 17.07.

Configure the Docker client

Precondition: Docker 17.07or above.

In the Home directory of the docker user, create the ~ / .docker / config.json file and add the settings for http_proxy, https_proxy, ftp_proxy, etc. Of course, you can also configure multiple proxy servers at the same time.

For paths that do not want to use no_proxy, you can also add the configuration of no_proxy. The contents of no_proxy are separated by commas and the wildcard character * is supported.

Example:

{"proxies": {"default": {"httpProxy": "http://127.0.0.1:3001"," httpsProxy ":" http://127.0.0.1:3001", "noProxy": "* .test.example.com, .example2.com"}

Start the container and you can see that the agent will be automatically added to the container.

This is the answer to the question about how to set up a network proxy in the Docker container. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Tags: Containers agents variables environment configuration ways examples questions projects networks content customers that is more help answers easy easy code advantages Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Xiaomi MariaDB Shulou Technology Shulou Tech Info