Dockerize LAMP

Let’s produce a docker-compose file to let a full website run from on any of your machines !

Actually more precisely we will prepare a docker compose file so we can test some web resources real quick and not only WordPress, Drupal, Magento… !

Checkout the features covered :

  • Docker Compose
  • Apache
  • Mysql
  • Php
  • Virtualhosts

I personally use this set up in order to quickly test  some external samples (ie : if you have some students, coworkers…).

You don’t need to install the full LAMP, WAMP, MAMP stack anymore !

Apache & Docker

Whenever you are looking for some server set up, you should think of Docker and browse Docker Hub to find a maintained image about it.
As an example Apache is widely used and there are a lot of repositories to get started with and Bitnami often stands out.
Bitnami is a company offering packaged applications you can use to speed up your environment configuration.
For a nice study, take some time reading on Bitnami about Lamp.
And in case you plan on using a single image per website type (ex: Drupal, WordPress…), take a look at the provided images.
But for now we need to run services that let we ship any type of web resource using Php with some added extensions (PDO…). Rather than using a Bitnami image we will use the Apache candidate from the official docker Php image :
  • php:7.4.9-apache-buster
Based on the above image we will need to add some extensions, hence we need to use a Dockerfile and add a few lines on top of the Php layer :
FROM php:7.4.9-apache-buster
RUN apt-get update \
  && apt-get install -y --no-install-recommends libpq-dev \
  && docker-php-ext-install mysqli pdo_pgsql pdo_mysql \
  && mv "/usr/local/etc/php/php.ini-development" "/usr/local/etc/php/php.ini"

Note : you can replace the php.ini-development sample by the production one depending on your needs.

The helper script docker-php-ext-install is used to add as many extensions as needed (See here for more information).

Here we simply add the PDO extension so we can use the library within Php scripts. You can add additional extensions in the same file – check the Dockerfile documentation if you need or drop a comment here :).

Now it’s time for the docker-compose.yml file :

version: '3'
    image: mysql:8
      - db_data:/var/lib/mysql
      - '3306:3306'
      MYSQL_ROOT_PASSWORD: somepass

      context: ./
      dockerfile: Dockerfile
      - '8081:80'
      - '4443:443'
     - ./websites:/var/www/html
     - ./conf:/etc/apache2/sites-enabled
     - ALLOW_OVERRIDE=true

  db_data: {}

The ALLOW_OVERRIDE directive comes from Apache server configuration which allows you to use .htaccess or not (see Apache for more details).

The apache service is built on top of our Dockerfile, external ports are filled in for us to be able to access the different web resources embed in the resulting container.

Next you will see the configuration under the conf folder.

And before  you start using the services, make sure you added the website you wish to test under this environment !

Structure of your setup

Following is the hierarchical structure required to run your services from your docker-compose file :


Under the conf directory you can define as many virtual hosts as needed (ex:

<VirtualHost *:80>
  DocumentRoot "/var/www/html/test"

  <Directory "/var/www/html/test">
    Options Indexes FollowSymLinks
    DirectoryIndex index.php
    AllowOverride All
    FallbackResource /index.php
    Require all granted

If you already experimented Apache installation and configuration of VirtualHosts then you already know about it.

As a reminder, we tell Apache that we need to listen to requests matching the domain name and that we want to return the result of the scripts located into the /var/www/html/test folder.

Now locally, let’s redirect the requests toward onto our machine (example taken for Linux) :

echo '' | sudo tee -a /etc/hosts


Let’s add a phpinfo() command within the index.php under websites/test/ folder onto our local machine.

Php site working on docker lamp

Here you are !

if you have correctly issued the following command and mimicked the structure proposed, then when you hit the page you get the same result.

$ docker-compose up

This command should be triggered next to your docker-compose file. Just add the -d argument in order to run in the background.

Mysql Access

Of course we do not forget to talk about the database here, you will likely need to create Databases and connect to them from within your web resources.

If you are used to having your own Mysql instance on your machine, you might enjoy using the Mysql Client to connect.

Then as usual get yourself onto you Mysql server :

$ mysql -uroot -p -h

Create a user for more security

This it the last tip before leaving you with your various DB commands.

Mysql 8 should be running now and the first thing to do is creating a new user with sufficient rights to connect to your database :


CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON db1.* TO 'jeffrey'@'localhost';

All done, I hope you liked the lines covered on this page, I now let you to your own tests. I’ll be welcoming any comments !

The whole example is available on the Docker Lamp repository.

2 responses to “Dockerize LAMP”

  1. […] should be running a Php server, no SQL needed. You could take a look at the Lamp & Docker guide to get a full set up running on your […]

  2. […] you already have a stack (Php Mysql…) on which you can test your scripts, otherwise follow this resource to build a LAMP docker […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Want more information?

Related links will be displayed within articles for you to pick up another good spot to get more details about software development, deployment & monitoring.

Stay tuned by following us on Youtube.

%d bloggers like this: