Docker Tutorial 3: docker compose for multi-container

Ecrit par << Paquet Judicaël >>

If you have missed the two previous articles, I suggest you go read them before you start with this tutorial docker 3 where we will learn how to manage several containers together with the help of the docker compose.

Article 1: Getting Started with Docker
Article 2: Master the Dockerfiles

This tutorial is independent of the two previous articles but it is very useful to have some knowledge of Docker to understand the power of docker composes.

Version françaiseTutoriel Docker 3 : docker compose pour le multi-conteneur

What is docker composes?

Docker compose is a very interesting docker package management tool. This tool will launch your containers and their possible links from a configuration file written in yaml. We could compare this tool to apt-get or composer but for the purpose of packaging docker containers. This tool makes life easier for docker users.

Docker compose installation

“Under Ubuntu configured basic, you must put sudo in front of your orders or review the configuration of your users”

We will have to install this new tool that is not part of the basic tools when we install Docker

sudo curl -o /usr/local/bin/docker-compose -L "https://github.com/docker/compose/releases/download/1.8.1/docker-compose-$(uname -s)-$(uname -m)"
sudo chmod +x /usr/local/bin/docker-compose

We will be able to verify that the installation worked well by testing a very simple docker-compose function that allows to display the version number of the installed tool:

docker-compose -v

Preparation of our images

To use our docker compose we will use two docker images: one that we will create with one Dockerfile and the other that we will recover on Docker Hub.

Image created from a Dockerfile

We will create our first image which will be an apache server with php that will have the ability to connect to an external MySql server.

Let’s start by creating a docker/ folder and put an index.php file like this:

 

<?php
 
 $dbh = new PDO('mysql:host=db;dbname=mysql', 'root', 'passe');
 
 foreach($dbh->query('SHOW DATABASES') as $row) {
   echo $row[0]. '<br/>';
 }

We will also do a docker/host.conf file:

<VirtualHost *:80>
     ServerAdmin webmaster@localhost
     DocumentRoot /var/www/test
 </VirtualHost>

We will now write our Dockerfile which will use these two files during the build of the image:

FROM ubuntu:16.04
 MAINTAINER Judicael paquet
 
RUN apt-get update && \
     apt-get install -y apache2 php libapache2-mod-php mysql-client php7.0-mysql
 
 RUN sed -i 's/;extension=php_mysqli.dll/extension=php_mysqli.dll/' /etc/php/7.0/apache2/php.ini
 RUN sed -i 's/;extension=php_pdo_mysql.dll/extension=php_pdo_mysql.dll/' /etc/php/7.0/apache2/php.ini
 
 ADD docker/host.conf /etc/apache2/sites-enabled/000-default.conf
 ADD docker/index.php /var/www/test/index.php
 
 ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

Everything is ready to build our image:

docker build -t monapache .

Image retrieved on Docker Hub

For our second image we will use another method: docker hub. We will retrieve an image of the Docker Hub locally with the following command:

docker pull mysql/mysql-server:5.7

Creating the docker-compose.yml file

Docker compose as I said above proposes to manage several containers at the same time with the possibility of making them communicate with one another through a single file: the docker-compose.yml.

I propose you to create ours:

 

db:
     image: mysql/mysql-server:5.7
     ports:
       - "3306:3306"
     environment:
       - "MYSQL_ROOT_PASSWORD=passe"
       - "MYSQL_USER=your_user"
       - "MYSQL_PASSWORD=your_user_password"
       - "MYSQL_DATABASE=your_database_name"
superapachephp:
     build: ./
     ports:
       - "8010:80"
     links:
       - "db:db"
     working_dir: "/home/docker"

As you can see, we have voluntarily put “password” in “root” password. This is the one we already put in our PHP file. Be aware that the name “db” is important because it will automatically be the name of the container’s host; that’s why we put “db” in hostname on the SQL connection made in PHP.

Except the changement of syntax, if you are familiar with docker run and dockerfiles, you should not be confused with this file. The link binds the container to another as with the -v option of the docker run that we did not see.

Now we are going to launch our docker-compose like this:

 

docker-compose up -d

Now try to call your url http://localhost: 8010 in your browser. If you have a blank page, you will need to do the following manipulation. It will allow you to put the right rights to the user Mysql:

docker ps 
# remplacez ubuntu_db_1 par l'id de votre conteneur db
docker exec -it ubuntu_db_1 mysql -uroot -p -e "CREATE USER
 'root'@'%' IDENTIFIED BY 'passe';GRANT ALL PRIVILEGES ON 
*.* TO 'root'@'%';FLUSH PRIVILEGES;"

As a reminder, the password will be “pass”.

If everything is ok, you will have the list of the bases present in the Mysql database on your Internet browser.

 

Conclusion docker compose

You now have all the basics to be able to quickly master docker compose. It is really a useful tool. More and more projects on Github propose besides this file docker-compose.yml to quickly mount the project.

One Reply to “Docker Tutorial 3: docker compose for multi-container”

Laissez une réponse

Votre adresse mail doit être renseigné Required fields are marked *