Tutorial

Installing Docker and Docker Compose on Debian 12

2024-12-14 #Docker#Docker Compose#Debian#Linux

More and more applications now support one-click deployment using Docker. This article will introduce how to install Docker and Docker Compose on Debian 12.

Verification Environment:

1
2
3
4
5
6
root@debian:~# lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 12 (bookworm)
Release: 12
Codename: bookworm

The installation also applies to Debian 11 and Ubuntu 24.04.

Introduction to Docker

Docker is an open-source containerization platform that packages applications and their dependencies into lightweight, portable containers, enabling them to run consistently in any environment.

  • Lightweight: Containers share the host OS kernel, allowing fast startup times and minimal resource usage.
  • Portable: Containers run consistently across development, testing, and production environments, eliminating the "works on my machine" problem.
  • Isolated: Each container runs independently and does not interfere with others.
  • Fast Deployment: Packaging, distribution, and deployment of applications are swift through image technology.

Introduction to Docker Compose

Docker Compose is a tool provided by Docker for defining and managing multi-container applications. With a simple YAML file named docker-compose.yml, you can define and launch multiple service containers, making deployment and management of applications much more convenient and removing the need to write complex docker run commands.

Features:

  • Multi-container Management: Start, stop, and manage multiple containers simultaneously.
  • YAML-based Configuration: Define services, networks, volumes, etc., in docker-compose.yml.
  • Simplified Commands: Manage containers using a single command: docker compose.

This article will introduce the installation of the V2 version, whose corresponding command is docker compose.

Installing from the Official Source

The following operations must be performed as the root user. Please use sudo -i or su root to switch to root.

First, install some essential 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 the apt repository:

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

For machines in Mainland China, you can use the domestic mirror from Tsinghua TUNA:

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

After that, update the system package lists and install Docker CE and the Docker Compose plugin:

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

Now 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
root@debian:~# docker version
Client: Docker Engine - Community
Version: 27.4.0
API version: 1.47
Go version: go1.22.10
Git commit: bde2b89
Built: Sat Dec 7 10:38:57 2024
OS/Arch: linux/amd64
Context: default

Server: Docker Engine - Community
Engine:
Version: 27.4.0
API version: 1.47 (minimum version 1.24)
Go version: go1.22.10
Git commit: 92a8393
Built: Sat Dec 7 10:38:57 2024
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.7.24
GitCommit: 88bf19b2105c8b17560993bee28a01ddc2f97182
runc:
Version: 1.2.2
GitCommit: v1.2.2-0-g7cb3632
docker-init:
Version: 0.19.0
GitCommit: de40ad0

If you need to run Docker in non-root mode, you can add a specific user to the docker group. For example, let's add the www-data user:

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

Docker Compose V2

We have installed the docker-compose-plugin, which adds the docker compose command to Docker:

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

Modifying Docker Configuration

The following configuration appends a custom intranet IPv6 address block, enables IPv6 for containers, and limits log file sizes to prevent Docker logs from filling up your 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

Common Docker Commands

Let's quickly understand common Docker commands. Managing them using docker compose is generally more convenient.

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
30
31
32
33
# Pull image
docker pull <image_name>:<tag>

# View currently running containers
docker ps

# View all containers (including stopped ones)
docker ps -a

# View local images
docker images

# Remove image
docker rmi <image_id_or_name>
docker rmi ubuntu:latest

# Run container
docker run -d -p <host_port>:<container_port> --name <container_name> <image_name>

# Start, stop, restart, remove
docker start / stop / restart / rm <container_name_or_id>

# Enter container and run interactive shell
docker exec -it <container_name_or_id> /bin/bash

# View container logs
docker logs <container_name_or_id>

# Check Docker disk space usage
docker system df

# Remove all unused images
docker image prune -a

Using Docker Compose

docker compose commands must be executed in the directory where the docker-compose.yml file is located. It is recommended to create a dedicated working directory to organize your configuration files, for example:

1
2
3
4
5
6
7
docker/
├── nginx/
│ └── docker-compose.yml
├── mysql/
│ └── docker-compose.yml
└── redis/
└── docker-compose.yml

Common docker compose commands:

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
30
31
32
# Pull/update images
docker compose pull

# Start containers
docker compose up

# Start containers in background
docker compose up -d

# Stop containers
docker compose stop

# Stop and remove containers, networks, volumes, and images
docker compose down

# Restart containers
docker compose restart

# View running services
docker compose ps

# View logs
docker compose logs
# View logs in real-time
docker compose logs -f

# Enter container and run interactive command
docker compose exec <service_name> <command>
docker compose exec nginx /bin/bash

# View service resource stats (real-time CPU, memory usage, etc.)
docker compose stats

Still feel docker compose is too long? Run the following commands to use dc as an alias:

Before running, run dc first to ensure the command is not currently in use by another program, and run dc version after to verify.

1
2
echo 'alias dc="docker compose"' >> ~/.bashrc 
source ~/.bashrc

Output:

1
2
3
4
5
root@debian:~# dc
-bash: dc: command not found

root@debian:~# dc version
Docker Compose version v2.31.0

Afterwards, once you have created a docker-compose.yml file, starting or updating the service is as simple as running:

1
2
dc pull 
dc up -d

Now go find interesting projects on GitHub or Docker Hub!

Reference: Debian 12 / Ubuntu 24.04 Docker and Docker Compose Installation Tutorial

Comments
Share

Comments