How to Install and Use Docker Compose on Debian 9
Updated on
•6 min read

Docker Compose is a tool that allows you to define and orchestrate multi-container Docker applications. It uses a YAML file to configure the application’s containers, networks, and volumes.
Compose can be used for various purposes. Single host application deployments, automated testing, and local development are the most popular use cases for Docker Compose.
This tutorial will walk through the process of installing the latest version of Docker Compose on Debian 9. We’ll also explore the basic Docker Compose concepts and commands.
Prerequisites
Ensure that you have met the following prerequisites before continuing with this tutorial:
- Logged in as a user with sudo privileges .
- Have Docker installed on your Debian 9 machine.
Install Docker Compose on Debian
The Docker Compose installation package is available in the official Debian 9 repositories but it may not always be the latest version. The recommended approach is to install Docker Compose from the Docker’s GitHub repository.
At the time of writing this article, the latest stable version of Docker Compose is version 1.23.1
. Before downloading the Compose binary visit the Compose repository release page on GitHub
and check if there is a new version available for download.
Perform the following steps to install the latest version of Docker Compose on Debian 9:
Start by downloading the Docker Compose binary into the
/usr/local/bin
directory using the followingcurl
command: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
When the download is complete, give executable permissions to the Compose binary:
sudo chmod +x /usr/local/bin/docker-compose
Verify the installation by typing:
docker-compose --version
The output will look something like this:
docker-compose version 1.23.1, build b02f1306
Getting started with Docker Compose
In this section we will show how to use Docker Compose to manage a WordPress stack on your Debian 9 machine.
Start by creating a directory for the project and changing into it :
mkdir wordpress_app
cd wordpress_app
Open your text editor and create a file
named docker-compose.yml
inside the project directory:
nano docker-compose.yml
Paste the following content:
version: '3.3'
services:
db:
image: mysql:5.7
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:
So what does the code above do?
In the first line, we are specifying the Compose file version . There are several different versions of the Compose file format with support for specific Docker releases.
Next, we are defining two services, db
and wordpress
. Each service runs one image and it will create a separate container when docker-compose is run.
The db
service:
- Uses the
mysql:5.7
image. If the image is not present on your system Compose will pull it from the Docker Hub public repository. - Uses the restart
always
policy which will instruct the container to always restart. - Creates a named volume
db_data
to persist the database. - Defines the environment variables for the
mysql:5.7
image.
The wordpress
service:
- Uses the
wordpress
image. If the image is not present on your system Compose will pull it from the Docker Hub public repository. - Uses the restart
always
policy which will instruct the container to always restart. - Mounts the
wp_data
directory on the host to/var/lib/mysql
inside the container. - Forwards the exposed port 80 on the container to port 8080 on the host machine.
- Defines the environment variables
for the
wordpress
image. - The
depends_on
instruction defines the dependency between the two services. In this example,db
will be started beforewordpress
.
From the project directory, start up the WordPress application by running the following command:
docker-compose up
The output should look something like this:
...
wordpress_1_70f2f980e1fb | [Mon Nov 19 18:00:31.002748 2018] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.25 (Debian) PHP/7.2.12 configured -- resuming normal operations
wordpress_1_70f2f980e1fb | [Mon Nov 19 18:00:31.002912 2018] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
Compose will pull both images, start two containers and create the wp_data
directory in your project directory.
Enter http://0.0.0.0:8080/
in your browser and you will see the Wordpress installation screen.
At this point the WordPress application is up and running and you can start working on your theme or plugin.
To stop Compose press CTRL+C
.
You can also start the Compose in a detached mode by passing the -d
flag.
docker-compose up -d
To check the running services use the ps
option:
docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------
wordpress_app_db_1 docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp
wordpress_app_wordpress_1 docker-entrypoint.sh apach ... Up 0.0.0.0:8080->80/tcp
When Compose is running in detached mode to stop the services use:
docker-compose stop
If you want to remove the containers entirely use the down
option:
docker-compose down
Passing the --volumes
switch will also remove the data volumes:
docker-compose down --volumes
Uninstalling Docker Compose
If you need to uninstall Docker Compose you can simply remove the binary by typing:
sudo rm /usr/local/bin/docker-compose
Conclusion
You have learned how to install and use Docker Compose on a Debian 9. If you have any questions, please leave a comment below.