How to enable mod_rewrite in Docker Official PHP

Maybe are suffering like me, trying to run you PHP application that uses a .htaccess file to overwrite some URL on official PHP Docker image, in my case was using Laravel.

The problem? Well, I have to add some feature to and old app made in Laravel some year ago and I did using xampp, we are living in the future so now we have containers. I don’t be very good using Docker yet I think I have a lot to learn but I try my best.

I have a repository with a Docker configuration to run a PHP server with a mysql services that fit with my need to run the old app and work in the new features. The first problem that I have was to enable the directory where the Laravel app is, I solved this adding the volume to the docker-compose file

version: '3.1'
    build: .
    - ${APACHE_PORT}:80
    - db
    - ./www:/var/www/html/
    # Laravel app
    - ./local_laravel_app:/var/www/server_laravel_app/

Great, now the app is available in the PHP server container, but there one thing left to fix, the mod_rewrite. This module doesn’t come enabled by default in php:x.x-apache image thus is have to be added to the Dockerfile configuration. So the Docker file it can look like this:

FROM php:5.6-apache
RUN docker-php-ext-install mysqli
RUN a2enmod rewrite
RUN service apache2 restart

We have to add the a2enmod to enable the mod_rewrite and restart the Apache server.

Install pdo_mysql library using WordPress image in Docker

If you work with the image of Worpress for Docker and need to do something “out of the box” like connecting to the database that uses the WordPress image from a script that is not part of the WordPress theme it will not be possible because you need to install the MySQL pdo library in WordPress container.

To solve this we only need only two commands:

  • A command to install the pdo library that we need
  • A command to restart the WordPress container to load the new installed library

Install pdo library in WordPress container:

docker exec -it container_wordpress_1 docker-php-ext-install pdo_mysql

Restart Apache in WordPress container

docker exec -it container_wordpress_1 /etc/init.d/apache2 reload

Ready, your custom script can now connect to the same database used by the WordPress container.


For some time I have wanted to leave MAMP to work on WordPress projects and use Docker, luckily this week I came across a tweet from Fran Piaggio where he shares a repository of Jared Palmer with a basic configuration of Docker to work on WordPress in 2019. In addition to the WordPress configuration Jared added a Webpack what is a very useful.

The repository of Jared encouraged me to take up my idea of having a WordPress configuration for my projects, so with that base I can be guided to create my own configuration that I call Dockerpress.

Unlike the Jared repository, it does not have Webpack configuration, since I use another configuration that you can find in Webpack Boilerplate repository, what I have added to it is a Mailhog as a service to catch test emails and an file for enviroment values.

I hope it is useful or a basis for your own configuration.

Dockerpress on Github