This article applies to both Debian 10 Buster and Ubuntu 20.04 Focal.

What is Docker?

Docker is a containerisation technology that allows containers to be built quickly on a server and run software without polluting the host, eliminating the need to install and configure various environments. The open source Docker community is dedicated to improving this type of technology and making it freely available for the benefit of all users.

What is Docker Compose?

Traditionally, ops would need to run docker run to start various containers, and once there are too many containers, it is impossible to remember all the run parameters and commands at once, so we can use Docker Compose to solve this problem.

Docker Compose is a tool to help define and share multi-container applications. With Compose, you can create YAML files to define services and start or clean up everything with a single command. The great advantage of using Compose is that you can define your application stack in a file so that it sits at the root of your project repository (which is now version controlled) and makes it easy for others to participate in your project. Others can simply clone your repository and start writing applications. In fact, you may see a lot of projects on GitHub/GitLab doing this now. (via Using Docker Compose)

Installing Docker using official sources

The following operations need to be done under the root user, so use sudo -i or su root to switch to the root user to do so.

First, install some necessary packages.

1
2
3
apt update
apt upgrade -y
apt install curl vim wget gnupg dpkg apt-transport-https lsb-release ca-certificates

Then add Docker’s GPG public key and apt source.

  • Debian

    1
    2
    
    curl -sSL https://download.docker.com/linux/debian/gpg | gpg --dearmor > /usr/share/keyrings/docker-ce.gpg
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-ce.gpg] https://download.docker.com/linux/debian $(lsb_release -sc) stable" > /etc/apt/sources.list.d/docker.list
    
  • Ubuntu

    1
    2
    
    curl -sSL https://download.docker.com/linux/debian/gpg | gpg --dearmor > /usr/share/keyrings/docker-ce.gpg
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-ce.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -sc) stable" > /etc/apt/sources.list.d/docker.list
    

If you are in China, then you can consider using Tsinghua’s tuna source.

  • Debian

    1
    2
    
    curl -sS https://download.docker.com/linux/debian/gpg | gpg --dearmor > /usr/share/keyrings/docker-ce.gpg
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-ce.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian $(lsb_release -sc) stable" > /etc/apt/sources.list.d/docker.list
    
  • Ubuntu

    1
    2
    
    curl -sS https://download.docker.com/linux/debian/gpg | gpg --dearmor > /usr/share/keyrings/docker-ce.gpg
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-ce.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu $(lsb_release -sc) stable" > /etc/apt/sources.list.d/docker.list
    

You can then update your system and install Docker CE.

1
2
apt update
apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin

At this point you can use the docker version command to check if the installation was successful.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
root@debian ~ # docker version
Client: Docker Engine - Community
 Version:           20.10.22
 API version:       1.41
 Go version:        go1.18.9
 Git commit:        3a2c30b
 Built:             Thu Dec 15 22:28:22 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.22
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.9
  Git commit:       42c8b31
  Built:            Thu Dec 15 22:26:14 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.14
  GitCommit:        9ba4b250366a5ddde94bb7c9d1def331423aa323
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

If you need a specific user to be able to run Docker in Docker rootless mode, then you can add that user to the docker group as well, for example we add the www-data user to it.

1
2
apt install docker-ce-rootless-extras
sudo usermod -aG docker www-data

Installing Docker Compose

Since we already have docker-compose-plugin installed, Docker now comes with the docker compose command, which basically replaces docker-compose.

1
2
root@debian ~ # docker compose version
Docker Compose version v2.14.1

If some images or commands are incompatible, we can also install Docker Compose separately.

We can install the latest version directly using the official Docker distribution Github.

1
2
curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-Linux-x86_64 > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

At this point you can use the docker-compose version command to check if the installation was successful.

1
2
root@debian ~ # docker-compose version
Docker Compose version v2.14.2

Modify Docker configuration

The following configuration will add a custom intranet IPv6 address, enable IPv6 for containers, and limit the log file size to prevent Docker logs from filling up the hard drive.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
cat > /etc/docker/daemon.json << EOF
{
    "log-driver": "json-file",
    "log-opts": {
        "max-size": "20m",
        "max-file": "3"
    },
    "ipv6": true,
    "fixed-cidr-v6": "fd00:dead:beef:c0::/80",
    "experimental":true,
    "ip6tables":true
}
EOF

Then restart the Docker service.

1
systemctl restart docker

Now that we have installed Docker and Docker Compose, we can start having fun installing various software.