How to Install and Use Docker-Compose on CentOS 7

Channel: Linux
Abstract: [[email protected] wordpress-site]# Run ‘docker-compose start’ to start containers and its services [[email protected] wordpress-site]# docker-compose

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.

  •  How to Install Docker on CentOS 7
  • Install and Configure Docker Swarm mode on CentOS 7

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

[[email protected] ~]# yum install epel-release -y
[[email protected] ~]#  yum install python-pip -y
[[email protected] ~]# 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

[[email protected] ~]# pip install --upgrade pip

Verify the Docker version using below command

[[email protected] ~]# docker-compose --version
docker-compose version 1.15.0, build e12f3b9
[[email protected] ~]#
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

[[email protected] ~]# docker pull wordpress
[[email protected] ~]# docker pull mysql
[[email protected] ~]# 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

[[email protected] ~]# mkdir wordpress-site
[[email protected] ~]# cd wordpress-site/
[[email protected] 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

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,

[[email protected] 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

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
[[email protected] 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
[[email protected] wordpress-site]#
Stop and start the Containers & Its Services

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

[[email protected] wordpress-site]# docker-compose stop
Stopping wp_web ... done
Stopping wp_db  ... done
[[email protected] wordpress-site]#

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

[[email protected] wordpress-site]# docker-compose start
Starting dbserver  ... done
Starting webserver ... done
[[email protected] 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}’

[[email protected] wordpress-site]# docker-compose logs
[[email protected] 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

[[email protected] 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
[[email protected] wordpress-site]#

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

Ref From: linuxtechi

Related articles