This is a quick tutorial to setup (W|L|M)AMP stack in docker.
You can use either docker or docker toolbox.In this tutorial we are going to cover set of instructions on how to create Development Environment for Docker using PHP, MySQL and Apache webserver.

Docker Installation

1. Install Docker. Download docker from official docker website.

*If you are running Windows OS without Hyper-V, you would need Docker ToolBox installed on your system which uses docker-machine to create and attach to a VM. This machine is a Linux VM that hosts Docker for you and your Windows system.

To setup Docker on your system, click Docker Installation and follow the instructions.

2. In the Docker Quick-Start Terminal, docker-machine ip command will give the ip address of the docker host.

Setting Up Docker

Project Structure

├──docker-php-project
│   └── .dockerfile
│        └──php
│           ├──Dockerfile
│           └── vhost.conf
├──public_html
|   └──index.php
├──database_volume
|   └──docker_tutorial
├──docker-compose.yml
├──custom.ini 

Edit docker-php-project/.dockerfile/php/Dockerfile file with following code

 

Edit docker-php-project/.dockerfile/php/vhost.conf file with following code

 

Edit docker-compose.yml file with following code

 

Creating Project

Edit public_html/index.php with following code.

 

Building PHP, MySQL Docker Image

Building
First we build our image which we modified from official php image (php:7.1.8-apache).

Open CLI of your OS and run docker compose command with up and build option.
Run this command in folder where docker-compose.yml is placed.

 

Docker download (pull) the PHP image we are extending, it might take a few minutes.
After build completes container start running.

Running
To run docker container run docker compose command.

 

Now open your browser and open url
* http://localhost:80 – if you are running docker.
* http://192.168.99.100:80 – if you are running docker toolbox. If this ip does not work then look into docker virtual box machine ip address or in kitematic.

Stopping
Run docker compose command with stop option.

 
You can also press ctrl+c combination in window CLI where docker compose up is used.

Using Volume With MySQL

When ever docker image is rebuilt MySQL data is cleaned because rebuilding delete old data.
To save data we use volume, but the problem is mysql do not copy data to volume because of data folder protection in mysql.
So use MariaDB to save data in volume.

Now we see docker real power. Instead of doing heavy work we can change from mysql to mariadb easily.

Edit docker-compose.yml file with following code

 

Rebuild docker

 
That’s it, now even after image rebuilding our database is stored in our local machine inside ‘database_volume’ folder.


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *