How to Install and Use Docker Compose on CentOS 7

Updated on

4 min read

Install and Use Docker Compose on CentOS 7

Docker Compose is a tool that allows you to define and run multi-container Docker applications.

With Compose, you define the application’s services, networks and volumes in a single YAML file, then spin your application with a single command.

Compose can be used for different purposes such as single host application deployments, automated testing, and local development.

This tutorial walks you through installing the latest version of Docker Compose on CentOS 7. We will also cover the basic Docker Compose concepts and commands.

Prerequisites

Ensure that you met the following prerequisites before continuing with this tutorial:

Install Docker Compose on CentOS

The recommended method for installing Docker Compose on CentOS 7 is by downloading the Compose binary 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.

Complete the following steps to install Docker Compose on CentOS 7:

  1. Start by downloading the Docker Compose binary into the /usr/local/bin directory using 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, make the binary executable by typing:

    sudo chmod +x /usr/local/bin/docker-compose
  3. To verify the installation type the following command to print 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’ll show how to use Docker Compose to run a WordPress stack on your CentOS 7 machine.

Start by creating a new directory for the project and navigating into it:

mkdir my_app && cd my_app

Next, 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.

The first line specifies 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 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, spin up the WordPress application using the following command:

docker-compose up

The output should look something like this:

...
wordpress_1  | [Sat Oct 13 21:30:48.286382 2018] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.25 (Debian) PHP/7.2.10 configured -- resuming normal operations
wordpress_1  | [Sat Oct 13 21:30:48.286425 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.

Open your browser, type http://0.0.0.0:8080/ in 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. environment variables If you want to start the Compose in a detached mode use 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

To completely remove the containers 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 want to uninstall Docker Compose, simply delete the binary using the following command:

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

Conclusion

You have learned how to install and use Docker Compose on a CentOS 7.

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