How to Set Up a Docker Compose Environment on Debian 12 Bookworm

How to Set Up a Docker Compose Environment on Debian 12 Bookworm

Docker has become an essential tool for modern software development and deployment, offering a consistent and efficient way to build, ship, and run applications in containers. While Docker alone is powerful, Docker Compose extends its capabilities by allowing users to define and run multi-container applications with ease.

In this guide, we will walk through the steps to set up Docker and Docker Compose on a Debian 12 Bookworm system, ensuring a clean and functional environment to build and manage containerized services.


What is Docker Compose?

Docker Compose is a tool that allows you to define and manage multi-container Docker applications using a YAML file, typically named docker-compose.yml. With a single command, you can start up or tear down entire application stacks, making development and testing more manageable.

For instance, a typical web application might require:

  • A web server (e.g., Nginx)
  • An application server (e.g., Node.js or Python Flask)
  • A database (e.g., PostgreSQL)

Instead of starting each container manually, Docker Compose allows you to define all services in one file and launch them with docker compose up.


Prerequisites

Before we begin, ensure you have the following:

  • A running instance of Debian 12 Bookworm
  • A user with sudo privileges
  • Access to the terminal or SSH

Installing Docker on Debian 12 Bookworm

Let’s start by installing Docker Engine on your system.

Step 1: Update the System

sudo apt update && sudo apt upgrade -y

Step 2: Install Required Packages

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

Step 3: Add Docker’s Official GPG Key

sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Step 4: Set Up the Docker Repository

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

Step 5: Install Docker Engine

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

Step 6: Enable and Start Docker

sudo systemctl enable docker
sudo systemctl start docker

Step 7: Verify Docker Installation

docker --version

Expected output:

Docker version 24.x.x, build xxxxxxx

Installing Docker Compose

As of Docker 20.10, Docker Compose V2 is included as a plugin (docker compose) rather than a standalone binary (docker-compose).

To verify Docker Compose is installed:

docker compose version

Expected output:

Docker Compose version v2.x.x

If for any reason it’s not available, you can manually install the plugin.

Manual Installation (if needed)

DOCKER_COMPOSE_VERSION="v2.24.0"
sudo curl -SL https://github.com/docker/compose/releases/download/$DOCKER_COMPOSE_VERSION/docker-compose-linux-$(uname -m) \
  -o /usr/local/lib/docker/cli-plugins/docker-compose

sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose

Recheck with:

docker compose version

Creating a Docker Compose Project

Let’s create a simple example to demonstrate how Docker Compose works. We’ll run a basic web application using Nginx and Redis.

Step 1: Create Project Directory

mkdir ~/mycomposeproject && cd ~/mycomposeproject

Step 2: Create docker-compose.yml

version: '3.9'

services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html:ro

  redis:
    image: redis:alpine

Step 3: Add Static HTML Content

mkdir html
echo "<h1>Hello from Docker Compose on Debian 12</h1>" > html/index.html

Managing Containers with Docker Compose

Start the Services

docker compose up -d

View Running Containers

docker compose ps

View Logs

docker compose logs

Stop the Services

docker compose down

This will stop and remove the containers. To also remove volumes, networks, and images:

docker compose down --volumes --rmi all

Best Practices for Docker Compose Projects

  • Use .env files to store environment variables and secrets.
  • Keep services modular and avoid bloated docker-compose.yml files.
  • Use named volumes for persistent data (especially for databases).
  • Use healthchecks to monitor container readiness.
  • Define networks explicitly if multiple services require isolation or controlled communication.

Example:

networks:
  frontend:
  backend:

services:
  app:
    networks:
      - frontend
      - backend

Troubleshooting Common Issues

Docker Permission Denied

If you get:

Got permission denied while trying to connect to the Docker daemon socket

Add your user to the docker group:

sudo usermod -aG docker $USER
newgrp docker

Port Conflicts

Make sure ports defined in your Compose file (like 8080) aren’t used by other services.

YAML Formatting Errors

Docker Compose is strict about indentation and formatting. Always double-check your docker-compose.yml file using tools like:

docker compose config

Conclusion

Setting up a Docker Compose environment on Debian 12 Bookworm is straightforward and brings substantial benefits for managing complex applications. By leveraging Compose, you can organize services clearly, manage them more efficiently, and scale your environments with ease.

Whether you’re running a WordPress site, a full-stack web app, or microservices architecture, Docker Compose simplifies the process significantly. Combined with Debian’s stability and performance, it becomes a reliable setup for developers and system administrators alike.


Next Steps

  • Explore more advanced Compose options like depends_on, restart, and build.
  • Integrate your Compose projects into CI/CD pipelines.
  • Experiment with container orchestration platforms like Docker Swarm or Kubernetes if your stack grows.