Docker Compose for Laravel Projects: Simplified Setup Guide

Docker Compose has revolutionized the way developers set up and manage their development environments. For Laravel projects, using Docker Compose simplifies the process, ensuring consistency across different setups and reducing configuration time. This guide provides a straightforward approach to setting up Docker Compose for your Laravel projects.

Why Use Docker Compose with Laravel?

Laravel is a popular PHP framework that benefits from a consistent environment. Docker Compose allows you to define all necessary services—such as PHP, MySQL, and Nginx—in a single configuration file. This setup ensures that your development environment mirrors production, reduces “it works on my machine” issues, and facilitates easy onboarding for new team members.

Prerequisites

  • Docker installed on your machine
  • Docker Compose installed
  • Basic knowledge of Laravel and Docker

Basic Docker Compose File for Laravel

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

version: '3.8'

services:
  app:
    image: laravelphp/php-fpm
    container_name: laravel_app
    restart: unless-stopped
    working_dir: /var/www
    volumes:
      - ./:/var/www
    networks:
      - laravel_network

  web:
    image: nginx:alpine
    container_name: laravel_web
    restart: unless-stopped
    ports:
      - "8000:80"
    volumes:
      - ./:/var/www
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - app
    networks:
      - laravel_network

  db:
    image: mysql:5.7
    container_name: laravel_db
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: laravel
      MYSQL_USER: laraveluser
      MYSQL_PASSWORD: laravelpassword
    ports:
      - "3306:3306"
    volumes:
      - dbdata:/var/lib/mysql
    networks:
      - laravel_network

networks:
  laravel_network:
    driver: bridge

volumes:
  dbdata:

Configuring Nginx

Create an nginx.conf file in your project root with the following configuration:

server {
    listen 80;
    server_name localhost;
    root /var/www/public;

    index index.php index.html;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass app:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

Starting Your Containers

Run the following command in your project directory:

docker-compose up -d

Accessing Your Laravel Project

Open your browser and navigate to http://localhost:8000. You should see your Laravel application’s welcome page. Your environment is now set up with PHP, MySQL, and Nginx, all managed via Docker Compose.

Additional Tips

  • Use docker-compose down to stop and remove containers.
  • Use docker-compose logs to view logs for debugging.
  • Customize environment variables in docker-compose.yml as needed.

By leveraging Docker Compose, Laravel developers can streamline their development workflow, ensuring consistency and ease of setup across different environments. Happy coding!