The docker-compose is a CLI tool that automates some very long arguments we other wise will be passing to “docker run” command. This tool will also help us to start multiple containers at the same time.
In real world deployments, there can be multiple containers working together for a result, and they will have to communicate to each other. To make this happen certain network settings have to be made, and can be a difficult task to set this up. This is where the docker composer comes.
The command docker-compose up is similar to docker run <image>. The command docker-compose up –build runs the image after re-building the images.
In this example we are going to use two servers to run a WordPress application
- A webserver having wordpress
- A MariaDB server hosting database
(Assumption : You already know how to install and get a wordpress site running)
Create a folder in your server hosting Docket named /sandbox/wordpress.
Download the latest wordpress application and store it in a folder named /sandbox/wordpress/wordpress
Now go to the following folder
[root@gw20-lap-doc1 wordpress]# pwd
/sandbox/wordpress
Create a file named Dockerfile with the following content
======= ======= =
[root@gw20-lap-doc1 wordpress]# cat Dockerfile
FROM centos
RUN yum -y install httpd
RUN yum -y install php
RUN yum -y install php-mysql
RUN yum -y install net-tools curl
RUN yum -y install openssh-clients telnet
RUN echo “Welcome to Shiju webpage” > /var/www/html/index.html
WORKDIR /var/www/html/
COPY ./wordpress ./wordpress
CMD [“/usr/sbin/httpd”, “-D”, “FOREGROUND”]
======= ======= =
Create an image named wpdb using Centos as a base image, and install mysql or mariadb server that will have root at the username and root123 as the password
Now create a file named docker-compose.yml
====== ====
[root@gw20-lap-doc1 wordpress]# cat docker-compose.yml
version: ‘3’
services:
$$$$$ wpdbserver:
$$$$$$$$$$ image: ‘wpdb’
$$$$$$$$$$ ports:
$$$$$$$$$$$$$$$ – “3306:3306”
$$$$$ wordpressserver:
$$$$$$$$$$ restart: always
$$$$$$$$$$ build: .
$$$$$$$$$$ ports:
$$$$$$$$$$$$$$$ – “80:80”
====== ====
The restart:always parameter ensures the container restarts if it crashes
Run the following command to bring up the containers as per the Dockerfile, and enable them to communicate with each other
[root@gw20-lap-doc1 wordpress]# docker-compose up
Pay attention to the two lines in the output
Starting wordpress_wpdbserver_1 … done
Starting wordpress_wordpressserver_1 … done
Open another window and run the following command to see the status of the containers:
[root@gw20-lap-doc1 wordpress]# docker ps
(Verify if you can see the wordpress site via a browser. Now you have to update the wp-config.php file in the wordpress folder, and also create databases and db username to get the site completed.)
[root@gw20-lap-doc1 wordpress]# docker-compose ps
Run the above command from the folder where you ran the docker-compose up command.