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

Compose can be used for different purposes. Single host application deployments, automated testing, and local development are the most popular use cases for Docker Compose.

In this tutorial, we’ll show you how to install the latest version of Docker Compose on Ubuntu 18.04 and explore the basic Docker Compose concepts and commands.

The same instructions apply for Ubuntu 16.04 and any other Debian based distribution, including Debian, Linux Mint and Elementary OS.

Prerequisites

Make sure that you have met the following prerequisites before continuing with this tutorial:

Install Docker Compose on Ubuntu

The Docker Compose installation package is available in the official Ubuntu 18.04 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.

To install Docker Compose on Ubuntu 18.04, follow these steps:

  1. Download the Docker Compose binary into the /usr/local/bin directory with the following curl 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
  2. Once the download is complete, apply executable permissions to the Compose binary:

    sudo chmod  x /usr/local/bin/docker-compose
  3. Verify the installation by running the following command which will display the Compose version:

    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 set up a multi-container WordPress application on Ubuntu 18.04.

Start by creating a project directory and navigating into it:

mkdir my_appcd my_app

Launch your text editor and create a file named docker-compose.yml inside the project directory:

nano docker-compose.yml

Paste the following content:

docker-compose.yml

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:

Let’s analyze the code line by line.

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 the system it will be pulled 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 make the database persistent.
  • 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 before wordpress.

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  | [Sun Sep 23 22:31:43.499055 2018] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.25 (Debian) PHP/7.2.10 configured -- resuming normal operations
wordpress_1  | [Sun Sep 23 22:31:43.499796 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        
----------------------------------------------------------------------------------
my_app_db_1          docker-entrypoint.sh mysqld      Up      3306/tcp, 33060/tcp 
my_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 for any reason you want 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 Ubuntu 18.04.

Using Docker Compose can significantly improve your workflow and productivity. You can define your development environment with Docker Compose and share it with the project collaborators.

If you have any questions, please leave a comment below.