Create a WAMP environment with docker

An easy code to install Apache, MySQL and PHP with Docker !
Sunday, October 31, 2021

Here is a simple way to make a WAMP server with Docker.

WAMP is an architecture based on Windows,Apache,MySQL,PHP.

But actually, with Docker, we are not constrained by using Windows, so the real architecture is XAMP. (X means all OS)

To do it, first, install Docker on your computer / server.

Create these files in your project folder:

  • ./Dockerfile
  • ./docker-compose.yaml
  • ./www/public/index.php

Put your PHP code in ./www/ directory. The public directory would be ./www/public/ in this case.

The Dockerfile

FROM php:apache AS php_dev
RUN pecl install xdebug && docker-php-ext-enable xdebug
RUN a2enmod rewrite
RUN a2enmod headers
# Install Postgre PDO
# RUN apt-get update \\
# && apt-get install -y libpq-dev \\
# && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \\ # Install postgres extensions
# && docker-php-ext-install pdo pdo_pgsql pgsql
COPY --from=composer /usr/bin/composer /usr/bin/composer # Install composer in the container
# Point to /var/www/html/public/ folder
ENV APACHE_DOCUMENT_ROOT /var/www/html/public/
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
FROM php AS php_prod
# Your prod config here

Note: the highlighted lines are about installing composer and postgresql database extension. Comment them if you don’t need them !

The docker-compose.yml

version: "3.4"
services:
site:
build:
context: .
target: php_dev
ports:
- "80:80"
volumes:
- ./www/:/var/www/html/
adminer:
image: adminer
ports:
- "8080:8080"
database:
image: mysql
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=test
- MYSQL_USER=admin
- MYSQL_PASSWORD=root

The PHP code

<?php
var_dump('TEST XDEBUG');
phpinfo();

And then do docker-compose up and target the port 80 of your machine (generally http://localhost:80)

Don’t forget to add tags to freeze the dependencies and images you installed !


Recommended articles