How to set the Proxy for Docker on Ubuntu

Configure the proxy for Docker on Ubuntu

The Docker services will not use environment variables to get proxy information. Instead, you will have to configure the service to use a proxy. This tutorial will show you how to set the proxy for Docker on Ubuntu servers.

Services in Ubuntu 16.04 and above are registered with SystemD. Docker is one of the few packages that determine proxy information from a service configuration in SystemD, rather than from an environment variable.

Systemd Docker Service

In order to the set the proxy for Docker, you will need to create a configuration file for the Docker service. No configuration files exist by default, so one will have to be created.

All Systemd service configuration are stored under /etc/systemd/system. In order to keep things organized, we can create a child directory docker.service.d, rather than creating a file called /etc/systemd/system/docker.service.

Within this configuration file, we can set our HTTP and HTTPS proxy. We can also set our NO_PROXY value, too.

Creating Proxy Configuration

  1. Create a new directory for our Docker service configurations.
    sudo mkdir -p /etc/systemd/system/docker.service.d
  2. Create a file called proxy.conf in our configuration directory.
    sudo vi /etc/systemd/system/docker.service.d/proxy.conf
  3. Add the following contents, changing the values to match your environment.
  4. Save your changes and exit the text editor.
  5. Reload the daemon configuration.
    sudo systemctl daemon-reload
  6. Restart Docker to apply our changes.
    sudo systemctl restart docker.service

After the service is restarted Docker should be able to pull images from external repositories. You can test this by attempting to pull down an image. If the download completes and does not timeout, your proxy settings have been applied.