“Dockerizing your web application stack (Nginx, PHP, MySQL)” is about how to convert your web application stack from multiple VMs or bare metal based into Container based using Docker.
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
Dockerize Your Web Application Stack - Salman El Farisi
1. Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Dockerize Your Web
Application Stack
(PHP, MySQL and NginX)
Salman El Farisi
dev.salman.farisi@gmail.com
salman.farisi@badr-interactive.com
Lead Developer @ PT. Badr Interactive
2. What is Docker ?
• Docker is a software containerization platform
which enable us to run our software in
isolated environment (container).
• Isolated => keep the consistency of our
software dependencies without being worried
to be conflicted with other software in the
same machine.
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
5. Docker Installation (2)
• Download and Install Docker for desktop:
– Mac :
https://download.docker.com/mac/stable/Docker.dmg
– Windows (using Hyper-V) :
https://download.docker.com/win/stable/Docker%20for%
20Windows%20Installer.exe
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
6. Docker Installation (3)
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Enable Hyper-V on Windows:
Docker for Windows need Hyper-V to run
Linux Container.
7. Docker Installation (4)
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Installation on Progress
Wait until the installation
process is finished
8. Docker Installation (5)
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Test Your Docker Installation
Open terminal / powershell and run
`docker version` command to check
whether your Docker is installed
9. Say Hello To Docker!
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
It’s time to say hello :)
Run `docker run hello-world` from
your terminal / powershell to make
sure your docker is working properly
10. Dockerize
• Is a process converting your app to run within
a Docker container
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Container Container Container
Code Code
VPS
11. Typical Web App Stack
• Web Server (NginX)
• Database (MySQL / MariaDB)
• Language (PHP)
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
12. Fat Container vs Slim Container
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Container
Code
PRO:
• Straight Forward
• Simple
CONS:
• Huge Size
• Tightly Coupled
13. Fat Container vs Slim Container (2)
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Container
Container
Container
Code
Code
PRO:
• Small Size
• Loosely Coupled
CONS:
• Need some effort to
compose all of
them
14. Docker Compose
• Compose is a tool for defining and running
multi-container Docker applications. With
Compose, you use a YAML file to configure
your application’s services. Then, with a single
command, you create and start all the services
from your configuration.
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Source: https://docs.docker.com/compose/overview/
15. Docker Compose (2)
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Docker compose definition file:
1. It must be named as `docker-
compose.yml`
2. Consist of 3 sections:
• Services
• Volumes (optional)
• Networks (optional)
3. This is the place where you put all
of your container definition and
linking them together
16. Dockerize Your App
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
What we need to do:
1. Make sure NginX can read our
“src” folder (index.html)
2. Make sure NginX can execute the
php script (index.php)
3. Make sure NginX can connect to
DB (db_connect.php)
17. Dockerize Your App (2)
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Docker-compose syntax version
Our container name
Docker image that we will use for our
webserver (nginx ver. 1.13)
Expose container port
(80) into host port (80)
Bind “src “ folder in host to
“/usr/share/nginx/html in container
Goal:
Make sure NginX can read our “src”
folder (index.html)
18. Dockerize Your App (3)
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Run `docker-compose up -d` to run our Docker container
Run `docker ps –a` to check our container status
19. Dockerize Your App (4)
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Open web browser and type ‘localhost’ to check our app
Goal: Make sure NginX can read our
“src” folder (index.html) [COMPLETED]
20. Dockerize Your App (5)
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
ContainerContainer
CodeCode
:9000
PHP-FPM
(Fastcgi Process Manager)
Goal: Make sure NginX can
execute the php script
(index.php)
What to do:
1. Bind our source code folder
into PHP Container, so it can
execute our php script file
2. Link nginx and php together
so that nginx can pass the
request into php container
(via port 9000)
21. Dockerize Your App (6)
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
PHP FPM Container definition
Define a network called “network01”
Connect php container into “network01” and
give alias “phpnetwork” so that other container
can “ping” this container using it’s alias (treat this
like a host name)
22. Dockerize Your App (7)
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
NginX Configuration:
1. Create new file called “nginx.conf”
2. When there is a request with “.php” at
the end of url / location, pass the
request into php container
3. We can pass the request into php
container by using it’s alias name
(must connect into the same network)
23. Dockerize Your App (7)
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Bind “nginx.conf” file in host
into “/etc/nginx/conf.d/default.com” in container
Connect webserver container into
“network01” (same as php container)
Update webserver container definition:
24. Dockerize Your App (8)
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Run ‘docker-compose up –d’ again
Open web browser and go to ‘http://localhost/index.php’
to check our app
Goal: Make sure NginX can execute the
php script (index.php) [COMPLETED]
25. Dockerize Your App (9)
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Configure MySQL container using
environment variable
Connect MySQL container into
“network01” (same as php) and give
alias “database”
Bind port “3306” in container into
port “3366” in host / local (you can
connect to MySQL container from
host / local using this port)
26. Dockerize Your App (10)
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Run ‘docker exec –it php /bin/bash’
to go inside php container
Run ‘docker-php-ext-install pdo mysqli’ (inside container) to
install php pdo and mysqli extension.
27. Dockerize Your App (10)
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Build php script to test our database connection
28. Dockerize Your App (11)
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Run ‘docker-compose up –d’ again
Open web browser and go to ‘http://localhost/db_connect.php’
to check our app
Goal: Make sure NginX can connect to
DB (db_connect.php) [COMPLETED]
29. Dockerize Your App (12)
Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Container ContainerContainer
CodeCode
30. Thursday, April 26, 2018 DevOpsDays @Menara BTPN, Jakarta
Thank You :)
Salman El Farisi
dev.salman.farisi@gmail.com
salman.farisi@badr-interactive.com
Lead Developer @ PT. Badr Interactive