steps to install docker on Ubuntu 20

1
2
3
4
5
6
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install docker-ce

verify the installation

1
sudo systemctl status docker

add current user to docker group

  • in order to avoid adding sudo when running docker command
1
sudo usermod -aG docker ${USER}

Network proxy settings

Dockerd Proxy

To configure the proxy for dockerd, which is managed by systemd, create and edit a configuration file:

1
2
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo touch /etc/systemd/system/docker.service.d/proxy.conf

Add the following to proxy.conf:

1
2
3
4
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:8080/"
Environment="HTTPS_PROXY=http://proxy.example.com:8080/"
Environment="NO_PROXY=localhost,127.0.0.1,.example.com"

Replace proxy.example.com:8080 with your proxy address.

Container Proxy

To configure proxy settings for containers, edit ~/.docker/config.json:

1
2
3
4
5
6
7
8
9
{
 "proxies": {
   "default": {
     "httpProxy": "http://proxy.example.com:8080",
     "httpsProxy": "http://proxy.example.com:8080",
     "noProxy": "localhost,127.0.0.1,.example.com"
   }
 }
}

For container runtime proxies, use the -e option to set environment variables.

Docker Build Proxy

For docker build, inject proxy settings using --build-arg:

1
2
3
4
5
docker build . \
    --build-arg "HTTP_PROXY=http://proxy.example.com:8080/" \
    --build-arg "HTTPS_PROXY=http://proxy.example.com:8080/" \
    --build-arg "NO_PROXY=localhost,127.0.0.1,.example.com" \
    -t your/image:tag

Applying Changes

To apply the changes:

1
2
sudo systemctl daemon-reload
sudo systemctl restart docker

Trouble shooting

when running command sudo apt update, it has following error

1
2
3
4
5
6
7
8
Err:5 https://download.docker.com/linux/ubuntu focal Release                                            
  Could not handshake: The TLS connection was non-properly terminated. [IP: <xx.xxx.xx.xxx> 3128]
Hit:6 http://security.ubuntu.com/ubuntu focal-security InRelease                                        
Hit:7 http://us.archive.ubuntu.com/ubuntu focal-security InRelease
Reading package lists... Done
E: The repository 'https://download.docker.com/linux/ubuntu focal Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

Solution

According to this answer, modify /etc/apt/apt.config to make sure the proxy settings are correct, especially the https settings.

  • Wrong config:
Acquire::http::proxy "http://proxy.mydom.it:8080";
Acquire::https::proxy "https://proxy.mydom.it:8080";
  • Good config:
Acquire::http::proxy "http://proxy.mydom.it:8080";
Acquire::https::proxy "http://proxy.mydom.it:8080";