Introduction

I just clean installed my OS, and was gonna set up my development system and thought: might as well make a blog out of it. We're gonna use docker compose on this guide.

What is Docker?

TL;DR: Copy the example here.

Prerequisites

  • Docker
  • macOS / Windows 11 / Linux

Structure

Folder
- docker-compose.yml
- Dockerfile
- yourfiles ...

docker-compose.yml is a config file allows you to define the services, networks, and volumes required to run your application in a declarative way.

Dockerfile is a text document that contains instructions for building a Docker image.

Example

Here's a working example demonstrating how to use Docker to run a PHP application with Apache as the web server and MySQL for the database.

Note: Some parts are not explained, just google the words and modify it to your needs.

The directory should look similar to this.

Project1
- docker-compose.yml
- php81.Dockerfile
- index.php
- info.php
...

php81.Dockerfile

It means that use docker image php:8.1-apache, edit apache's document root, move the ini-development file to make the php.ini (when hosting use ini-production), install php extensions, enable mod rewrite, install composer.

You can change php:8.1-apache to php:8.3-apache if you want the latest PHP.

You can change APACHE_DOCUMENT_ROOT to /var/www/html/public if you're using something like Laravel.

You can add php extensions using docker-php-ext-install or using docker-php-extension-installer if the extension needs dependencies not local to the image. Or just only use the latter.

The example above uses the ones that I use and the common extensions, Modify it to your needs. Some extensions are pre-enabled and it depends on the image so please check phpinfo or php -m.

docker-compose.yml

It means that make services named php81, db, phpmyadmin that uses the same network. php81 builds using our php81.Dockerfile. db builds using the image mysql/mysql-server:8.0, and so on.

If you are using Windows use backslash (\), and forwardslash (/) when using Linux/Mac. So in Windows it will look something like this C:\project:var/www/html you only need to edit the left side of colon (:), since the right side is the container's.

To Change MYSQL Password, edit MYSQL_ROOT_PASSWORD value.

info.php

index.php

On terminal run the command docker compose up -d in your project directory

You can access the project in your browser using localhost

Check PHP info localhost/info.php

Access phpMyAdmin using localhost:8080

How to Enable Directory Indexing

If you we're using WAMP or XAMPP before, you might have been used on enabled indexing which they defaulted. So, if you want to enable directory listing follow this.

php81.Dockerfile

Edit php81.Dockerfile and add this. This line enables directory indexing.

On your terminal run docker compose up -d --build

Go to your browser localhost to see if it reflect changes (should look like image below)

Docker Apache Mysql PHP Directory Indexing

Multiple PHP Versions

So you are wondering how to do set up multiple versions of php using docker? Just add a new service, or (protip: make a Dockerfile per project).

The directory should be similar to this.

Projects
- docker-compose.yml
- php81.Dockerfile
- php56.Dockerfile
...

docker-compose.yml

php56.Dockerfile

Run the command docker compose up -d

PHP 8.1 localhost:8081

PHP 5.6 localhost:8056

Final Thoughts

Remember to check ports if you are using many containers. Play with it and learn. GLHF.