PHP + MySQL using Docker Compose
Tags: php, mysql, docker, tutorial
Published: 2018-02-27 02:04:20

---
title: "PHP + MySQL using Docker Compose"
description: "Leverage the power of Docker Compose to create a local development environment for PHP + MySQL."
tags: php, mysql, docker, tutorial
canonical_url: https://alysivji.github.io/php-mysql-docker-containers.html
cover_image: https://thepracticaldev.s3.amazonaws.com/i/1a9n18vev0tdfhad6k3m.png
published: true
---

_This [post](https://alysivji.github.io/php-mysql-docker-containers.html) was originally published on [Siv Scripts](https://alysivji.github.io/)_

Being a Software Engineer isn't just about being effective in a specific programming language, it's about being able to solve any given problem using the tools at hand.

This week at work I have to extend the functionality of a WordPress plug-in so it can fit into our microservices-based backend architecture. This means learning enough PHP to write some production-grade code.

I don't want to install PHP on my local machine so this is the perfect use case for [Docker](https://www.docker.com/)! In this [Quick Hit](https://alysivji.github.io/category/quick-hits.html), I will describe how to create a containerized PHP + MySQL development environment using Docker Compose.

The [LAMP Stack](https://en.wikipedia.org/wiki/LAMP_(software_bundle)) is back!

----

### Instructions

* We'll start by creating a folder for this project: ```mkdir lamp-stack && cd lamp-stack```

* Create another subdirectory, `/php` which contains the following `index.php` file:

```php
<!-- ./php/index.php -->

<html>
    <head>
        <title>Hello World</title>
    </head>

    <body>
        <?php
            echo "Hello, World!";
        ?>
    </body>
</html>
```

* Populate `docker-compose.yml` with the following configuration:

```yml
# ./docker-compose.yml

version: '3'

services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: my_secret_pw_shh
      MYSQL_DATABASE: test_db
      MYSQL_USER: devuser
      MYSQL_PASSWORD: devpass
    ports:
      - "9906:3306"
  web:
    image: php:7.2.2-apache
    container_name: php_web
    depends_on:
      - db
    volumes:
      - ./php/:/var/www/html/
    ports:
      - "8100:80"
    stdin_open: true
    tty: true
```

* Our directory structure should look as follows:

```console
$ tree
.
├── docker-compose.yml
└── php
    └── index.php
```

* Execute `docker-compose up -d` in the terminal and load [http://localhost:8100/](http://localhost:8100/) in your browser.

Time to learn some [P](https://www.w3schools.com/php/default.asp)[H](https://www.tutorialspoint.com/php/index.htm)[P](http://php.net/manual/en/tutorial.php)!


##### Notes

* We use [port-forwarding](https://docs.docker.com/config/containers/container-networking/) to connect to the inside of containers from our local machine.
    * webserver: `http://localhost:8100`
    * db: `mysql://devuser:devpasslocalhost:9906/test_db`
* Our local directory, `./php`, is [mounted](https://docs.docker.com/storage/volumes/) inside of the webserver container as `/var/www/html/`
    * The files within in our local folder will be served when we access the website inside of the container