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 | root@debian:~# lsb_release -a |
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 | apt update |
Then, add Docker's GPG public key and the apt repository:
1 | curl -sSL https://download.docker.com/linux/debian/gpg | gpg --dearmor > /usr/share/keyrings/docker-ce.gpg |
1 | curl -sSL https://download.docker.com/linux/debian/gpg | gpg --dearmor > /usr/share/keyrings/docker-ce.gpg |
For machines in Mainland China, you can use the domestic mirror from Tsinghua TUNA:
1 | curl -sS https://download.docker.com/linux/debian/gpg | gpg --dearmor > /usr/share/keyrings/docker-ce.gpg |
1 | curl -sS https://download.docker.com/linux/debian/gpg | gpg --dearmor > /usr/share/keyrings/docker-ce.gpg |
After that, update the system package lists and install Docker CE and the Docker Compose plugin:
1 | apt update |
Now you can use the docker version command to check if the installation was successful:
1 | root@debian:~# docker version |
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 | apt install docker-ce-rootless-extras |
Docker Compose V2
We have installed the docker-compose-plugin, which adds the docker compose command to Docker:
1 | root@debian:~# docker compose version |
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 | cat > /etc/docker/daemon.json << EOF |
Then restart the Docker service:
1 | systemctl restart docker |
Common Docker Commands
- You can check the Docker Cheat Sheet
Let's quickly understand common Docker commands. Managing them using docker compose is generally more convenient.
1 | # Pull image |
Using Docker Compose
- You can check the Docker Compose Cheat Sheet
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 | docker/ |
Common docker compose commands:
1 | # Pull/update images |
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 | echo 'alias dc="docker compose"' >> ~/.bashrc |
1 | echo 'alias dc="docker compose"' >> ~/.zshrc |
Output:
1 | root@debian:~# dc |
Afterwards, once you have created a docker-compose.yml file, starting or updating the service is as simple as running:
1 | dc pull |
Now go find interesting projects on GitHub or Docker Hub!
Reference: Debian 12 / Ubuntu 24.04 Docker and Docker Compose Installation Tutorial
Comments