Categories
Admin Docker PHP Programming Tutorials Web Dev

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 https://docs.docker.com/install, it’s very easily explained if you follow step by step.

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("172.17.0.2", "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 comment here below and share it on your preferred social networks, thank you!

By Mirko Benedetti

Hi there, my name is Mirko Benedetti and I'm a Software Developer. Excellence is what I consider to be our ultimate goal, and passion for technology constantly drives me to it. I began programming self-taught at a very young age; since then I learned a lot and every day I love learning new things. Here is my Linkedin Curriculum, feel free to connect.

6 replies on “Short Tutorial on Docker, PHP and MySQL”

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

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?

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,
Thanks

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

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

Leave a Reply

Give me your opinion, I will be grateful.