Containerize Laravel With Apache, MySQL, and MongoDB Using Docker Containers - Tutorials24x7
Containerize Laravel With Apache, MySQL, and MongoDB Using Docker Containers - Tutorials24x7
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
Recent Posts
- php
ports:
Build AI into SQL Learn More
- "80:80"
Container
volumes:
ize
https://devops.tutorials24x7.com/blog/containerize-laravel-with-apache-mysql-and-mongodb-using-docker-containers 1/12
8/18/22, 4:46 PM Containerize Laravel with Apache, MySQL, and MongoDB using Docker Containers | Tutorials24x7
HOME
- ./logs/apache:/var/log/apache2
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY MongoDB
- ./src/helloworld:/usr/local/apache2/htdocs/helloworld
and
php:
Mongo
container_name: php
Express
HOME
build: ./docker/php
ABOUT US using
ports:
TERMS & CONDITIONS
PRIVACY POLICY Docker
- "9000:9000"
Container
volumes:
s
- ./src/helloworld:/usr/local/apache2/htdocs/helloworld
working_dir: /usr/local/apache2/htdocs/helloworld
composer:
container_name: composer
Container
image: composer/composer
ize MySQL
volumes:
and
- ./src/helloworld:/usr/local/apache2/htdocs/helloworld
phpMyAd
working_dir: /usr/local/apache2/htdocs/helloworld
min using
command: install Docker
Container
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 apache. Create the directories <project path>/logs/apache to
store the logs.
# docker/php/Dockerfile
FROM php:8.1-fpm
Create the Dockerfile within the Apache directory as shown below. Build AI into SQL
MindsDB
# docker/apache/Dockerfile
FROM httpd:2.4.51
We also need to configure the Virtual Host to pass the PHP requests to PHP-FPM via port 9000. Now,
create the default configuration file as shown below.
# docker/apache/helloworld.apache.conf
<VirtualHost *:80>
DocumentRoot /usr/local/apache2/htdocs/helloworld/public
<Directory /usr/local/apache2/htdocs/helloworld>
DirectoryIndex index.php
AllowOverride All
</Directory>
</VirtualHost>
Now, update the Dockerfile within the Apache directory as shown below.
# docker/apache/Dockerfile
FROM httpd:2.4.51
>> /usr/local/apache2/conf/httpd.conf
After creating all the directories and files, the directory structure should be similar as shown below.
helloworld
https://devops.tutorials24x7.com/blog/containerize-laravel-with-apache-mysql-and-mongodb-using-docker-containers 2/12
8/18/22, 4:46 PM Containerize Laravel with Apache, MySQL, and MongoDB using Docker Containers | Tutorials24x7
-- php
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
-- Dockerfile
-- apache
-- Dockerfile
HOME
-- helloworld.apache.conf
ABOUT US
TERMS & CONDITIONS
-- src
PRIVACY POLICY
-- <laravel app directories and files>
-- logs
-- apache
-- docker-compose.yml
Next, we will install the Laravel project template files in the src directory using the command shown
below. It's required for the first time for the fresh installation of Laravel.
# Clear Directory
cd <path to project>/src
# Install Laravel
# Output
Updating dependencies
....
....
Publishing complete.
Now, run the command docker-compose build to build the images for PHP and Apache Web Server.
# Build
cd <path to project>
docker-compose build
# Output
Building php
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn
Building apache
https://devops.tutorials24x7.com/blog/containerize-laravel-with-apache-mysql-and-mongodb-using-docker-containers 3/12
8/18/22, 4:46 PM Containerize Laravel with Apache, MySQL, and MongoDB using Docker Containers | Tutorials24x7
HOME
=> CACHED [3/3] RUN echo "Include
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
/usr/local/apache2/conf/helloworld.apache.conf"
=> exporting to image
=> => exporting layers
HOME
=> => writing image sha256:163fe8f15637311df7bb05aab39101a1505723c105461d253539939d
=> => naming to docker.io/library/helloworld_apache ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
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. It should show Laravel's default, Welcome
Page, as shown in Fig 1.
Fig 1
In this step, we will continue with our previous step and install MySQL and phpMyAdmin. We will also
configure our laravel application to access the MySQL database. Now, update the docker-compose.yml
as shown below.
# docker-compose.yml
version: "3.8"
services:
apache:
container_name: apache
build: ./docker/apache
links:
- php
ports:
- "80:80"
volumes:
- ./logs/apache:/var/log/apache2
- ./src/helloworld:/usr/local/apache2/htdocs/helloworld
php:
container_name: php
build: ./docker/php
links:
- mysql
- "9000:9000"
Learn More
volumes:
- ./src/helloworld:/usr/local/apache2/htdocs/helloworld
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
composer:
container_name: composer
image: composer/composer
HOME
volumes:
ABOUT US
TERMS & CONDITIONS
- ./src/helloworld:/usr/local/apache2/htdocs/helloworld
PRIVACY POLICY
working_dir: /usr/local/apache2/htdocs/helloworld
command: install
mysql:
image: mysql:8.0.27
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: 'password'
MYSQL_DATABASE: helloworld
MYSQL_USER: helloworld
MYSQL_PASSWORD: 'password'
ports:
- "3306:3306"
volumes:
- ./database/mysql:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: pma
links:
- mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
ports:
- 8085:80
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-fpm
Now configure the Laravel application to use the MySQL database by updating the files as shown below.
You can also follow The Complete Guide To Perform CRUD Operations In Laravel Framework Using
MySQL.
# Laravel Configurations
cd <path to project>/src/helloworld
# Update .env
....
....
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=helloworld
DB_USERNAME=root
DB_PASSWORD= '<root-password>'
....
....
We can also create other users and databases using phpMyAdmin. Now, run the command docker-
compose build to build the application.
# Build
cd <path to project>
docker-compose build
# Output
https://devops.tutorials24x7.com/blog/containerize-laravel-with-apache-mysql-and-mongodb-using-docker-containers 5/12
8/18/22, 4:46 PM Containerize Laravel with Apache, MySQL, and MongoDB using Docker Containers | Tutorials24x7
Building php
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
....
....
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.
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.
https://devops.tutorials24x7.com/blog/containerize-laravel-with-apache-mysql-and-mongodb-using-docker-containers 6/12
8/18/22, 4:46 PM Containerize Laravel with Apache, MySQL, and MongoDB using Docker Containers | Tutorials24x7
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
Fig 3
Now create the database tables using the artisan command as shown below.
# CD to Project Path
cd <path to project>
# List Files
# Example
# Output
total 300
....
....
# Migration Command
# Example
# Output
Migrating: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrating: 2019_12_14_000001_create_personal_access_tokens_table
This completes the installation and configuration of MySQL and phpMyAdmin for the Laravel application.
# Press Ctrl + C
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.
Build AI into SQL Learn More
# docker-compose.yml
version: "3.8"
https://devops.tutorials24x7.com/blog/containerize-laravel-with-apache-mysql-and-mongodb-using-docker-containers 7/12
8/18/22, 4:46 PM Containerize Laravel with Apache, MySQL, and MongoDB using Docker Containers | Tutorials24x7
services:
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
apache:
container_name: apache
build: ./docker/apache
HOME
links:
ABOUT US
TERMS & CONDITIONS
- php
PRIVACY POLICY
ports:
- "80:80"
volumes:
- ./logs/apache:/var/log/apache2
- ./src/helloworld:/usr/local/apache2/htdocs/helloworld
php:
container_name: php
build: ./docker/php
links:
- mysql
- mongo
ports:
- "9000:9000"
volumes:
- ./src/helloworld:/usr/local/apache2/htdocs/helloworld
working_dir: /usr/local/apache2/htdocs/helloworld
composer:
container_name: composer
image: composer/composer
volumes:
- ./src/helloworld:/usr/local/apache2/htdocs/helloworld
working_dir: /usr/local/apache2/htdocs/helloworld
command: install
mysql:
image: mysql:8.0.27
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: 'password'
MYSQL_DATABASE: helloworld
MYSQL_USER: helloworld
MYSQL_PASSWORD: 'password'
ports:
- "3306:3306"
volumes:
- ./database/mysql:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: pma
links:
- mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
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
command: [--auth]
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
links:
Learn More
- mongo
https://devops.tutorials24x7.com/blog/containerize-laravel-with-apache-mysql-and-mongodb-using-docker-containers 8/12
8/18/22, 4:46 PM Containerize Laravel with Apache, MySQL, and MongoDB using Docker Containers | Tutorials24x7
restart: unless-stopped
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
ports:
- "8081:8081"
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY
Replace the root password while configuring the docker-compose.yml. POLICY
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-fpm
Also, configure the Laravel application to access the MongoDB database along with MySQL. You may
skip MySQL and configure MongoDB as the only server depending on your requirements. You can also
follow The Complete Guide To Perform CRUD Operations In Laravel Framework Using MongoDB.
# Laravel Configurations
cd <path to project>/src/helloworld
# Update .env
....
....
MONGO_CONNECTION=mongodb
MONGO_HOST=mongo
MONGO_PORT=27017
MONGO_AUTH_DATABASE=admin
MONGO_DATABASE=helloworld
MONGO_USERNAME=root
MONGO_PASSWORD= '<password>'
....
....
....
....
....
....
'connections' => [
....
....
'mongodb' => [
'options' => [
],
....
....
Now, configure the providers to use the MongoDB extension provided by Jens Segers by updating the
config/app.php file as shown below.
....
'providers' => [
https://devops.tutorials24x7.com/blog/containerize-laravel-with-apache-mysql-and-mongodb-using-docker-containers 9/12
8/18/22, 4:46 PM Containerize Laravel with Apache, MySQL, and MongoDB using Docker Containers | Tutorials24x7
p [
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
....
....
Jenssegers\Mongodb\MongodbServiceProvider::class
],
HOME
ABOUT US
....
TERMS & CONDITIONS
.... PRIVACY POLICY
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 4
This completes the installation and configuration of MongoDB for the Laravel application.
Summary
This tutorial provided all the steps to containerize Laravel with Apache Web Server, MySQL, phpMyAdmin,
MongoDB, and MongoDB Express using Docker containers.
Write a Comment
Website Link
Captcha Key*
Submit
ALSO ON TUTORIALS24X7
https://devops.tutorials24x7.com/blog/containerize-laravel-with-apache-mysql-and-mongodb-using-docker-containers 10/12
8/18/22, 4:46 PM Containerize Laravel with Apache, MySQL, and MongoDB using Docker Containers | Tutorials24x7
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
Sponsored
The Hidden Secret for Making Men Look Slim Is Finally Out!
The Super Shaper
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
Contact Us
Email *
Join Us
Feedback
Testimonial
https://devops.tutorials24x7.com/blog/containerize-laravel-with-apache-mysql-and-mongodb-using-docker-containers 11/12
8/18/22, 4:46 PM Containerize Laravel with Apache, MySQL, and MongoDB using Docker Containers | Tutorials24x7
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
HOME
ABOUT US
TERMS & CONDITIONS
PRIVACY POLICY
Copyright © 2017 - 2022 Tutorials24x7. All Rights Reserved.
https://devops.tutorials24x7.com/blog/containerize-laravel-with-apache-mysql-and-mongodb-using-docker-containers 12/12