Note: Combined all resources into one.
To run Docker in Ubuntu with a proxy, follow these steps:
-
Set the proxy in the Docker daemon by editing the Docker service file:
sudo systemctl edit docker.service
Then paste the following:
# A Systemd service definition to set proxy configuration for Docker engine on Linux [Service] Environment="HTTP_PROXY=http://proxy:proxy_port" Environment="HTTPS_PROXY=http://proxy:proxy_port" Environment="NO_PROXY=localhost,127.0.0.1"
If authentication is required, provide your laptop credentials:
[Service] Environment="HTTP_PROXY=http://username:password@proxy:proxy_port" Environment="HTTPS_PROXY=http://username:password@proxy:proxy_port" Environment="NO_PROXY=localhost,127.0.0.1"
-
Set the proxy for Docker client to run and pull images:
sudo mkdir ~/.docker/ sudo nano ~/.docker/config.json
Then paste the following:
{ "proxies": { "default": { "httpProxy": "http://proxy:proxy_port", "httpsProxy": "http://proxy:proxy_port", "ftpProxy": "http://proxy:proxy_port", "noProxy": "*.test.example.com,.example2.com,127.0.0.0/8" } } }
If authentication is required, provide your laptop credentials:
{ "proxies": { "default": { "httpProxy": "http://username:password@proxy:proxy_port", "httpsProxy": "http://username:password@proxy:proxy_port", "ftpProxy": "http://username:password@proxy:proxy_port", "noProxy": "*.test.example.com,.example2.com,127.0.0.0/8" } } }
-
Set the proxy for Docker Compose:
Create a folder for configuring Docker service through systemd:
mkdir /etc/systemd/system/docker.service.d
Create a service configuration file at
/etc/systemd/system/docker.service.d/http-proxy.conf
, and paste the following:[Service] # NO_PROXY is optional and can be removed if not needed # Change proxy_url to your proxy IP or FQDN and proxy_port to your proxy port # For Proxy server which require username and password authentication, just add the proper username and password to the URL. (see example below) # Example without authentication Environment="HTTP_PROXY=http://proxy_url:proxy_port" "NO_PROXY=localhost,127.0.0.0/8" # Example with authentication Environment="HTTP_PROXY=http://username:password@proxy_url:proxy_port" "NO_PROXY=localhost,127.0.0.0/8"
-
Reload
systemctl
so that the new settings are read:sudo systemctl daemon-reload
Verify that the Docker service environment is properly set:
sudo systemctl show docker --property Environment
-
Restart the Docker service so that it uses the updated environment settings:
sudo systemctl restart docker
Another: To Set proxy in Dockerfile
To make the Docker images accessible to the proxy, you need to set the proxy in the Dockerfile that contains all the commands to be executed for the Docker images. You can do this by adding the following environment variables to the Dockerfile:
ENV http_proxy http://proxy:proxy_port
ENV https_proxy http://proxy:proxy_port
If the above doesn’t work then you need to set authentication also, try below
ENV http_proxy http://username:password@proxy_url:proxy_port
ENV https_proxy http://username:password@proxy_url:proxy_port
Below is an example of how you can set up a proxy in a Dockerfile:
# First Stage
FROM python:3.8.7-slim-buster AS compile-image
ENV http_proxy http://proxy:proxy_port
ENV https_proxy http://proxy:proxy_port
RUN apt-get update
&& apt-get install -y --no-install-recommends build-essential gcc
# Virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"