# Docker & Ubuntu Proxy

### Setting Up a System-Wide Proxy on Ubuntu

To configure proxy settings that apply to all users, shells and services on an Ubuntu host, add environment variables in one of the following locations.

#### 1. Edit `/etc/environment`

1. Open the file with root privileges:

   ```bash
   sudo nano /etc/environment
   ```
2. Add the proxy definitions (customize host, port and no-proxy list as required):

   ```ini
   http_proxy="http://myproxy.server.com:8080/"
   https_proxy="http://myproxy.server.com:8080/"
   ftp_proxy="http://myproxy.server.com:8080/"
   no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"

   # Upper-case variants for compatibility with certain applications
   HTTP_PROXY="http://myproxy.server.com:8080/"
   HTTPS_PROXY="http://myproxy.server.com:8080/"
   FTP_PROXY="http://myproxy.server.com:8080/"
   NO_PROXY="localhost,127.0.0.1,localaddress,.localdomain.com"
   ```
3. Save and exit (in nano: **Ctrl+O**, **Enter**, **Ctrl+X**).
4. Apply changes immediately by running:

   ```bash
   source /etc/environment
   ```

   Otherwise, the settings will take effect at the next login.

#### 2. Create a Shell Profile Script

An alternative approach uses `/etc/profile.d/` to export variables for all interactive and non-interactive shells.

1. Create the script file:

   ```bash
   sudo nano /etc/profile.d/proxy.sh
   ```
2. Insert the following content:

   ```bash
   #!/bin/sh
   export http_proxy="http://myproxy.server.com:8080/"
   export https_proxy="http://myproxy.server.com:8080/"
   export ftp_proxy="http://myproxy.server.com:8080/"
   export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
   ```
3. Make the script executable:

   ```bash
   sudo chmod +x /etc/profile.d/proxy.sh
   ```
4. Reload the profile or log in again:

   ```bash
   source /etc/profile.d/proxy.sh
   ```

#### 3. Configure APT to Use the Proxy

To ensure that `apt-get` and related tools honor the proxy:

1. Open or create an APT configuration snippet:

   ```bash
   sudo nano /etc/apt/apt.conf.d/95proxies
   ```
2. Add the following lines:

   ```ini
   Acquire::http::proxy  "http://myproxy.server.com:8080/";
   Acquire::https::proxy "http://myproxy.server.com:8080/";
   Acquire::ftp::proxy   "ftp://myproxy.server.com:8080/";
   ```
3. Save, exit and test with:

   ```bash
   sudo apt-get update
   ```

***

### Setting Up a Proxy for Docker

Docker’s daemon and build processes require their own proxy configuration when managed by systemd.

1. Create the systemd drop-in directory if it does not already exist:

   ```bash
   sudo mkdir -p /etc/systemd/system/docker.service.d
   ```
2. Create or edit the proxy configuration file:

   ```bash
   sudo nano /etc/systemd/system/docker.service.d/http-proxy.conf
   ```
3. Add the following content, adapting values as needed:

   ```ini
   [Service]
   Environment="HTTP_PROXY=http://myproxy.server.com:8080/"
   Environment="HTTPS_PROXY=http://myproxy.server.com:8080/"
   Environment="NO_PROXY=localhost,127.0.0.1,docker-registry.example,.corp"
   ```
4. Reload systemd and restart Docker to apply the new settings:

   ```bash
   sudo systemctl daemon-reload
   sudo systemctl restart docker
   ```
5. Verify that the environment variables are active:

   ```bash
   sudo systemctl show --property=Environment docker
   ```
