Laravel Docker Build (beginner friendly)

Docker Compose provides an excellent way to containerize Laravel applications for both development and production environments. This guide will walk you through creating a complete Docker setup for Laravel applications.

1. Project Structure

First, create the following structure in your Laravel project:

your-laravel-project/├── .docker/│   └── nginx/│       ├── nginx.conf│       └── sites-enabled/│           └── nginx.conf├── Dockerfile└── docker-compose.yml

2. Creating the Nginx Configuration

Create the Nginx configuration file in .docker/nginx/sites-enabled/nginx.conf:

server {    listen 80;    server_name localhost;    root /var/www/html/public;    add_header X-Frame-Options "SAMEORIGIN";    add_header X-Content-Type-Options "nosniff";    add_header X-XSS-Protection "1; mode=block";    index index.php;    charset utf-8;    location / {        try_files $uri $uri/ /index.php?$query_string;    }    location = /favicon.ico { access_log off; log_not_found off; }    location = /robots.txt  { access_log off; log_not_found off; }    error_page 404 /index.php;    location ~ \.php$ {        fastcgi_pass 127.0.0.1:9000;        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;        include fastcgi_params;        fastcgi_index index.php;        fastcgi_buffers 16 16k;        fastcgi_buffer_size 32k;        fastcgi_read_timeout 600;    }    location ~ /\.(?!well-known).* {        deny all;    }}

3. Creating the Dockerfile

Create a Dockerfile in your project root:

FROM php:8.2-fpm# Install system dependenciesRUN apt-get update && apt-get install -y \    libzip-dev \    zip \    nginx \    curl# Clear cacheRUN apt-get clean && rm -rf /var/lib/apt/lists/*# Install PHP extensionsRUN docker-php-ext-install pdo_mysql zipRUN pecl install redis && docker-php-ext-enable redis# Configure NginxCOPY .docker/nginx/sites-enabled/nginx.conf /etc/nginx/conf.d/default.confRUN rm /etc/nginx/sites-enabled/default# Install composerRUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer# Copy applicationCOPY . /var/www/html# Set working directoryWORKDIR /var/www/html# Install dependenciesRUN composer install --no-dev --optimize-autoloader# Set permissionsRUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache# Create entrypoint scriptCOPY <<'EOF' /usr/local/bin/entrypoint.sh#!/bin/bashphp artisan config:cachephp artisan route:cachephp artisan optimizephp-fpm -D && nginx -g "daemon off;"EOFRUN chmod +x /usr/local/bin/entrypoint.shEXPOSE 80ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

4. Creating Docker Compose Configuration

Create a docker-compose.yml file:

services:  app:    build:      context: .    ports:      - "8080:80"    depends_on:      - db      - redis    environment:      DB_HOST: db      DB_PORT: 3306      DB_DATABASE: laravel      DB_USERNAME: laravel_user      DB_PASSWORD: secret      REDIS_HOST: redis      REDIS_PORT: 6379    networks:      - laravel-network  db:    image: mysql:8.0    environment:      MYSQL_DATABASE: laravel      MYSQL_USER: laravel_user      MYSQL_PASSWORD: secret      MYSQL_ROOT_PASSWORD: secret    volumes:      - db-data:/var/lib/mysql    networks:      - laravel-network  redis:    image: redis:alpine    volumes:      - redis-data:/data    networks:      - laravel-networkvolumes:  db-data:  redis-data:networks:  laravel-network:    driver: bridge

5. Running the Application

To start your Laravel application:

# Build the imagesdocker-compose build# Start the containersdocker-compose up -d

6. Additional Configuration Tips

Environment Variables

Create a .env file in your Laravel project and make sure to update the database and Redis connection details to match the Docker service names:

DB_CONNECTION=mysqlDB_HOST=dbDB_PORT=3306DB_DATABASE=laravelDB_USERNAME=laravel_userDB_PASSWORD=secretREDIS_HOST=redisREDIS_PASSWORD=nullREDIS_PORT=6379

Development vs Production

For development environments, you might want to add volumes to enable hot reloading:

services:  app:    volumes:      - .:/var/www/html      - /var/www/html/vendor      - /var/www/html/node_modules

7. Final Steps

After deployment, you can access your Laravel application at http://localhost:8080. The setup includes:

  • PHP-FPM with Nginx
  • MySQL database
  • Redis for caching
  • Automated container orchestration
  • Proper networking between services
  • Volume persistence for database and cache

This Docker Compose setup provides a solid foundation for Laravel deployments, whether in development or production environments. Remember to adjust configurations according to your specific needs and security requirements.