How to install Docker on Debian

Aryan Arabshahi
April 15, 2021
Share:

Docker is one of the most popular services that ever made. It allows you to create an isolated environment for development or deployment. With Docker, you can manage your infrastructure in the same way as you manage your applications. By using Docker, you can design high-performance scalable applications. It's one of the best solutions for microservices deployment and allows you to orchestrate your application on multiple servers. In this article, We are going to install Docker on Debian 10 based on its official instruction.

To install Docker and docker-compose, use the automatic installation script:

wget https://raw.githubusercontent.com/aryan-arabshahi/bash-scripts/master/install-docker.sh
bash install-docker.sh

And the result will be:

Docker automatic installation script

Let's have a look into the script:

First of all, it checks the root access for the installation, after that it detects the OS-ID from the “os-release” file to run the proper install instruction:

#!/usr/bin/env bash

check_root_access() {
    if [[ $EUID -ne 0 ]]; then
        abort "\n[!] Run script as root.\n"
    fi
}

get_os_id() {
    echo $(cat /etc/os-release | grep -w "ID=*" | sed 's/ID=//g')
}

abort() {
    MESSAGE=$1
    if [[ !(-z $MESSAGE) ]]; then
        echo -e $MESSAGE
    fi
    exit 1;
}

# Abort script if there is no root access
check_root_access

case $(get_os_id) in
    debian )
        install_on_debian
        ;;
    * )
        abort "[-] Unknown OS!"
        ;;
esac


For the Debian based operating systems, the installation instruction contains the following steps:

1. Update the apt package index and install packages to allow apt to use a repository over HTTPS:

apt update && apt install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release


2. Add Docker’s official GPG key:

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


3. Use the following command to set up the stable repository:

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


4. Update the apt package index, and install the latest version of Docker Engine and contained:

apt update && apt install -y \
    docker-ce \
    docker-ce-cli \
    containerd.io


So, The installation function is:

install_on_debian() {

    echo -e "\n[+] Installing Docker\n"

    apt update && apt install -y \
        apt-transport-https \
        ca-certificates \
        curl \
        gnupg \
        lsb-release

    curl -fsSL https://download.docker.com/linux/debian/gpg | \
        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/debian $(lsb_release -cs) stable" \
        | tee /etc/apt/sources.list.d/docker.list > /dev/null

    apt update && apt install -y \
        docker-ce \
        docker-ce-cli \
        containerd.io

    echo -e "\n[+] Installing docker-compose\n"

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

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

    return 0
}


Now, docker and docker-compose commands have been installed.