Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install and use Docker Compose on Debian 10 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 10, Buster. We will also look at the core concepts and commands of Docker Compose.

Prerequisites

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

Installing Docker Compose on Debian 10

It's possible that the official Debian 10 repositories won't always have the most recent version of 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 10, 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

Conclusion

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

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

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

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.