DropVPS Team
Writer: Cooper Reagan
How to set proxy for Docker in Ubuntu?

Table of Contents
What you will read?
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
-
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:
-
Create/Edit the Configuration File:
Create or edit the
http-proxy.conffile:sudo nano /etc/systemd/system/docker.service.d/http-proxy.confAdd 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" -
Reload Systemd Configuration:
After saving the changes, reload the systemd configuration to apply the changes:
sudo systemctl daemon-reload -
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.
-
Edit the Docker Daemon Configuration File:
Open or create the
daemon.jsonfile:sudo nano /etc/docker/daemon.json - 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" } } } - 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!