Windows Docker Container with PHP, Apache and MariaDB

The new year saw me refreshing one of my popular development stacks. My main system is Windows 10 (2004) and for some project work, I use Docker to enable the use of specific versions of PHP with MariaDB and Apache. I decided to update one of my stacks to more recent versions, including an updated docker base image.

For this build my Dockerfile now looks like this: –

Directory Structure

This is the contents of the directory containing the docker file.

  • scripts\installMariaDB.ps1
  • scripts\installPHP.ps1
  • scripts\systemConfig.ps1
  • packages\httpd-2.4.46-win64-VS16.zip
  • packages\mariadb-10.5.8-winx64.msi
  • packages\php-7.4.13-Win32-vc15-x64.zip
  • packages\VC_redist.x64.exe
  • mounted\my_web_site\apache_conf\httpd.conf
  • mounted\my_web_site\php_ini\php.ini
  • remove_temp_container.ps1
  • grab_blank_db.ps1
  • Dockerfile
  • create_my_web_site_container.ps1
  • buildImage.ps1

Build Image

This script simply contains the docker command to build the image: –

The thread-safe version of PHP is being used as this is required by Apache and comes with php7apache2_4.dll.

After installing Apache we are setting the service so it will not automatically start. Instead, we will be launching Apache when the container starts so we can specify a mounted location for the httpd.conf file. This slows down the starting of the container however, for development work once the stack is up and running it stays so for a while. As such I can afford a minor delay.

MariaDB

MariaDB binaries can be obtained from https://downloads.mariadb.org/mariadb/+releases/


In the above example, I have set the password to 1234. Make sure you use something more secure and imaginative.

MariaDB is configured to store its databases within the image in directory C:\DBData. When the container is created MariaDB will create its default databases within the container at this location. However, I prefer the database to be stored on my host PC so it can be used by different containers. Doing so also simplifies my back-up strategy.

If I am doing this for the first time, or wish to start again with a fresh MariaDB database, I create a temporary container called temp_container and copy the database files from the container to the host. In this example, my host location is g:\mariadb_databases\10.5\DBData\.

Once the files are copied to the host we no longer need the container which can be deleted: –

systemConfig.ps1

The container we are creating will be serving web pages that are accessible from the host’s via port 8080. E.g. http://localhost:8080/index.php

The following command resolve issues with loopbacks. Port 8080 is only visible on the host, if any PHP code, which is executing within the container, uses port 8080 it will fail. For example, the PHP script may look at the incoming URL and use the port number specified there.

This command instructs Windows running within the container to map port 8080 to port 80 for localhost requests as this is the port Apache, within the container, is listening on.

Creating the Main Container

The following script creates the container that we will be working with. In this example, the web site is located on my host at G:\my_web_site which contains a simple index.php file.

The image’s name is called myWebSite and as you can see were are using 4 mounts to point to host locations where we store: –

  • Web site files and scripts to serve
  • Database storage
  • PHP’s configuration file php.ini
  • Apache’s configuration file httpd.conf

I usually have serveral slightly different configs and containers to support different web sites and testing configurations.

The above script is using’s docker’s run command to manually start Apache with a specified httpd.conf file to use. C:\HTTPConf\ is mounted to my host’s G:\docker\builds\phpMDB\mounted\my_web_site\apache_conf\ directory.

Leave a Reply

Your email address will not be published. Required fields are marked *