LAMP Docker Tutorial

Today we bring you a small pill of knowledge with the hand of Docker for website development in PHP.

What web developer has not had to configure a lamp setup with MySQL, PHP/Apache etc ? There are many solutions and today I want to present to you a small Tutorial to have your own LAMP Stack so you can develop easily any php website with a simple Docker-compose file.

LAMP Docker Tutorial

There are many solutions to do this. Since us - web developers are normally inside so many projects, we need to be able to switch between setups so we are going to mount everything from a docker-compose.yml file that we can just copy and run Docker easily.

First things first, I'm asuming that you have but if you haven't, install Docker in your computer. Up to windows Windows 10 Home you have to use the docker-toolbox, where as Windows 10 pro users get to use the latest version of Docker containers.

Check that your installation is correct by executing

docker --version

Now, create an new folder where you will be working and create a new file, paste the following code and save it as docker-compose.yml. Don't worry we will explain what's going on in a minute

version: '2'
    image: 'mariadb:latest'
      - MYSQL_USER=local_user
      - MYSQL_DATABASE=local_database
      - MYSQL_ROOT_PASSWORD=password
    # volumes:
      # Mounting the content of the database inside of a subfolder named mariadb_data
      # - './mariadb_data:/var/lib/mysql'
      - '3306:3306'
      - 3306
    image: adminer
    restart: always
      - 8080:8080
    image: php:7.3-apache
      # Mounting the website inside of a subfolder named app
      - ./app/:/var/www/html
      - 80
      - 80:80

Let's analyze what Docker is doing here. We are going to create three services. One of the services will be the mysql database. The next service will be a mysql explorer like phpMyadmin called Adminer. This will allow us to explore the mysql database if we don't happen to have any sql explorer installed.

The last service will be the Apache php service with version 7.3 . If you need another specific version for PHP, simply change the numbers.

Let's go back to the docker terminal (I suggest you use the Docker Quickstart Terminal if you are not familiar with the cli or if you are like me and have it missconfigured in your laptop) and run the command.

docker-compose up

If any errors occur, make sure that the docker machine has access to the folder you are working in. (Share the folder in either Docker Desktop or Docker toolbox for legacy systems).

Now, check the IP of your docker virtual machine (normally it would display in localhost, but in windows legacy systems you'll need to check for the IP Address first. If you want it fast simply check it in Kitematic.

Now access your ip in your web browser. Remember you can access the port 8080 for the adminer interface for mysql, and you can access the port 3306 to connect to the mysql server.

Exploring the docker-compose.yml

The Docker-compose.yml files are pretty self explanatory. Whenever I work with projects or systems I like to set them up user Docker compose or docker stack, and run the images from my docker-compose.yml.

For the time being. we use EXPOSE to expose the docker container ports to any other app or container. This way we can access the database server from any other application. From the docker-compose what we are doing is declaring 3 different services that will act as independent containers.

If we wanted for example, to create a personalized php.ini file for our php server. We would need to create a php.ini file and then, under the volumes section in the apache: service , we should add the following line (remember to keep the indentation).

- ./php.ini:/usr/local/etc/php/php.ini

And in the same folder as we have the docker-compose.yml file we should have a php.ini file with the modifications that we wanted to personalize for our server. Check the specs of the php machine by echoing a phpinfo(); and then create your php.ini accordingly to your installation needs.

If you can't be bothered in editing the whole thing and you only need to modify it, you can always create a backup in your folder by accessing your apache docker container via ssh (or kitematic). Navigating to the file in the /usr/local/etc/php/ folder and then copying the php file to the /var/www/html . A new file will automatically pop up that you can edit and then add the line previously mentioned

Happy Coding!

Be the first to comment

Our reader's favorite picks

Subscribe to our newsletter

Let us send you spam from time to time. Yes, it is a bit of Spam, but interesting Spam, supreme quality spam to be honest!

Email *