How to Install and Use Docker Compose on Debian 11

Choose a different version or distribution

Introduction

Before we begin talking about how to install and use Docker Compose on Debian 11 Linux, let's briefly understand – What is Docker Compose ?

Docker is a containerization technology that enables you to easily develop, test, and deploy programs as portable, self-sufficient containers that can run virtually anywhere.

You may define and orchestrate multi-container Docker applications using the tool known as Docker Compose. The containers, networks, and volumes of the application are configured using a YAML file.

Compose can be used for a number of purposes. The most common use cases for Docker Compose are local development, automated testing, and single-host application deployments.

In this tutorial, you will install the most recent version of Docker Compose on Debian 11, Buster. We will also look at the core concepts and commands of Docker Compose.

Advantages of Installing and Using Docker Compose on Debian 11

  1. Application Orchestration: Docker Compose simplifies the orchestration of multi-container applications. It allows you to define complex architectures and dependencies, making it easier to manage and deploy your application.
  2. Configuration as Code: With Docker Compose, you define your application configuration using YAML files. This enables you to version control your application's infrastructure and easily reproduce the environment on different platforms.
  3. Improved Collaboration: Docker Compose facilitates collaboration among team members. By sharing the Docker Compose YAML file, everyone can work on the same application setup, ensuring consistent deployments across development, testing, and production environments.
  4. Easy Scaling: Docker Compose makes scaling your application a breeze. By specifying the number of instances for each service in the YAML file, you can easily scale up or down to meet demand without manual intervention or disruption.
  5. Portability and Consistency: Docker Compose ensures portability and consistency across different environments. By encapsulating all dependencies and configurations within the YAML file, you can deploy your application on any Docker-enabled system, ensuring consistent behavior.

Prerequisites

Before you begin, ensure that you have met the following requirements:

Installing Docker Compose on Debian 11

It's possible that the official Debian 11 repositories won't always have the most recent version of the Docker Compose installation package. The recommended method is to install Docker Compose from the Docker GitHub repository.

The latest stable version of Docker Compose is version 2.15.0 as of the time this tutorial was written. Visit the Compose repository release page on GitHub to see if a new version is available before downloading the Compose binary.

To install Docker Compose's latest version on Debian 11, follow these steps:

1) Using wget or curl, download the Docker Compose binary to the /usr/local/bin directory:

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

2) To make the Compose binary executable, use chmod:

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

3) Use the command that prints the Compose version to confirm the installation:

docker-compose --version

You will get an output like below:

Output

docker-compose version 1.23.1, build b02f1306

Getting Started with Docker Compose

We will demonstrate how to set up a local WordPress development environment using Docker Compose in this part.

Make a directory for the project, then navigate there:

mkdir wordpress_app && cd wordpress_app

Open a text editor, and make a file called docker-compose.yml:

nano docker-compose.yml

Copy and paste the following text:

version: '3.7'

services:
  db:
    image: mysql:8.0
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    volumes:
      - db_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: wordpress

  wordpress:
    image: wordpress
    restart: always
    volumes:
      - ./wp_data:/var/www/html
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: password
    depends_on:
       - db

volumes:
    db_data:
    wp_data:

Let us go through the code line by line.

The version of the Compose file is specified in the first line. The Compose file format is available in several variations, each of which supports a particular Docker release.

Next, we are defining the wordpress and db services. When Docker Compose is executed, each service creates an individual container.

The db service:

  • The image has been set to mysql:8.0 image. If the image is missing, Compose will retrieve it from the public Docker Hub repository. The line starting with command takes precedence over the default command.
  • If the container stops responding, Compose is instructed to restart it by the restart: always policy.
  • To persist the database, the container will use the named volume db_data.
  • Defines the mysql:8.0 image's environment variables.

The wordpress service:

  • The wordpress image is used.
  • Mounts the host's wp_data directory to /var/lib/mysql within the container.
  • Forwards the container's exposed port 80 to the host machine's port 8080.
  • Defines the wordpress image's environment variables.
  • The dependency between the two services is defined by the depends_on command. In this case, the db will launch before wordpress.

Run the following command to launch the WordPress stack from the project directory:

docker-compose up

The output should resemble this:

...
] /usr/sbin/mysqld: ready for connections. Version: '8.0.18'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
db_1_99946702ac7e | 2019-12-15T21:37:29.109255Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
...

The wp_data directory will be created in your project directory by Docker Compose, which will also start the containers and pull the images.

In your browser, type http://0.0.0.0:8080/ to access the standard WordPress installation screen.

The WordPress application is now operational, and you may begin working on it.

Press CTRL+C to stop Compose.

Using the -d option, you can also start the Compose in detached mode:

docker-compose up -d

Use the following command to view the running Docker containers:

docker-compose ps
Output

                 Name                               Command               State          Ports        
------------------------------------------------------------------------------------------------------
wordpress_app_db_1_99946702ac7e          docker-entrypoint.sh --def ...   Up      3306/tcp, 33060/tcp 
wordpress_app_wordpress_1_a428d8408817   docker-entrypoint.sh apach ...   Up      0.0.0.0:8080->80/tcp 

When Compose is operating in detached mode, use the following to stop the services:

docker-compose stop

Use the down option to completely remove the containers:

docker-compose down

Passing the --volumes switch removes the data volumes as well:

docker-compose down --volumes

Uninstalling Docker Compose

If you want to uninstall Docker Compose, just remove the binary by typing:

sudo rm /usr/local/bin/docker-compose

FAQs on Installing and Using Docker Compose on Debian 11

What is Docker Compose? Docker Compose is a tool that allows you to define and manage multi-container Docker applications. It uses a YAML file to define the services, networks, and volumes required for an application.

Docker Compose is a tool that allows you to define and manage multi-container Docker applications. It uses a YAML file to define the services, networks, and volumes required for an application.

Is Docker Compose compatible with Debian 11? 

Yes, Docker Compose is fully compatible with Debian 11.

What are the system requirements for installing Docker Compose on Debian 11?

To install and use Docker Compose on Debian 11, you need a Debian 11 system with Docker already installed.

How can I install Docker Compose on Debian 11? 

You can install Docker Compose on Debian 11 by following the steps outlined in the official Docker documentation. It typically involves downloading the Docker Compose binary and setting it as an executable.

What is the purpose of a Docker Compose YAML file? 

A Docker Compose YAML file is used to define the services, networks, and volumes required for a multi-container Docker application. It allows you to specify configurations, dependencies, and other settings in a declarative format.

How can I start a Docker Compose application on Debian 11?

To start a Docker Compose application on Debian 11, navigate to the directory containing the Docker Compose YAML file and run the docker-compose up command.

Can I update Docker Compose on Debian 11? 

Yes, you can update Docker Compose on Debian 11 by downloading the latest version from the official Docker Compose GitHub repository and replacing the existing binary.

Conclusion

We hope this detailed tutorial helped you understand how to install and use Docker Compose on Debian 11 Linux.

Download the binary, place it in a directory on the system path, and make it executable to install Docker Compose on a Debian 11 system.

If you have any queries, feel free to post a comment below, and we'll be happy to answer them.