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
x 1FROM php:7.1.8-apache
2
3MAINTAINER Kartikay Kanojia
4
5# Copying public_html folder files into /srv/app folder.
6COPY public_html/ /srv/app
7# Overwriting apache config file with our config file.
8COPY .docker/php/vhost.conf /etc/apache2/sites-available/000-default.conf
9
10# Installing packages.
11RUN docker-php-ext-install mbstring pdo pdo_mysql exif json fileinfo
12# Changes ownership of the application files to the Apache www-data user, making files writable by the Apache user.
13RUN chown -R www-data:www-data /srv/app \
14 && a2enmod rewrite
15
16# Run apt-get update to update the package libraries. Install zlib1g-dev, libicu-dev, g++.
17RUN apt-get update && apt-get install -y zlib1g-dev libicu-dev g++
18# Install php Intl extention.
19RUN docker-php-ext-configure intl
20RUN docker-php-ext-install intl
Edit docker-php-project/.dockerfile/php/vhost.conf file with following code
11 1<VirtualHost *:80>
2 DocumentRoot /srv/app
3
4 <Directory "/srv/app">
5 AllowOverride all
6 Require all granted
7 </Directory>
8
9 ErrorLog ${APACHE_LOG_DIR}/error.log
10 CustomLog ${APACHE_LOG_DIR}/access.log combined
11</VirtualHost>
Edit docker-compose.yml file with following code
33 1version'3.6'
2services
3 # Project service name 'app'.
4 app
5 build
6 context.
7 dockerfile .docker/php/Dockerfile
8 # Specify custom container name, rather than a generated default name.
9 container_name my-web-container
10 # exposes our app port to container app HOST:CONTAINER.
11 ports
12 80:80
13 # mount the root of our project to the /srv/app folder,
14 # which is where Apache will be looking for our code
15 # (specified in 'vhost.conf' file).
16 volumes
17 ./public_html:/srv/app
18 ./custom.ini:/usr/local/etc/php/conf.d/custom.ini
19 # Linking our 'app' service with 'mysql' service
20 links
21 mysql
22 # Mysql service
23 mysql
24 # pulling latest mysql image.
25 image mysql latest
26 # Exposing mysql port
27 ports
28 3306:3306
29 # Setting 'mysql' environment.
30 environment
31 MYSQL_DATABASE docker_tutorial
32 MYSQL_USER app
33 MYSQL_ROOT_PASSWORD password
Creating Project
Edit public_html/index.php with following code.
20 1<html>
2 <head>
3 <title>Docker PHP Mysql Tutorial</title>
4 </head>
5 <body>
6 <h1>Hello World!</h1>
7 <h4>Attempting MySQL connection from php...</h4>
8
9 $host = 'mysql';//Same as mysql service name.
10 $user = 'app';
11 $pass = 'password';
12 $conn = new mysqli($host, $user, $pass);
13
14 if ($conn->connect_error) {
15 die("Connection failed: " . $conn->connect_error);
16 }
17 echo "Connected to MySQL successfully!";
18
19 </body>
20</html>
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.
1 1docker-compose up --build
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.
1 1docker-compose up
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.
1 1docker-compose stop
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
26 1version'3.6'
2services
3 app
4 build
5 context.
6 dockerfile .docker/php/Dockerfile
7 container_name my-web-container
8 ports
9 80:80
10 volumes
11 ./public_html:/srv/app
12 ./custom.ini:/usr/local/etc/php/conf.d/custom.ini
13 links
14 mysql
15 mysql
16 image mariadb latest # CHANGE FROM MYSQL TO MARIADB.
17 ports
18 3306:3306
19 # Specify database volume.
20 volumes
21 ./database_volume:/var/lib/mysql
22 ./database_volume/docker_tutorial:/var/lib/mysql/docker_tutorial
23 environment
24 MYSQL_DATABASE docker_tutorial
25 MYSQL_USER app
26 MYSQL_ROOT_PASSWORD password
Rebuild docker
1 1docker-compose up --build
That’s it, now even after image rebuilding our database is stored in our local machine inside ‘database_volume’ folder.
0 Comments