Short Tutorial on Docker, PHP and MySQL

In this simple tutorial I’m gonna teach you some basic tricks about a skinny LAMP application containerized with Docker.

One of the main focuses of Docker is to solve a common problem in developing and deploying complex applications. We know it as “The Matrix from Hell”.

When designing an application stack which is more than trivial, you have to take care of the compatibility between different version of tools and languages and their dependecies. They have to coexist in the same machine.

With Docker, you solve this problem by instantiating different light-weight machines in a single host, without the need to create real virtual machines on the cloud and spending a lot more. A Docker container consumes a lot less resources.

For the installation of Docker, please refer to this link to the official website, it’s very easily explained if you follow all the steps.

Docker comes in different Linux flavours. It is emulated on different other operating systems and comes in two different editions.

Starting the Docker daemon.

We made this tutorial on Fedora 27. For the beginning choose the Community Edition (CE) and start it like this:

$ sudo systemctl start docker

Now that you installed everything, and all is up and running, you can issue the following command to check if Docker is working correcly, you should see the classical hello world message:

$ sudo docker run hello-world

Hence it’s time to run the first PHP application, first you have to download the relative images:

$ sudo docker pull php:7.2.4-apache
$ sudo docker pull mysql:5.7.22

You just downloaded the 7.2.4 and 5.7.22 versions from the official repository. Note that Docker layerizes images. Common parts of them do not have to be downloaded again.

Moreover they don’t take hard disk space. Check if it all went well, like this:

$ sudo docker image ls

Now run the following commands to start MySQL and check if it’s running (you should see “Up” under “STATUS”):

$ sudo docker run --name mysql_test -e MYSQL_ROOT_PASSWORD=password -d mysql
$ sudo docker ps

Then log into the MySQL shell and create an empty database, once you’re in it, like so:

$ sudo docker run -it --link mysql_test:mysql --rm mysql:5.7.22 sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'
mysql> CREATE DATABASE lamp_test;
mysql> exit;

Now check the IP address of your MySQL server container, and copy it:

$ sudo docker inspect mysql_test | grep "IPAddress"

Let’s create the application.

Last step, create an application file that connects to the database:

$ mkdir lamp_test
$ cd lamp_test
$ vi index.php

Put the following content in it, within the php tags, use your favorite editor instead of vi:

$conn = mysqli_connect("", "root", "password", "lamp_test");
if($conn) echo "Connected to the database.";

Replace the IP address with the one you have copied before. Then simply run the Apache and PHP container with the host folder attached:

$ sudo docker run -d --name php_test -v "$(pwd)":/var/www/html php:7.2.4-apache

The last thing you’re gonna have to do, is to install the MySQL PHP driver, which doesn’t come with the container out of the box:

$ sudo docker exec -it php_test bash
# docker-php-ext-install mysqli
# apachectl restart
# exit

Only one other thing, check the Apache server IP, just as you did for the MySQL one:

$ sudo docker inspect php_test | grep "IPAddress"

Now all you have to do, is to point your browser at that location. And we’re done, for now, I hope you gained something from this tutorial.

Please note that if you are in different operating systems or you choose different containers versions, you might encounter some issues. Contact me to solve them.

There are other ways to do the same thing we did here: in particular, in the real world you would have used Docker Compose to build the application stack.

Did you like this post? Please share it on your preferred social network, thank you!