LAMP Stack on Docker with Docker Compose

This passage will introduce how to install LAMP Stack on Docker, and we will learn how to make use of Docker Compose in cases like this.



LAMP is a suitable software bundle for building dynamic web sites and web applications. With Docker, we can quickly build web services using LAMP and deploy them anywhere. You can directly install the entire LAMP components in a single Docker container. But that is never the best solution, because, for example, when an error occurs and ruins the MySQL database, we don’t want to delete everything and set up every single component again. With Docker Compose, we can create components in separate containers with ease. So when an error occurs, we just need to rebuild damaged components. This passage will introduce how to install LAMP Stack on Docker, and we will learn how to make use of Docker Compose in cases like this.


In order to install LAMP Stack on Docker, you will need to have Docker Desktop on your computer or have both Docker Engine and Docker Compose on your Linux server.

Find resources we need on Docker Hub

In this tutorial, we need a PHP+Apache image and a MySQL image. They can be find on Docker Hub. We just need to search them on Docker Hub and get right into the official page.

PHP images

We need to keep down the specific tag to fetch the image we want. Supported tags are shown on the Docker Hub page.

PHP image supported tags

Now the tag we need is “apache”. We will use it later.

Pull images and run containers

We use pull and run command to fetch them and get them to work.

First we pull and run PHP+Apache using the commands below (You need to change [HOST DIRECTORY] to specify where you put your web pages on the host) :

docker pull php:apache
docker run -d -p 80:80 -v [HOST DIRECTORY]:/var/www/html php:apache

In the same way, we get MySQL to work. What is a little bit different here is that we need to specify a password for MySQL (You need to change [YOUR PASSWORD] to your own password for MySQL) :

docker pull mysql:latest
docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=[YOUR PASSWORD] -d mysql

If these commands go with no error, the two containers are both running. They can reach each other since they are in the same virtual network by default.

Check if it works

Use a browser on your host and open http://localhost. If you have put your own web pages in the directory you specified before, it should display those pages. If no web page is in the directory, you should see a forbidden error. This shows that the LAMP stack is installed successfully.

Forbidden sign

Write Docker Compose File

Using commands above will get the LAMP stack to work. But every time we need to create a new LAMP stack, we need to run the commands again. It’s quite troublesome. With a Docker Compose file, we just need to write it once and run as many times as you want.

Docker Compose file is similar to the command, but uses different syntax. Here is the official explanation of Docker Compose. And if you want to know more specifications of Docker Compose file, please visit here.

In this case, we create a new text file named docker-compose and write the content as below:

        image: php:apache
          - "80:80"
          - [YOUR DIRECTORY]:/var/www/html
        image: mysql:latest
          - "3306:3306"
        command: --default-authentication-plugin=mysql_native_password

All the parameters are clear and easy to understand. You just specify the parameters and all of the services run after the commands below is executed (You need to change [DIRECTORY] to the absolute directory where you save the docker-compose file):

docker-compose up

If no error occurs, the two containers will both start to run and the LAMP stack is installed successfully.

When you again need to set up a same LAMP stack, no need to run multiple commands, no need to write a new Docker Compose file, just use this file and the two simple commands and everything works fine.

docker-compose up

If you enjoy this passage and want some more details about Docker Compose please comment below and let me know!