# Configuring Docker Proxies

Configure HTTP and HTTPS proxies for the Docker daemon, running containers, and image builds.

## Set up Clash on Linux

The original setup used this reference: [Using Clash on Linux](https://blog.zzsqwq.cn/posts/how-to-use-clash-on-linux/).

## Configure a proxy for dockerd

`docker pull` is performed by the `dockerd` daemon. On this system, systemd manages the daemon, so its HTTP and HTTPS proxy settings need to be supplied through systemd.

Create a configuration directory:

```sh
sudo mkdir -p /etc/systemd/system/docker.service.d
```

Create `/etc/systemd/system/docker.service.d/http-proxy.conf` and add the environment variables:

```
[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"
```

Reload the configuration and restart Docker:

```sh
sudo systemctl daemon-reload
sudo systemctl restart docker
```

## Configure a proxy for containers

For HTTP and HTTPS traffic during container execution, either configure the Docker client or pass environment variables when starting a container.

Create or edit `~/.docker/config.json`:

```json
{
 "proxies":
 {
   "default":
   {
     "httpProxy": "http://proxy.example.com:8080/",
     "httpsProxy": "http://proxy.example.com:8080/",
     "noProxy": "localhost,127.0.0.1,.example.com"
   }
 }
}
```

Alternatively, specify environment variables with `docker run`. The original command is reproduced below:

```sh
docker run --env HTTP_PROXY="http://proxy.example.com:8080/ --env HTTPS_PROXY="http://proxy.example.com:8080/" --env NO_PROXY="localhost,127.0.0.1,.example.com"
```

**Translation note:** this original example has an unmatched quote after the `HTTP_PROXY` value. Close that value before adding the next `--env` option; also supply the image and other arguments needed by your own container.

## Configure a proxy during docker build

During image builds, supply build arguments or use environment variables in the Dockerfile. Here is the original `--build-arg` example:

```
docker build \
    --build-arg "HTTP_PROXY=http://proxy.example.com:8080/" \
    --build-arg "HTTPS_PROXY=http://proxy.example.com:8080/" \
    --build-arg "NO_PROXY=localhost,127.0.0.1,.example.com" .
```

## Further reading

- [Docker: From Beginner to Practice](https://yeasy.gitbook.io/docker_practice/)