Docker Compose is a tool for managing and running multiple container docker images allowing you to run multiple containers as a service. Compose uses a YAML file that is used to configure the application services with a single command to start all services from your configuration. Docker Compose was introduced to allow building and maintaining services in smaller pieces that work together. These small pieces are known as microservices.

Features of Docker Compose

  • Multiple isolated environments on a single host by using project names to isolate the environments. The project name is the base name of the project directory, which is the base name of the compose file. Dev host can use this project name context to create multiple copies of a single environment.
  • Docker Compose preserves volume data when containers are created, when you run the docker compose up command, any volumes from containers created from previous runs are copied to the new container ensuring data is not lost.
  • When you restart a service that has not changed, docker-compose reuses the existing container which makes changes to your environment very quickly.
  • Docker Compose supports variables that are used in the compose file. These variables can be substituted to customize composition for different environments.

This guide shows you how to install and use Docker Compose on CentOS 9|AlmaLinux 9|RHEL 9

Install Docker Compose on CentOS 9|AlmaLinux 9|RHEL 9

Docker-compose requires Docker CE. If you do not have it installed, use the steps on Install Docker CE to install it.

If you have it installed, check for the version to verify the installation.

$ docker version
Client: Docker Engine - Community
 Version:           20.10.17
 API version:       1.41
 Go version:        go1.17.11
 Git commit:        100c701
 Built:             Mon Jun  6 23:03:29 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
  Version:          20.10.17
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.17.11
  Git commit:       a89b842
  Built:            Mon Jun  6 23:01:12 2022
  OS/Arch:          linux/amd64
  Experimental:     false
  Version:          1.6.6
  GitCommit:        10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
  Version:          1.1.2
  GitCommit:        v1.1.2-0-ga916309
  Version:          0.19.0
  GitCommit:        de40ad0

To confirm successful docker-compose installation, run the following command.

$ docker compose version
Docker Compose version v2.6.0

Docker Compose YAML file

The Docker Compose file is written in YAML format, which defines the version, services, networks, volumes, configs, and secrets.

  • The Version allows you to use the latest version of Compose file format to have access to the latest features.
  • The Services are the computing components of the application. Under this option, you are able to define various building blocks of the application like the build options and port mapping. You can add all the services that are required for your application here. This allows Docker Compose to automatically start all the services at once with one command.
  • Networks allow services to communicate with each other. Networks establish an IP route between containers within services connected to each other.
  • Volumes are where the services store and share their data.
  • Configs allow services to configure the data that is dependent on runtime or the platform that the service is running on.
  • Secrets are part of configuration data that is used for sensitive data that should not be exposed without security considerations.

A docker-compose.yml file looks like the one below.

version: "3.9"  # optional since v1.27.0
    build: .
      - "8000:5000"
      - .:/code
      - logvolume01:/var/log
      - redis
    image: redis
  logvolume01: {}

The above file has two containers defined, the Web services for the frontend with build option and ports mapped and the Redis image which represents the Redis Database. The period symbol alongside the build option represents the current folder. Meaning the application will be built from the packages in the current folder you run the docker compose up command.

Compose and WordPress on CentOS 9/AlmaLinux 9/RHEL 9

Let us create a sample WordPress Application in an isolated environment built with docker containers.

Create a project directory and change it to that new directory.

mkdir ~/dctest
cd ~/dctest

Create a docker-compose.yml file

vi docker-compose.yml

Paste the following code that includes a WordPress blog with MySQL instance with volumes for data persistence.

version: "3.9"
    image: mysql:8.0
      - db_data:/var/lib/mysql
    restart: always
      MYSQL_ROOT_PASSWORD: StrongRootPassword
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: StrongDBUserPassword
      - db
    image: wordpress:latest
      - wordpress_data:/var/www/html
      - "8000:80"
    restart: always
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: StrongDBUserPassword
      WORDPRESS_DB_NAME: wordpress
  db_data: {}
  wordpress_data: {}

Run the docker compose up command to build the application in detached mode to pull the needed Docker images and starts the WordPress and database containers.

$ docker compose up -d
## Sample Output ##
[+] Running 5/5
 ⠿ Network dctest_default          Created                                 0.3s
 ⠿ Volume "dctest_db_data"         Created                                 0.0s
 ⠿ Volume "dctest_wordpress_data"  Cre...                                  0.0s
 ⠿ Container dctest-db-1           Started                                 0.8s
 ⠿ Container dctest-wordpress-1    Start...                                1.1s

Visit your browser on http://localhost:8000 and get to see the WordPress site.

You can then finish up on the setup and Install WordPress.

To stop the container, use the following command. This removes the containers and default networks but preserves the database.

docker compose down

To remove the database, use the following command

docker-compose down --volumes


From this guide, we have gone through an overview of Docker Compose and how to install Docker Compose on CentOS 9|AlmaLinux 9|RHEL 9. We have also seen how the docker-compose.yml file can be used to build and run multiple containers at a go. Docker Compose cache option allows saving the previous container runs data and ensures the containers start up quickly on the next run.


Please enter your comment!
Please enter your name here