Using Docker in a development environment

26 June 2019
actu2606
When I go back a few years and ask myself. Which development environments was I using? What were the problems? How did I cope when a client told me its application ran on PHP 4 when I had PHP 5.6 on my PC? I realize what difficulties we had. It was impossible to work with different configurations on the same workstation without using virtual machines.

It’s already 6 years since I first heard of Docker. At the time, we were just getting to know Vagrant, then the flagship software for creating and configuring virtual development environments. A quick search directed me to the first talk given by Solomon Hykes, co-founder of dotCloud, in which he presented Docker and gave a live demonstration. I immediately realized how useful Docker would be to the development teams. Docker has since undergone a lot of development; and the third version of the Docker platform was announced at the DockerCon 2019 conference.

 

So what is Docker?

Docker is an open-source software tool that lets you package one or more applications together with all their dependencies in containers, thus making them easier to use. A container, unlike a virtual machine, does not have an operating system.

 

Docker vs virtual machine

Unlike virtual machines, Docker lets you containerize applications so they are isolated but share resources on the host machine. Virtual machines include an operating system as well as applications.

2606photo

Using Docker at AUSY

The challenge of providing a standard development environment necessarily includes considering the project requirements and the available hardware. A good development environment is one that developers and integrators who do not necessarily have an in-depth knowledge of infrastructure can access and learn to use quickly.

With this in mind, AUSY held brainstorming sessions several years ago to analyze both our internal requirements and our customers’ expectations. The aim was to produce an environment that helped a developer get going quickly.

To make an environment available under all possible operating systems, we decided to build a Virtual Machine Linux desktop. All the development tools and most importantly a Docker environment are pre-installed on this machine.

We then used a multiservice installation for each Drupal 8 project. It has the following components:

  • Apache/PHP service, based on a CentOS-7 image, plus the following packages:
    • Apache
    • PHP7 and the modules required by Drupal 8
    • Drush
    • Composer
    • Compass
    • mhsendmail
  • db service: based on the “MySQL:5.6” image with a customized configuration file
  • phpMyAdmin service: based on the standard “phpmyadmin/phpmyadmin” image
  • Mailhog service: based on a “mailhog/mailhog:v1.0.0” image. This tool is ready to use and seems useful for quickly testing the functionality for sending emails.
  • Behat service: based on an image modelled on a “debian:stretch” image, to which we have added specific functional-testing tools.

 

Together, these components make up a robust, ready-to-use execution environment that can be extended with Dockerfile configuration description files and docker-compose.yml.

The post-implementation feedback has been very positive, with significant gains in efficiency and productivity.

 

A few practical details

Installation

Before you can use Docker, you must install it. To do so, start the terminal and run the following commands specific to the Linux Centos 7 distribution. For the other distributions, please consult the official documentation https://docs.docker.com/install/linux/docker-ce/centos/#install-docker-ce

sudo yum check-update

sudo curl -fsSL https://get.docker.com/ | sh

Once installation is complete, launch the Docker service:

sudo systemctl start docker

To check that Docker is working correctly, run the following command:

sudo systemctl status docker

 

visuel

If you see “active (running)” in the command output, (see screen shot above), this shows that the Docker service is active and running.

Hint

To make sure that the Docker service is launched automatically each time the system starts, you need to activate it.
sudo systemctl enable docker

Use

Docker-compose

Most modern applications comprise several services. This is known as a microservices architecture. When we use Docker, Docker Compose is the tool for building and running multi-container applications.

Let’s take the example of a Drupal application. We consider the following set of multiservices:

  • Apache
  • PHP and the PHP modules required by Drupal
  • MySQL
  • phpMyAdmin for visual access to the database
  • Possibly an interface to intercept emails sent from the application.

If we want all this software in a VM, it is highly likely that we will have to devote considerable time to configuring an operational environment. The maintenance and upgrade of the packages without regressions (all the components in each container) will then need more time. By contrast, with Docker we simply create a docker-compose.yml file to define the different versions in our environment.

 

Content of the docker-compose.yml file

visuel_2

Comment: You can define variables (for instance ${PROJECT_NAME}) to make the docker-compose.yml file as generic as possible. The values of these variables are populated for each project from the .env file located at the same level. You can thus manage an activity in a consistent way across all projects and environments.

 

Content of the .env file

visuel_3

The following services have been defined in the docker-compose.yml file:

Web service

visuel4

The web service (a container holding Apache, PHP and the PHP modules required) has been built from the “php:7.2-apache” image which downloads automatically from the Docker repository. Once it has been recovered, it can be supplemented with packages that are missing or necessary to the project.

Note that the only command run during the installation of this architecture is:

docker-compose up

The line ‘build: docker/apache’ found in the service definition means that an image is built from a Dockerfile that is present in the “docker/apache” folder.

The following lines appear at the start of this file:

visuel5

This means that docker-compose will search for the image “php:7.2-apache” then install Composer.

Thus changing version is made much easier, as we simply replace the line:

FROM php:7.2-apache

With

FROM php:7.3-apache

And run the command: docker-compose build && docker-compose up

DB service

visuel6

This set of instructions searches for the MySQL version 5.7 image from the docker repository and loads a customized configuration stored in the “docker/db/custom.cnf” file. This mechanism for sharing files between the host machine and the container relies on volumes. It allows data from the database to be persistent.

 

phpMyAdmin service

visuel7

This service is based on the phpMyAdmin image, available directly from the Docker hub.

We declare that this service will depend on the db container (MySQL)

depends_on:

  • db

 

We define the listening port for the service in the web service using the following command:

ports:

  • 8083:80

So if we want to access phpMyAdmin, we simply go to the address http://localhost:8083

Lastly, we define the environment variables for the phpMyAdmin container:

PMA_HOST: name of the database container (db).

PMA_USER: database user defined in the db container.

PMA_PASSWORD: associated password.

Once all these services have been loaded using the command docker-compose up, we can access our Drupal 8 application by going to the URL: http://localhost:8082

Docker is therefore an unbeatable tool for installing development environments quickly. Comparison with standard virtualization shows that Docker is way ahead as a tool for installing robust and optimized environments.

Using Docker Compose, you can easily create and run containers with a simple file definition. The wealth of images available in the Docker repository and the extensibility of the Docker environment make it difficult currently to replace it, if indeed there were serious contenders.

 

And you may be interested in our Drupal product.

Let's have a chat about your projects.

bouton-contact-en