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
- php:7.4.9-apache-buster
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'
services:
db:
image: mysql:8
volumes:
- db_data:/var/lib/mysql
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: somepass
apache:
build:
context: ./
dockerfile: Dockerfile
ports:
- '8081:80'
- '4443:443'
volumes:
- ./websites:/var/www/html
- ./conf:/etc/apache2/sites-enabled
environment:
- ALLOW_OVERRIDE=true
volumes:
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 :
conf
test.com.conf
websites
test
docker-compose.yml
Dockerfile
Under the conf directory you can define as many virtual hosts as needed (ex: test.com.conf).
<VirtualHost *:80>
ServerName test.com
DocumentRoot "/var/www/html/test"
<Directory "/var/www/html/test">
Options Indexes FollowSymLinks
DirectoryIndex index.php
AllowOverride All
FallbackResource /index.php
Require all granted
</Directory>
</VirtualHost>
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 test.com 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 test.com onto our machine (example taken for Linux) :
echo '127.0.0.1 test.com' | sudo tee -a /etc/hosts
Testing
Let’s add a phpinfo() command within the index.php under websites/test/ folder onto our local machine.
<?php
phpinfo();

Here you are !
if you have correctly issued the following command and mimicked the structure proposed, then when you hit the http://test.com:8081 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 127.0.0.1
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.
[…] 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 […]
[…] you already have a stack (Php Mysql…) on which you can test your scripts, otherwise follow this resource to build a LAMP docker […]