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 dependencies
RUN apt-get update && apt-get install -y \
    libzip-dev \
    zip \
    nginx \
    curl

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql zip
RUN pecl install redis && docker-php-ext-enable redis

# Configure Nginx
COPY .docker/nginx/sites-enabled/nginx.conf /etc/nginx/conf.d/default.conf
RUN rm /etc/nginx/sites-enabled/default

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Copy application
COPY . /var/www/html

# Set working directory
WORKDIR /var/www/html

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

# Set permissions
RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache

# Create entrypoint script
COPY <<'EOF' /usr/local/bin/entrypoint.sh
#!/bin/bash
php artisan config:cache
php artisan route:cache
php artisan optimize
php-fpm -D && nginx -g "daemon off;"
EOF

RUN chmod +x /usr/local/bin/entrypoint.sh

EXPOSE 80

ENTRYPOINT ["/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-network

volumes:
  db-data:
  redis-data:

networks:
  laravel-network:
    driver: bridge

5. Running the Application

To start your Laravel application:

# Build the images
docker-compose build

# Start the containers
docker-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=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel_user
DB_PASSWORD=secret

REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_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.

Subscribe to CTmakes

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe