How to Configure Docker to Use Proxy?

A proxy is utilized to improve the performance of Docker by caching frequently requested resources. It reduces the number of requests made to the internet and results in faster resource access. In Docker, the proxy information can be configured in the Docker configuration file or passed as environment variables when starting the Docker daemon.

This article will illustrate various methods to configure the docker using the proxy. 

How to Configure Docker to Use Proxy?

To configure Docker to use a proxy on Linux, you’ll need to edit the Docker daemon configuration file. The location of this file is typically located at “/etc/docker/daemon.json” (if not, you can create it as well).

Prerequisite: Check the Docker Services

Before starting configuration in Docker, it is necessary to ensure that the Docker service is actively running: 

$ sudo systemctl status docker

The output shows that Docker services are in an active state.

Step 1: Open Docker Configuration File

Open the Docker daemon file “/etc/docker/daemon.json” in a text editor:

$ sudo nano /etc/docker/daemon.json

Step 2: Add the Following JSON Object to the File

After navigating the particular file, add the following JSON object to the file, replacing the placeholder values with the appropriate information to httpProxy, httpsProxy, and noProxy:

{
  "proxies":
  {
    "default":
    {
      "httpProxy": "http://proxy.example.com:80",
      "httpsProxy": "https://proxy.example.com:443",
      "noProxy": "localhost,127.0.0.1"
    }
  }
}

Alternate Method: Using Environment Variable

Alternatively, users can access the “docker” configuration file that is located in the “/etc/default/docker” directory to perform modifications:

$ sudo nano /etc/default/docker

In our case, assign the “http://127.0.0.1:3128/” value to the “http_proxy” environment variable as below:

Alternate Method 2: Using Docker Services

User can manually assign the services by accessing the “http-proxy.conf” that is located in the “/etc/systemd/system/docker.service.d” directory:

$ sudo nano /etc/systemd/system/docker.service.d/http-proxy.conf

Add the following lines in the “http-proxy.conf” file as below:

[Service]
Environment="HTTP_PROXY=http://proxy.example.com:80"
[Service]
Environment="HTTPS_PROXY=https://proxy.example.com:443"

Step 3: Restart the Docker daemon 

To configure the dependent files, restart the Docker daemon. For this, execute the “restart” utility as follows:

$ sudo service docker restart

Step 4: Verify the Proxy Settings

Users can verify the proxy setting through the “docker info” command as below:

$ sudo docker info

The output shows the proxy information displayed in the terminal, indicating that Docker is now configured to use the proxy.

Conclusion

To configure Docker to use a proxy, the user can set the proxy information in the Docker daemon “/etc/docker/daemon.json” configuration file or pass the proxy information as environment variables. To set proxy information in the Docker daemon file, edit the /etc/docker/daemon.json file and add the proxy information. This article has explained step by step procedure to configure the docker to use the proxy.