Laravel Docker #7 - Connect Laravel with MongoDB Database
Sometimes, you may want to use MongoDB as your database for a Laravel project. Docker makes it simple to set up Laravel with MongoDB in a clean and easy way. In this tutorial, we will learn how to connect Laravel with MongoDB using Docker.
This guide is explained step by step with simple words. Even if you are new to Docker or MongoDB, you can follow along and run Laravel with MongoDB support successfully.
Step 1: Create Laravel Project
First, create a fresh Laravel project by running the command below:
laravel new my-app
Step 2: Create Dockerfile
Now, create a Dockerfile in your project root. This will install PHP, Composer, and enable the MongoDB extension for PHP.
Dockerfile
FROM php:8.3-fpm
WORKDIR /var/www/html
RUN apt-get update & apt-get install -y libzip-dev unzip pkg-config && pecl install mongodb && docker-php-ext-enable mongodb && docker-php-ext-install zip pdo pdo_pgsql
# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY . /var/www/html
# Install PHP dependencies
RUN composer install
RUN chown -R www-data:www-data /var/www/html
RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache /var/www/html/vendor
RUN chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache /var/www/html/vendor
EXPOSE 80
Step 3: Create NGINX Config File
Next, create the NGINX configuration file to handle Laravel requests properly.
.docker/apache/default.conf
server {
listen 80;
index index.php index.html;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass web:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}
location ~ /\.ht {
deny all;
}
}
Step 4: Create docker-compose.yml
Now, create the docker-compose.yml file. This will define Laravel, Nginx, and MongoDB services.
docker-compose.yml
services:
web:
build:
context: .
dockerfile: Dockerfile
image: my-laravel-app
volumes:
- .:/var/www/html
- /var/www/html/storage
- /var/www/html/bootstrap/cache
- /var/www/html/vendor
working_dir: /var/www/html
user: "www-data"
depends_on:
- db
nginx:
image: nginx:latest
ports:
- "8080:80"
volumes:
- .:/var/www/html
- ./.docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- web
db:
image: mongo:6.0
container_name: mongodb
command: ["--auth", "--setParameter", "authenticationMechanisms=SCRAM-SHA-1"]
environment:
MONGO_INITDB_ROOT_USER: root
MONGO_INITDB_ROOT_PASSWORD: secret
MONGO_INITDB_DATABASE: laravel
ports:
- "27017:27017"
volumes:
- dbdata:/var/db
volumes:
dbdata:
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:8080
Now you can use.