< All posts

Aug. 28, 2021, 6:21 p.m.

Installation of Docker on Debian

Docker Installation on Debian

What is docker?

Docker is an open-source containerization software platform that allows you to build, test, and deploy applications quickly.

Installation on Debian

Follow the steps below to install the Docker Engine.
First we need to set up the Docker repository for us to use the latest and stable version of Docker instead of using the Docker packages that is built in Debian.
Fire up your shell and execute the following commands.

Update the package cache from the internet

sudo apt-get update

Command output:

[sudo] password for user:                                         
Get:1 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB]                                                                         
Hit:2 http://deb.debian.org/debian buster InRelease                                                             
Get:3 http://deb.debian.org/debian buster-updates InRelease [51.9 kB]              
Get:4 http://security.debian.org/debian-security buster/updates/main Sources [187 kB]
Get:5 http://security.debian.org/debian-security buster/updates/main amd64 Packages [292 kB]
Get:6 http://security.debian.org/debian-security buster/updates/main Translation-en [151 kB]
Fetched 748 kB in 1s (1,085 kB/s)                             
Reading package lists... Done         
Building dependency tree                               
Reading state information... Done     
All packages are up to date.                      

Install the necessary requirements for us to import the docker repo key

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

Command output:

[sudo] password for user:                
Reading package lists... Done
Building dependency tree
Reading state information... Done            
ca-certificates is already the newest version (20200601~deb10u2).                                                                                           
gnupg is already the newest version (2.2.12-1+deb10u1).       
lsb-release is already the newest version (10.2019051400).
curl is already the newest version (7.64.0-4+deb10u2).             
The following NEW packages will be installed:
  apt-transport-https                                                                                                                                       
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.                                                                                              
Need to get 149 kB of archives.                                                                                                                             
After this operation, 156 kB of additional disk space will be used.                                                                                         
Do you want to continue? [Y/n] y                                                                                                                            
Get:1 http://deb.debian.org/debian buster-updates/main amd64 apt-transport-https all 1.8.2.3 [149 kB]                                                       
Fetched 149 kB in 0s (1,564 kB/s)
Selecting previously unselected package apt-transport-https.
(Reading database ... 39343 files and directories currently installed.)                                                                                     
Preparing to unpack .../apt-transport-https_1.8.2.3_all.deb ...
Unpacking apt-transport-https (1.8.2.3) ...
Setting up apt-transport-https (1.8.2.3) ...       

Import the Docker’s official GPG key

 curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Importing the repo (assuming amd64/x86_64 architecture)

 echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Update the package cache and proceed to install Docker Engine

sudo apt update
sudo apt install docker-ce docker-ce-cli container.io

Command output

The following additional packages will be installed:  


Reading state information... Done                                                                                                                    [44/672]
The following additional packages will be installed:
  docker-ce-rootless-extras docker-scan-plugin libltdl7                                                                                                     
Suggested packages:                                                                                                                                         
  aufs-tools cgroupfs-mount | cgroup-lite
Recommended packages:
  slirp4netns
The following NEW packages will be installed:
  containerd.io docker-ce docker-ce-cli docker-ce-rootless-extras docker-scan-plugin libltdl7                                                               
0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
Need to get 108 MB of archives.
After this operation, 466 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://deb.debian.org/debian buster/main amd64 libltdl7 amd64 2.4.6-9 [390 kB]                                                                        
Get:2 https://download.docker.com/linux/debian buster/stable amd64 containerd.io amd64 1.4.6-1 [28.3 MB]                                                    
Get:3 https://download.docker.com/linux/debian buster/stable amd64 docker-ce-cli amd64 5:20.10.7~3-0~debian-buster [41.4 MB]                                
Get:4 https://download.docker.com/linux/debian buster/stable amd64 docker-ce amd64 5:20.10.7~3-0~debian-buster [24.8 MB]                                    
Get:5 https://download.docker.com/linux/debian buster/stable amd64 docker-ce-rootless-extras amd64 5:20.10.7~3-0~debian-buster [9,062 kB]                   
Get:6 https://download.docker.com/linux/debian buster/stable amd64 docker-scan-plugin amd64 0.8.0~debian-buster [3,889 kB]                                  
Fetched 108 MB in 9s (12.1 MB/s)
Selecting previously unselected package containerd.io.
(Reading database ... 39347 files and directories currently installed.)                                                                                     
Preparing to unpack .../0-containerd.io_1.4.6-1_amd64.deb ...
Unpacking containerd.io (1.4.6-1) ...
Selecting previously unselected package docker-ce-cli.
Preparing to unpack .../1-docker-ce-cli_5%3a20.10.7~3-0~debian-buster_amd64.deb ...                                                                         
Unpacking docker-ce-cli (5:20.10.7~3-0~debian-buster) ...
Selecting previously unselected package docker-ce.
Preparing to unpack .../2-docker-ce_5%3a20.10.7~3-0~debian-buster_amd64.deb ...                                                                             
Unpacking docker-ce (5:20.10.7~3-0~debian-buster) ...
Selecting previously unselected package docker-ce-rootless-extras.
Preparing to unpack .../3-docker-ce-rootless-extras_5%3a20.10.7~3-0~debian-buster_amd64.deb ...                                                             
Unpacking docker-ce-rootless-extras (5:20.10.7~3-0~debian-buster) ...
Selecting previously unselected package docker-scan-plugin.
Preparing to unpack .../4-docker-scan-plugin_0.8.0~debian-buster_amd64.deb ...
Unpacking docker-scan-plugin (0.8.0~debian-buster) ...
Selecting previously unselected package libltdl7:amd64.
Preparing to unpack .../5-libltdl7_2.4.6-9_amd64.deb ...
Unpacking libltdl7:amd64 (2.4.6-9) ...
Setting up docker-scan-plugin (0.8.0~debian-buster) ...
Setting up containerd.io (1.4.6-1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /lib/systemd/system/containerd.service.                                    
Setting up libltdl7:amd64 (2.4.6-9) ...
Setting up docker-ce-cli (5:20.10.7~3-0~debian-buster) ...
Setting up docker-ce-rootless-extras (5:20.10.7~3-0~debian-buster) ...
Setting up docker-ce (5:20.10.7~3-0~debian-buster) ...

Setting up docker-ce-rootless-extras (5:20.10.7~3-0~debian-buster) ...
Setting up docker-ce (5:20.10.7~3-0~debian-buster) ...
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service.
Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /lib/systemd/system/docker.socket.
Processing triggers for systemd (241-7~deb10u7) ...
Processing triggers for libc-bin (2.28-10) ...          

Testing the installation by downloading and executing a hello world docker application

sudo docker run hello-world

Sample output:

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest: sha256:9f6ad537c5132bcce57f7a0a20e317228d382c3cd61edae14650eec68b2b345c
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

We have completed the installation, however we just run a command under sudo which is not recommended. Proceed below for us to use docker commands without super user privileges.

Operating Docker as non-root user

Warning

The docker group grants privileges equivalent to the root user.

Create a docker group, if not yet created by docker upon installation

 sudo groupadd docker

Add your current user ($USER parameter automatically detects your current logged in username) to that group

sudo usermod -aG docker $USER

You can replace $USER with the name of other user that you want to be able to use docker without super user privileges.

Activate changes to the groups

newgrp docker 

Test that you can run docker commands without using sudo

docker run hello-world

Command output


Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Installing docker-compose

Download the binary

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Apply executable permissions

 sudo chmod +x /usr/local/bin/docker-compose

Test the installation

docker-compose --version

Command output:

$  docker-compose --version
docker-compose version 1.29.2, build 5becea4c

References:

https://docs.docker.com/engine/install/

https://docs.docker.com/compose/install/