How to Install and Use Docker-Compose on CentOS 7

Docker-compose is a command line tool to define and configure multi-container docker applications. In other words we can say docker compose is used to link multiple containers and deploy application from a single command.  Let’s assume we want to deploy WordPress site inside a container, so to deploy this i would be requiring one Web Server container (Apache/ nginx)  and one database container (mysql / mariadb).

With Docker Compose we can easily include multiple containers inside our docker-compose file and can also define all other configuration required for my application. Docker-compose tool can be used in development, testing and staging environment, apart from these it can be easily used in CI (Continues Integration) workflow

In this article we will discuss how to install Docker Compose on existing Docker host and then we will learn how to deploy containers from docker-compose command. In our previous article we have already discuss the following topics.

I am assuming here that we have existing docker host up and running. Now let’s jump into the installation steps of docker compose tool.

Installation steps of Docker Compose

Login to your docker host and run the beneath commands one after the another

[root@docker-host ~]# yum install epel-release -y
[root@docker-host ~]#  yum install python-pip -y
[root@docker-host ~]# pip install docker-compose

Note: pip version 6.0 or higher is recommended for docker-compose to work smoothly.  In case pip version is below 6.0 then run the below command to upgrade it

[root@docker-host ~]# pip install --upgrade pip

Verify the Docker version using below command

[root@docker-host ~]# docker-compose --version
docker-compose version 1.15.0, build e12f3b9
[root@docker-host ~]#

Deploy Containers with Docker Compose tool

To deploy docker containers with docker-compose command , first create a directory under that directory create a compose file with name ‘docker-compose.yml‘ or ‘docker-compose.yaml‘. In the compose file we will define the services for our applications and container images.

Before start creating compose file, let’s first pull WordPress and MySQL container images

[root@docker-host ~]# docker pull wordpress
[root@docker-host ~]# docker pull mysql
[root@docker-host ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              c73c7527c03a        2 days ago          412MB
wordpress           latest              f28808014819        7 days ago          406MB

Create a directory with name “wordpress-site

[root@docker-host ~]# mkdir wordpress-site
[root@docker-host ~]# cd wordpress-site/
[root@docker-host wordpress-site]#

Create docker-compose.yml file with the following content

version: '3.0'

services:
  webserver:
    image: wordpress
    container_name: wp_web
    ports:
      - 8080:80
    links:
      - dbserver:mysql
    environment:
      WORDPRESS_DB_PASSWORD: 6zcznAEjLWp79P
  dbserver:
    image: mysql:latest
    container_name: wp_db
    environment:
      MYSQL_ROOT_PASSWORD: 6zcznAEjLWp79P

wordpress-site-docker-compose

In the above compose file we have define two services with the name “webserver” and “dbserver”, for these services I have also specified container image . Apart from this I have specify environments by mentioning the mysql root password and wordpress db password.

Let’s deploy our application i.e WordPress site by running the below command,

[root@docker-host wordpress-site]# docker-compose up

Note: Run ‘docker-compose up’ command from the directory where docker-compose file exists.

Above Command will deploy two containers with name “wp_web” and “wp_db”. Try access your WordPress site using URL :

http://{docker-host-ip}:8080

WordPress-Installation-Inside-Container

Complete the screen instructions to finish WordPress Installation. This confirms that we have successfully deployed WordPress site inside the containers using docker-compose tool.

Let’s have a look on options of ‘docker-compose’ command

List the Containers which are deployed for our application

[root@docker-host wordpress-site]# docker-compose ps
 Name               Command               State          Ports
----------------------------------------------------------------------
wp_db    docker-entrypoint.sh mysqld      Up      3306/tcp
wp_web   docker-entrypoint.sh apach ...   Up      0.0.0.0:8080->80/tcp
[root@docker-host wordpress-site]#

Stop and start the Containers & Its Services

Either press Ctrl+C on the command or run below command

[root@docker-host wordpress-site]# docker-compose stop
Stopping wp_web ... done
Stopping wp_db  ... done
[root@docker-host wordpress-site]#

Run ‘docker-compose start’ to start containers and its services

[root@docker-host wordpress-site]# docker-compose start
Starting dbserver  ... done
Starting webserver ... done
[root@docker-host wordpress-site]#

View the Containers Logs

Run ‘docker-compose logs’ command to view logs of containers and to view the logs of a specific container use the command ‘docker-compose logs {service-name}’

[root@docker-host wordpress-site]# docker-compose logs
[root@docker-host wordpress-site]# docker-compose logs dbserver

Stop & Remove Containers along with its network

With help of ‘docker-compose down’ command we can stop and remove containers from a single command

[root@docker-host wordpress-site]# docker-compose down
Stopping wp_web ... done
Stopping wp_db  ... done
Removing wp_web ... done
Removing wp_db  ... done
Removing network wordpresssite_default
[root@docker-host wordpress-site]#

That’s all from this tutorial, For more options please refer “docker-compose –help

Share Now!

Leave a Comment