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, which is known 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, which have to coexist in the same machine.

The Docker virtual environment

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 (emulated on different other operating systems) and in two different editions. This tutorial is based on Fedora 27. For the beginning choose the Community Edition (CE) and start it like this:

$ sudo systemctl start docker

Now that you have all installed, 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 images are layerized, so common parts of them do not have to be downloaded again and 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"

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 networks or comment here below, thank you!

10 thoughts on “Short Tutorial on Docker, PHP and MySQL

  1. It’s awesome for me to have a web page which is useful in favor of my experience. Thanks Mirko

  2. Good day! Do you know if they make any plugins to protect against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any recommendations?

  3. Hello Chas,
    that’s not exactly the topic of this post, but it could be a topic for this blog in general.
    There are many plugins out there, each CMS has its own.
    Looking forward to writing a post on that,

  4. I just like the helpful info you supply in your articles.
    Best of luck for the next!

  5. Thank you Frankie,
    I’m looking forward to write my next tutorial.

  6. Thanks for your informative blog. Where else could I get that kind of info written in such a way? I’ve a project that I’m just now working on, and I have been searching for such info.

  7. Thank you Benjamin,
    if you want you can share your project with us, otherwise contact me in private to discuss it in more detail.

  8. I can definitely see your enthusiasm within what you write. The arena hopes for more passionate bloggers such as you, who aren’t afraid to say how they believe.

  9. Hello Chen,
    what you say is true, especially if you look at the full blog; not this article in particular.
    I’m merely trying to transmit my passion to people.

Leave a Reply

Give me your opinion, I will be grateful.