Docker for Drupal developers. How to create LAMP

If you don’t know what Docker is, we suggest looking through this awesome online tutorial "Try It!". It takes about 10 minutes but you will get a minimal necessary knowledge for further work.

In this article, we don’t want to repeat the official documentation or countless tutorials for beginners. We will give you all the details about how to apply Docker in your workflow. And creating LAMP is perfect for the first step.

Let’s agree that we will not reinvent the wheel and will use ready-made solutions which follow the philosophy of Docker. Many resources describe how to create images from scratch. But they forget that there are official repositories for these applications on Docker Hub. We will not explain what the benefit of official images is in comparison with custom images. I think you know it very well, so let's build our LAMP.

There are no official repositories for LAMP. You can find only custom solutions. But they conflict with the philosophy of Docker "one process in a container".

We are going to use php (https://registry.hub.docker.com/_/php/) and mysql (https://registry.hub.docker.com/_/mysql/) images. Because we use php as a module of Apache we don’t need to use a separate image for web server. We use versions recommended for Drupal 7.

1. Creating a container for database

docker run --name database -e MYSQL_ROOT_PASSWORD=root -d \
	-v "$PWD/db":/var/lib/mysql mysql

run - launch processes in an isolated container. If Docker doesn’t find the necessary image, it will be downloaded from Docker Hub.

--name database - name of the container.

-e MYSQL_ROOT_PASSWORD=root - set the value of environment variables. In this case we would like to use “root” like root’s password.

-d - run as a background process.

-v "$PWD/db":/var/lib/mysql - mounts a folder from the host with container. We use $PWD like a pointer to the current folder. So, all databases created in the container will be stored in the “src” folder. It will save your data if your container is deleted or damaged.

mysql - name of the image.

2. Creating web server

Because the official repository of php doesn’t contain some of the necessary modules, we create Dockerfile to customize this image.

# use a module of apache for php 5.5
FROM php:5.5-apache

# enable a module of apache for “clean urls”
RUN a2enmod rewrite

# install modules of apache that will be required for Drupal 7
RUN apt-get update && apt-get install -y libpng12-dev libjpeg-dev && \
	rm -rf /var/lib/apt/lists/* && docker-php-ext-configure gd \
	--with-png-dir=/usr --with-jpeg-dir=/usr && docker-php-ext-install gd
RUN docker-php-ext-install mysqli
RUN docker-php-ext-install pdo_mysql

3. Link the containers

You need to create a new subfolder “db” in the folder where you store the Dockerfile. This subfolder will contain the files of your projects.

docker run --name=server --link database:db -d \
	-v "$PWD/src":/var/www/html -p 80:80 webserver

--link database:db link our container with the container for MySQL. In this case in the file /etc/host will add a new row with ip address of the MySQL container. In the future, we can use hostname “db” instead of ip address.

-p 80:80 - indicates the port forwarding from the host machine to our container.

That is all, now we can use this LAMP. Add Drupal files to the “src” subfolder. You should use hostname “db” instead of “localhost” in the settings.php file. Docker stop and start commands will help you to work with the containers.

You might also like