Docker PHP + Apache + MySQL

How to run a Docker container with PHP + Apache + MySQL.

To run PHP with Apache:

docker run -d -p 8056:80 --name my-apache-php-app -v "$PWD":/var/www/html php:7.2-apache

To connect to an existing MySQL container (see separate HowTo on Docker MySQL):

docker run -d -p 8056:80 --name my-apache-php-app -v "$PWD":/var/www/html --link some-mysql:mysql php:7.2-apache

At this point, there is just only the sqlite database driver installed:

docker exec my-apache-php-app php -i|grep pdo

-> pdo_sqlite

To add the MySQL PHP module to the container, we can either build a new image by hand, or write a Dockerfile to specify and build a new image from. Here is the steps to build a new image by hand:

docker start <containerId>

docker exec -it <containerId> /bin/bash

# apt-get update

#docker-php-ext-install pdo pdo_mysql

At this point, we can restart the container, when the MySQL PHP driver will be loaded and active.

To create a new image from the existing container:

docker commit -a “image-author-name” -m “Added MySQL PDO driver” <containerId> php-7.2-pdo-mysql

And to start a container from the new image:

docker run -d -p 8056:80 –restart=unless-stopped –name <new container name> –link some-mysql:mysql -v /home/admin/var/www/html:/var/www/html php-7.2-pdo-mysql


Another option is to write our own Dockerfile. See other HowTo for that.