Deploy Laravel with, MySQL, and Nginx using Docker Compose

Docker Compose Tutorial for Laravel, MySQL, and Nginx

This tutorial will guide you through setting up a Laravel application with MySQL as the database and Nginx as the web server using Docker Compose. We will create a single docker-compose.yml file that includes all the necessary services and configurations, allowing you to quickly deploy your Laravel application in a containerized environment.

Docker Compose File

Create a docker-compose.yml file in the root directory of your Laravel project with the following content:

version: '3.8'

services:
  # Laravel App
  app:
    image: laravel:latest
    container_name: laravel_app
    build:
      context: .
      dockerfile: Dockerfile
    working_dir: /var/www/html
    volumes:
      - ./:/var/www/html
      - ./php.ini:/usr/local/etc/php/conf.d/php.ini
    environment:
      - DB_HOST=db
      - DB_PORT=3306
      - DB_DATABASE=laravel
      - DB_USERNAME=root
      - DB_PASSWORD=root
    networks:
      - laravel_network

  # MySQL Database
  db:
    image: mysql:8.0
    container_name: mysql_db
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: laravel
      MYSQL_USER: root
      MYSQL_PASSWORD: root
    volumes:
      - dbdata:/var/lib/mysql
    networks:
      - laravel_network

  # Nginx Server
  web:
    image: nginx:alpine
    container_name: nginx_web
    volumes:
      - ./:/var/www/html
      - ./nginx.conf:/etc/nginx/nginx.conf
    ports:
      - "80:80"
    depends_on:
      - app
    networks:
      - laravel_network

volumes:
  dbdata:

networks:
  laravel_network:
    driver: bridge

Configuration Files

Dockerfile: Create a Dockerfile in the root directory of your Laravel project with the following content:

FROM php:8.1-fpm

RUN apt-get update && apt-get install -y \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install gd \
    && docker-php-ext-install pdo_mysql

WORKDIR /var/www/html

COPY . .

RUN composer install

nginx.conf: Create an nginx.conf file with the following configuration:

server {
    listen 80;
    index index.php index.html;
    server_name localhost;
    root /var/www/html/public;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /\.ht {
        deny all;
    }
}

php.ini (optional): If you need custom PHP configurations, you can add a php.ini file to the root directory and map it in the docker-compose.yml.

Running the Application

Access Laravel: Open your browser and navigate to http://localhost. Your Laravel application should be running with MySQL and Nginx.

Stopping Containers:

docker-compose down

Build and Run Containers:

docker-compose up -d --build

This setup allows you to quickly deploy a Laravel application with MySQL and Nginx using Docker Compose, all in one file.








Open-source Apps

9,500+

Medical Apps

500+

Lists

450+

Dev. Resources

900+

Read more