Containerize PHP With NGINX, MySQL, and MongoDB Using Docker Containers - Tutorials24x7
Containerize PHP With NGINX, MySQL, and MongoDB Using Docker Containers - Tutorials24x7
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
Ad removed.
Details
Ads by
Send feedback Why thi
Recent Posts
Create a directory to store your project-specific files. I have created the directory helloworld to store all
the project files. Now create the file docker-compose.yml at the root of your project directory as shown
below. Container
ize PHP
with
# docker-compose.yml
NGINX,
version: "3.8"
MySQL,
services:
and
nginx:
MongoDB
container_name: nginx
using
build: ./docker/nginx
Docker
command: nginx -g "daemon off;"
Container
links:
s
- php
ports:
- "80:80"
volumes:
Container
- ./src:/var/www/html
ize
https://devops.tutorials24x7.com/blog/containerize-php-with-nginx-mysql-and-mongodb-using-docker-containers 1/11
8/18/22, 4:47 PM Containerize PHP with NGINX, MySQL, and MongoDB using Docker Containers | Tutorials24x7
php:
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY MongoDB
container_name: php
and
Affordable Access To Latest LC
build: ./docker/php
Mongo
ports:
HOME Open
Express
Future Proof -Your Lab With LC Refresh. Flexible Financing,ABOUT
Personalized
US Training & Support
"9000:9000"
using
Waters TERMS & CONDITIONS
volumes:
PRIVACY POLICY Docker
- ./src:/var/www/html
Container
working_dir: /var/www/html s
Now create the directories - docker and src within the project root directory. Also, create two directories
within the docker directory i.e. php and nginx.
Container
Create the Dockerfile within the PHP directory as shown below. ize MySQL
and
phpMyAd
# docker/php/Dockerfile
min using
FROM php:8.1-fpm
Docker
Container
Create the Dockerfile within the NGINX directory as shown below. s
# docker/nginx/Dockerfile
FROM nginx:1.20
Create the index.php file within the src directory as shown below.
# src/index.php
<?php
echo phpinfo();
We also need to configure the Server Block or Virtual Host to pass the PHP requests to PHP-FPM vial port
9000. Now, create the default configuration file as shown below.
# docker/nginx/helloworld.nginx.conf
server {
listen 80;
root /var/www/html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
Now, update the Dockerfile within the NGINX directory as shown below.
# docker/nginx/Dockerfile
FROM nginx:1.20
After creating all the directories and files, the directory structure should be similar as shown below.
helloworld
-- docker
-- php
-- Dockerfile
-- nginx
-- Dockerfile
-- helloworld.nginx.conf
-- src
-- index.php
-- docker-compose.yml
Now, run the command docker-compose build to build the images for PHP and NGINX Web Server.
https://devops.tutorials24x7.com/blog/containerize-php-with-nginx-mysql-and-mongodb-using-docker-containers 2/11
8/18/22, 4:47 PM Containerize PHP with NGINX, MySQL, and MongoDB using Docker Containers | Tutorials24x7
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
# Build
docker-compose build
HOME Open
Future
Proof Your Lab With LC Refresh. Flexible Financing,ABOUT
Personalized
US Training & Support
Waters# Output
TERMS & CONDITIONS
PRIVACY POLICY
Building php
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn
Building nginx
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn
After completing the build, we can run the application using the command shown below.
# Launch Application
docker-compose up
# Output
Now open the browser and enter the URL - http://localhost/index.php. It should show the output of
index.php as shown in Fig.1.
https://devops.tutorials24x7.com/blog/containerize-php-with-nginx-mysql-and-mongodb-using-docker-containers 3/11
8/18/22, 4:47 PM Containerize PHP with NGINX, MySQL, and MongoDB using Docker Containers | Tutorials24x7
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
Fig 1
In this step, we will continue with our previous step and install MySQL and phpMyAdmin. We will also
access the MySQL database from the PHP code using PDO. Now, update the docker-compose.yml as
shown below.
# docker-compose.yml
version: "3.8"
services:
nginx:
container_name: nginx
build: ./docker/nginx
links:
- php
ports:
- "80:80"
volumes:
- ./src:/var/www/html
php:
container_name: php
build: ./docker/php
links:
- mysql
ports:
- "9000:9000"
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
mysql:
image: mysql:8.0.27
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: '<root-password>'
MYSQL_DATABASE: helloworld
MYSQL_USER: helloworld
MYSQL_PASSWORD: '<db-password>'
ports:
- "3306:3306"
volumes:
- ./database/mysql:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: pma
links:
- mysql
environment:
PMA_HOST: mysql
https://devops.tutorials24x7.com/blog/containerize-php-with-nginx-mysql-and-mongodb-using-docker-containers 4/11
8/18/22, 4:47 PM Containerize PHP with NGINX, MySQL, and MongoDB using Docker Containers | Tutorials24x7
PMA_PORT: 3306
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
PMA_ARBITRARY: 1
Affordable
restart: Access
always
To Latest LC
ports:
HOME Open
Future Proof -Your Lab With LC Refresh. Flexible Financing,ABOUT
8085:80
Personalized
US Training & Support
# Build
cd <path to project>/helloworld
docker-compose build
# Output
Building php
....
....
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn
We also need to run the command docker-compose up to launch the application as shown below. It will
pull the MySQL and phpMyAdmin images and take time for the first time. The subsequent launches will
be faster.
# Launch Application
docker-compose up
# Output
....
....
....
....
Digest: sha256:382dedf6b43bf3b6c6c90f355b4dda660beb3e099dqw1bb3241170e54fca6d59
....
....
....
....
Now, try to access phpMyAdmin from the Browser using the URL http://localhost:8085. It should show
the phpMyAdmin home page as shown in Fig.2.
https://devops.tutorials24x7.com/blog/containerize-php-with-nginx-mysql-and-mongodb-using-docker-containers 5/11
8/18/22, 4:47 PM Containerize PHP with NGINX, MySQL, and MongoDB using Docker Containers | Tutorials24x7
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
Fig 2
Now, login to phpMyAdmin using the username as root and the root password configured in the docker-
compose.yml. Also, leave the server blank. It should show the phpMyAdmin home page as shown in Fig
3.
Fig 3
# Press Ctrl + C
Now, install the PHP extensions to access MySQL from the PHP source files by updating the Dockerfile as
shown below.
# docker/php/Dockerfile
FROM php:8.1
Also, run the build and up commands to again build the application and launch it. We can test MySQL
connectivity using PHP as shown below. You can also follow How To Connect MySQL Database With PHP.
# src/mysql.php
<?php
$hostname = "mysql";
$dbname = "helloworld";
$username = "helloworld";
$password = "db-password";
try {
https://devops.tutorials24x7.com/blog/containerize-php-with-nginx-mysql-and-mongodb-using-docker-containers 6/11
8/18/22, 4:47 PM Containerize PHP with NGINX, MySQL, and MongoDB using Docker Containers | Tutorials24x7
HOME
ABOUT US
TERMS &$username,
$conn = new PDO( "mysql:host=$hostname;dbname=$dbname", CONDITIONS $password
PRIVACY POLICY
Affordable
Access
// Configure To mode
catch( PDOException $e ) {
$conn = null;
If we open the PHP file using the URL http://localhost/mysql.php, it should show the output as shown in
Fig 4.
Fig 4
In this step, we will continue with our previous step and install MongoDB and MongoDB Express. Now,
update the docker-compose.yml as shown below.
# docker-compose.yml
version: "3.8"
services:
nginx:
container_name: nginx
build: ./docker/nginx
links:
- php
ports:
- "80:80"
volumes:
- ./src:/var/www/html
php:
container_name: php
build: ./docker/php
links:
- mysql
- mongo
ports:
- "9000:9000"
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
mysql:
image: mysql:8.0.27
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: '<root-password>'
MYSQL_DATABASE: helloworld
MYSQL_USER: helloworld
MYSQL_PASSWORD: '<db-password>'
ports:
- "3306:3306"
volumes:
- ./database/mysql:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: pma
https://devops.tutorials24x7.com/blog/containerize-php-with-nginx-mysql-and-mongodb-using-docker-containers 7/11
8/18/22, 4:47 PM Containerize PHP with NGINX, MySQL, and MongoDB using Docker Containers | Tutorials24x7
links:
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
- mysql
PMA_HOST: mysql
HOME Open
Future Proof PMA_PORT:
Your Lab 3306
ports:
- 8085:80
mongo:
image: mongo:5.0
container_name: mongo
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=password
restart: unless-stopped
ports:
- "27017:27017"
volumes:
- ./database/mongodb/db:/data/db
- ./database/mongodb/dev.archive:/Databases/dev.archive
- ./database/mongodb/production:/Databases/production
mongo-express:
image: mongo-express
container_name: mexpress
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=root
- ME_CONFIG_MONGODB_ADMINPASSWORD=password
- ME_CONFIG_MONGODB_URL=mongodb://root:password@mongo:27017/?authSource=admin
- ME_CONFIG_BASICAUTH_USERNAME=mexpress
- ME_CONFIG_BASICAUTH_PASSWORD=mexpress
links:
- mongo
restart: unless-stopped
ports:
- "8081:8081"
Replace the root password while configuring the docker-compose.yml. Also, install the PHP extensions to
access MongoDB from the PHP source files by updating the Dockerfile as shown below.
# docker/php/Dockerfile
FROM php:8.1
Now, run the build and up commands to again build the application and launch it. We can access
MongoDB using the URL - http://localhost:8081. It will ask for the basic authentication configured by us.
The home page should be similar as shown below.
Fig 5
Summary
This tutorial provided all the steps to containerize PHP with NGINX Web Server, MySQL, phpMyAdmin,
MongoDB, and MongoDB Express using Docker containers.
https://devops.tutorials24x7.com/blog/containerize-php-with-nginx-mysql-and-mongodb-using-docker-containers 8/11
8/18/22, 4:47 PM Containerize PHP with NGINX, MySQL, and MongoDB using Docker Containers | Tutorials24x7
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
Write a Comment
Affordable Access To Latest LC
Name Write here...
HOME Open
Future Proof Your Lab With LC Refresh. Flexible Financing,ABOUT
Personalized
US Training & Support
Website Link
Captcha Key*
Submit
ALSO ON TUTORIALS24X7
https://devops.tutorials24x7.com/blog/containerize-php-with-nginx-mysql-and-mongodb-using-docker-containers 9/11
8/18/22, 4:47 PM Containerize PHP with NGINX, MySQL, and MongoDB using Docker Containers | Tutorials24x7
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
Sponsored
Affordable Access
Talent shortage To Latest
hampers Vietnam’s LC
move up the value chain
HOME Open
FutureTheProof
Business Times
Your Lab With LC Refresh. Flexible Financing,ABOUT
Personalized
US Training & Support
The Hidden Secret for Making Men Look Slim Is Finally Out!
The Super Shaper
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
Testimonial
https://devops.tutorials24x7.com/blog/containerize-php-with-nginx-mysql-and-mongodb-using-docker-containers 10/11
8/18/22, 4:47 PM Containerize PHP with NGINX, MySQL, and MongoDB using Docker Containers | Tutorials24x7
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
https://devops.tutorials24x7.com/blog/containerize-php-with-nginx-mysql-and-mongodb-using-docker-containers 11/11