Menu
Buy VPS
Payments
Tools
User

DropVPS Team

Writer: Cooper Reagan

How to set proxy for Docker in Ubuntu?

How to set proxy for Docker in Ubuntu?

Publication Date

Category

Articles

Reading Time

3 Min

Table of Contents

Configuring Docker to use a proxy server is essential in environments where direct internet access is restricted or when you need to route traffic through a specific proxy. In this guide, we will walk you through the steps to set up a proxy for Docker on an Ubuntu system.

Step 1: Identify Your Proxy Server

Before configuring Docker, you need the details of your proxy server, including the IP address and port number. The proxy format typically looks like this:

http://username:[email protected]:port

If your proxy does not require authentication, it will look like:

http://proxyserver.com:port

Step 2: Configure Docker to Use the Proxy

In this step, we will configure Docker to route its network traffic through a specified proxy server. This is essential for environments where direct internet access is not available, or to enforce network policies. There are two primary methods to achieve this:

Method 1: Set Proxy in Docker Service Configuration

  1. Create or Edit the Systemd Drop-in File:

    Docker uses systemd to manage its service. You can create a drop-in file to set the proxy settings. Open a terminal and create a directory for the Docker service configuration if it doesn’t exist:

  2. Create/Edit the Configuration File:

    Create or edit the http-proxy.conf file:

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

    Add the following lines, replacing the placeholders with your actual proxy details:

    [Service]
    Environment="HTTP_PROXY=http://username:[email protected]:port/"
    Environment="HTTPS_PROXY=http://username:[email protected]:port/"
    Environment="NO_PROXY=localhost,127.0.0.1"

    If your proxy does not require authentication, you can simplify it:

    [Service]
    Environment="HTTP_PROXY=http://proxyserver.com:port/"
    Environment="HTTPS_PROXY=http://proxyserver.com:port/"
    Environment="NO_PROXY=localhost,127.0.0.1"
  3. Reload Systemd Configuration:

    After saving the changes, reload the systemd configuration to apply the changes:

    sudo systemctl daemon-reload
  4. Restart Docker Service:

    Restart the Docker service to apply the new proxy settings:

    sudo systemctl restart docker

Method 2: Configure Proxy in Docker Daemon Configuration File

Alternatively, you can configure the proxy settings directly in the Docker daemon configuration file.

  1. Edit the Docker Daemon Configuration File:

    Open or create the daemon.json file:

    sudo nano /etc/docker/daemon.json

     

  2. Add Proxy Settings:

    Add the following JSON configuration, replacing the placeholders as necessary:

    {
      "proxies": {
        "default": {
          "httpProxy": "http://username:[email protected]:port/",
          "httpsProxy": "http://username:[email protected]:port/",
          "noProxy": "localhost,127.0.0.1"
        }
      }
    }

    If your proxy does not require authentication, it will look like this:

    {
      "proxies": {
        "default": {
          "httpProxy": "http://proxyserver.com:port/",
          "httpsProxy": "http://proxyserver.com:port/",
          "noProxy": "localhost,127.0.0.1"
        }
      }
    }
  3. Restart Docker Service:
    Save the changes and restart the Docker service:

    sudo systemctl restart docker

Step 3: Testing Your Proxy Configuration

To verify that Docker is using the proxy settings, you can pull an image from Docker Hub. Run:

docker run hello-world

If the image pulls successfully, your proxy configuration is working correctly.

By following these steps, you have successfully configured Docker to use a proxy in Ubuntu. This setup is crucial for managing containerized applications in restricted network environments. For more tips and tutorials, keep visiting DropVPS.com!

Linux VPS

Follow DropVPS on Google

Add DropVPS as a preferred source to see our latest VPS guides and tutorials surfaced first in Google Search and AI Overviews.

🐧Linux VPS

Need a Linux Server for This?

Run Debian, Ubuntu, or any Linux distro on DropVPS — fast NVMe SSD, full root access, and 24/7 support. Perfect for everything you just read.

  • Full Root Access
  • Debian & Ubuntu Ready
  • 99.99% Uptime
  • 24/7 Support
Get Linux VPS →

No commitment · Cancel anytime

U
Loading...

Related Posts