|
| 1 | +--- |
| 2 | +title: Configure Docker to use a proxy server |
| 3 | +description: How to configure the Docker client to use a proxy server |
| 4 | +keywords: network, networking, proxy, client |
| 5 | +--- |
| 6 | + |
| 7 | +If your container needs to use an HTTP, HTTPS, or FTP proxy server, you can |
| 8 | +configure it in different ways: |
| 9 | + |
| 10 | +- In Docker 17.07 and higher, you can |
| 11 | + [configure the Docker client](#configure-the-docker-client) to pass |
| 12 | + proxy information to containers automatically. |
| 13 | + |
| 14 | +- In Docker 17.06 and lower, you must |
| 15 | + [set appropriate environment variables](#use-environment-variables) |
| 16 | + within the container. You can do this when you build the image (which makes |
| 17 | + the image less portable) or when you create or run the container. |
| 18 | + |
| 19 | +## Configure the Docker client |
| 20 | + |
| 21 | +1. On the Docker client, create or edit the file `~/.docker/config.json` in the |
| 22 | + home directory of the user which starts containers. Add JSON such as the |
| 23 | + following, substituting the type of proxy with `httpsProxy` or `ftpProxy` if |
| 24 | + necessary, and substituting the address and port of the proxy server. You |
| 25 | + can configure multiple proxy servers at the same time. |
| 26 | + |
| 27 | + You can optionally exclude hosts or ranges from going through the proxy |
| 28 | + server by setting a `noProxy` key to one or more comma-separated IP |
| 29 | + addresses or hosts. Using the `*` character as a wildcard is supported, as |
| 30 | + shown in this example. |
| 31 | + |
| 32 | + ```json |
| 33 | + { |
| 34 | + "proxies": |
| 35 | + { |
| 36 | + "default": |
| 37 | + { |
| 38 | + "httpProxy": "http://127.0.0.1:3001", |
| 39 | + "noProxy": "*.test.example.com,.example2.com" |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + ``` |
| 44 | + |
| 45 | + Save the file. |
| 46 | + |
| 47 | + 2. When you create or start new containers, the environment variables are |
| 48 | + set automatically within the container. |
| 49 | + |
| 50 | + |
| 51 | +## Use environment variables |
| 52 | + |
| 53 | +### Set the environment variables manually |
| 54 | + |
| 55 | +When you build the image, or using the `--env` flag when you create or run the |
| 56 | +container, you can set one or more of the following variables to the appropriate |
| 57 | +value. This method makes the image less portable, so if you have Docker 17.07 |
| 58 | +or higher, you should [configure the Docker client](#configure-the-docker-client) |
| 59 | +instead. |
| 60 | + |
| 61 | +| Variable | Dockerfile example | `docker run` Example | |
| 62 | +|:--------------|:--------------------------------------------------|:----------------------------------------------------| |
| 63 | +| `HTTP_PROXY` | `ENV HTTP_PROXY "http://127.0.0.1:3001"` | `--env HTTP_PROXY "http://127.0.0.1:3001"` | |
| 64 | +| `HTTPS_PROXY` | `ENV HTTPS_PROXY "https://127.0.0.1:3001"` | `--env HTTPS_PROXY "https://127.0.0.1:3001"` | |
| 65 | +| `FTP_PROXY` | `ENV FTP_PROXY "ftp://127.0.0.1:3001"` | `--env FTP_PROXY "ftp://127.0.0.1:3001"` | |
| 66 | +| `NO_PROXY` | `ENV NO_PROXY "*.test.example.com,.example2.com"` | `--env NO_PROXY "*.test.example.com,.example2.com"` | |
| 67 | + |
0 commit comments