Skip to content

Installing Docker CE

This is how to install the Community Edition of Docker.

Install Prerequisites

Some packages needed to install this apt-repository.

apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

Add Key and Sourcelist

Official solution

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]') $(lsb_release -cs) stable" >> /etc/apt/sources.list.d/docker.list

Generic solution

Probably a better than the official one using a sources-file.

cat <<EOD | sudo tee /etc/apt/sources.list.d/docker-ce.sources >/dev/null
Types: deb
URIs: https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]')
Suites: $(lsb_release -cs)
Components: stable
Architectures: $(dpkg --print-architecture)
Signed-By:
$(wget -qO- "https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/gpg" | sed 's/^/ /')
EOD

For older distributions not supporting .sources files.

curl -fsSL "https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/gpg" | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]') stable" >>/etc/apt/sources.list.d/docker.list

Install the Package

sudo apt install docker-ce 

Sanity Check

Check that the package is coming from the docker repository.

apt-cache policy docker-ce

Add Group to User

To allow a non-root user to use docker add the docker group to the user.

usermod -aG docker <login>

Move Docker Storage to Another Device

By default, the Docker storage location is /var/lib/docker.
To check the where the location is, execute this command.

docker info -f '{{.DockerRootDir}}'  

Change it by following the next steps.

1) Stop the Services

systemctl stop docker docker.socket containerd

2) Move the Existing Data

mv /var/lib/docker /mnt/my-disk/docker

3) Configuration Changes

Change: Storage Location Image Layers

Modify the file /etc/docker/daemon.json by adding the next part to the possible existing configuration.

{
  "data-root": "/mnt/my-disk/docker"
}

Log to journald for all Daemon Containers

Modify the file /etc/docker/daemon.json by adding the next part to the possible existing configuration.

{
  "log-driver": "journald"
}

Add TCP port for Daemon

Modify the file /etc/docker/daemon.json by adding the next part to the possible existing configuration.

{
  "hosts": [
    "unix:///run/docker.sock",
    "tcp://127.0.0.1:2375"
  ]
}

Remove conflicting json-file option from service file /usr/lib/systemd/system/docker.service by issuing command systemctl edit docker.service and insert the section and entries to be changed like this:

[Service]
# Clear the entry before assigning a complete new one. (Causes an error otherwise.)
ExecStart=
ExecStart=/usr/bin/dockerd --containerd=/run/containerd/containerd.sock

The file /etc/systemd/system/docker.service.d/override.conf is created upon closing the editor.

1) Start the Services

Start or restart the services.

systemctl start docker docker.socket containerd
systemctl restart docker docker.socket containerd