Deploying a Laravel App with Docker and MySQL Using Docker Compose, Step by Step Guide

Deploying a Laravel App with Docker and MySQL Using Docker Compose, Step by Step Guide

Laravel, a powerful PHP framework, is widely recognized for its elegant syntax, robust features, and ease of use, making it a preferred choice for web application development among freelance developers and enterprise teams alike. Its features like routing, ORM, and task scheduling, combined with a large ecosystem of tools, streamline the development process, enabling developers to build scalable and maintainable applications efficiently.

Docker is a platform that simplifies the process of developing, shipping, and running applications by using containers. Containers package an application and its dependencies together, ensuring consistency across different environments, from development to production. This guarantees that your Laravel application behaves the same regardless of where it's run, eliminating the common "it works on my machine" problem.

Docker Compose is a tool specifically designed for defining and running multi-container Docker applications. With Docker Compose, you can manage your entire stack using a simple YAML file, describing the services, networks, and volumes your app requires. This makes managing complex environments involving databases, caching layers, and other services straightforward.

Benefits of Using Docker to Deploy Your Laravel App:

  1. Consistency Across Environments: Docker ensures that the environment remains consistent across all stages—development, testing, staging, and production. This consistency reduces bugs related to environment differences, allowing for smoother transitions between different phases of the development cycle.
  2. Simplified Setup and Deployment: Docker Compose allows you to define and configure your entire development environment, including the Laravel app, MySQL database, and Nginx server, in a single file. This simplifies setup and deployment, making it easier for teams to get up and running quickly, especially in larger projects where multiple services are involved.
  3. Isolation and Security: Docker containers isolate the Laravel application from other services running on the same host, minimizing the risk of conflicts and enhancing security. Each service runs in its own container with defined resource limits, reducing the impact of potential vulnerabilities.
  4. Scalability: Docker makes it easy to scale your Laravel application by running multiple instances of containers and load balancing them. This is particularly beneficial for enterprise teams that need to handle high traffic and ensure high availability.
  5. Portability: Docker containers can run on any system that supports Docker, whether it’s a developer’s local machine, an on-premise server, or a cloud platform. This portability enables seamless transitions between different environments, making it ideal for freelance developers working on diverse projects.

Deploy Laravel App with MySQL with Docker and Docker Compose

This tutorial walks you through deploying a Laravel application using Docker, MySQL, and Docker Compose.

By the end, you'll have a working Laravel app running in a Dockerized environment.

Prerequisites

  • Docker installed on your system
  • Docker Compose installed
  • A Laravel project (you can create one using laravel new my-app)

1: Create a Dockerfile

Create a Dockerfile in the root directory of your Laravel project. This file will define the environment for your Laravel application.

# Use the official PHP image as the base
FROM php:8.2-fpm

# Set working directory
WORKDIR /var/www

# Install system dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl

# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Copy existing application directory contents
COPY . .

# Install dependencies
RUN composer install --optimize-autoloader --no-dev

# Set permissions for Laravel
RUN chown -R www-data:www-data /var/www \
    && chmod -R 755 /var/www/storage

# Expose port 9000 and start PHP-FPM server
EXPOSE 9000
CMD ["php-fpm"]

2: Create a docker-compose.yml File

Next, create a docker-compose.yml file in the root directory. This file will define the services (containers) that make up your application.

version: '3.8'

services:
  # The application container
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: laravel-app
    container_name: laravel-app
    restart: unless-stopped
    working_dir: /var/www
    volumes:
      - .:/var/www
      - ./vendor:/var/www/vendor
      - ./storage:/var/www/storage
    networks:
      - laravel

  # The MySQL container
  mysql:
    image: mysql:8.0
    container_name: mysql
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_DATABASE: laravel
      MYSQL_USER: laravel_user
      MYSQL_PASSWORD: laravel_password
    volumes:
      - mysql_data:/var/lib/mysql
    networks:
      - laravel

  # The Nginx container
  nginx:
    image: nginx:latest
    container_name: nginx
    restart: unless-stopped
    ports:
      - "8080:80"
    volumes:
      - .:/var/www
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    networks:
      - laravel

networks:
  laravel:
    driver: bridge

volumes:
  mysql_data:
    driver: local

3: Create an Nginx Configuration File

In the root directory of your project, create a directory named nginx and inside it, create a default.conf file with the following content:

server {
    listen 80;
    index index.php index.html;
    server_name localhost;

    root /var/www/public;

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

    location ~ \.php$ {
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

4: Update the Laravel .env File

In your Laravel .env file, update the following database configuration to match the settings in your docker-compose.yml file:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel_user
DB_PASSWORD=laravel_password

5: Build and Run Your Containers

Now, you can build and run your Docker containers using Docker Compose:

docker-compose up -d --build

This command will build the images and start the containers in detached mode.

6: Access Your Laravel Application

Once the containers are running, you can access your Laravel application by navigating to http://localhost:8080 in your web browser.

Final Snippet

Here's the final command to bring everything up:

docker-compose up -d --build

This command will build your Docker environment and start your Laravel application, which you can access via http://localhost:8080.

Conclusion

You have successfully deployed a Laravel application with Docker and Docker Compose. This setup is ideal for local development and can be adapted for production environments with further customization.

Deploying Laravel with Docker and Docker Compose offers a consistent, secure, and scalable environment that simplifies setup and deployment, making it a powerful solution for freelance developers and enterprise teams alike.








Open-source Apps

9,500+

Medical Apps

500+

Lists

450+

Dev. Resources

900+

Read more