Welcome to weblogs.com.pk Sign in | Join | Help

Dockerizing PHP + MySQL Application

Docker allow us to package the application with all its dependencies; and this makes an ideal platform to deploy and migrate existing PHP / MySQL applications. We cannot only consolidate multiple PHP applications on the server; where one application is using the latest runtimes and other might need specific versions of PHP and MySQL, but also can test the application in an isolated environment with newer runtimes or try updating the application framework to newer versions. For this post; I will be migrating WordPress application to Docker.

I am running WordPress using IIS Express and MySQL on my Windows development machine; but things apply equally to Linux. For migration; we need the WordPress’ PHP files and SQL script to generate its associated MySQL database. Using the information from wp-config.php file and mysqldump we can create the SQL script; and using Windows 10 Anniversary Update’s Bash on Ubuntu on Windows (quite mouthful) we can create the TGZ (tar + gzip) file from the WordPress www root easily. We are creating the TGZ file because Docker supports it natively; when creating the web container it will automatically expand it to the specified folder and its easier to manage single file instead of hundreds of web application files

  • Note; that we created the TGZ file from the www root folder; its required so that there is no parent directory in the archive; I also placed it in the separate project folder where all the required files for Docker will be kept

Lets setup the required containers; I am going to setup two containers; one for MySQL / Database and the other for the PHP / Web. I will be using the official images so that I can rebuild my images whenever they are updated (security fix; newer versions etc) Lets start a standard mysql:5.6 container; we will need to name it so we can link it later with the web container. I am also exposing its MySQL port so I can connect to it from MySQL CLI to create the database and import the data using the dump SQL we created earlier.

  • I am using Visual Studio Code; and have set the End of Line from \r\n to \n in its Workspace Setting to make the files I am creating / editing Linux friendly; later we will be creating scripts
  • I am using Docker for Windows
  • Stop the Local MySQL Windows Service; if you have it; before mapping the container’s MySQL port
  • When running the MySQL container in production; we don't have to expose its port; we can link the web container to it and it will be able to access MySQL just fine

Lets create a Dockerfile for the web container; we will be basing our container on a standard php:5.6-apache and add the required Linux component and PHP extensions using the script / mechanism php:5.6 container recommends. We can add TGZ file and Docker Build will extract it into the specified folder. I have also kept a copy of wp-config.php; make changes in it for new MySQL settings; and created a test.php file to check the connection and copying these two files over the TGZ

  • I simply referred to WordPress official Dockerfile to learn which Linux components and PHP extensions it need

Dockerfile for Web Container

FROM php:5.6-apache
RUN apt-get update

COPY php.ini /usr/local/etc/php/

RUN set -ex; apt-get install -y libjpeg-dev libpng12-dev

RUN docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr
RUN docker-php-ext-install gd mysqli opcache

RUN { echo 'opcache.memory_consumption=128'; \
    echo 'opcache.interned_strings_buffer=8'; \
    echo 'opcache.max_accelerated_files=4000'; \
    echo 'opcache.revalidate_freq=2'; \
    echo 'opcache.fast_shutdown=1'; \
    echo 'opcache.enable_cli=1'; \
    } > /usr/local/etc/php/conf.d/opcache-recommended.ini

RUN a2enmod rewrite expires

ADD wordpress.tgz /var/www/html
COPY wp-config.php /var/www/html/wp-config.php
COPY test.php /var/www/html/test.php

Once our web image is created; we can simply run it linking to the MySQL container in which we created the required database earlier

  • We have to use the same port we were using earlier; as WordPress stores the Site URL in its database and redirect to it automatically if any other URL is requested

Stay tuned for the second part; in which we will use docker-compose and also try to automate certain manual steps we had to perform above

Published Thursday, December 29, 2016 12:39 AM by khurram
Filed under: , ,


# re: Dockerizing PHP + MySQL Application

Thursday, December 29, 2016 11:36 AM by Asim Hafeez

Good work.

New Comments to this post are disabled