To deploy a Laravel application using Docker, while maintaining the development environment benefits of Laravel Sail, you can follow these steps to create a production-ready Docker setup:
-
Create a Production Dockerfile: Start by creating a
Dockerfilespecifically for production. This file will be different from the one used by Sail, as it will be optimized for production use.FROM php:8.1-fpm # Set working directory WORKDIR /var/www # Install system dependencies RUN apt-get update && apt-get install -y \ git \ curl \ libpng-dev \ libjpeg-dev \ libfreetype6-dev \ zip \ unzip \ && docker-php-ext-configure gd --with-freetype --with-jpeg \ && docker-php-ext-install gd pdo pdo_mysql # Install Composer COPY --from=composer:2 /usr/bin/composer /usr/bin/composer # Copy application files COPY . . # Install application dependencies RUN composer install --optimize-autoloader --no-dev # Set permissions 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"] -
Create a Docker Compose File for Production: Create a
docker-compose.prod.ymlfile to define your production services.version: '3.8' services: app: build: context: . dockerfile: Dockerfile image: your-app-image container_name: your-app-container restart: unless-stopped environment: APP_ENV: production APP_DEBUG: false APP_KEY: your-app-key volumes: - .:/var/www networks: - app-network webserver: image: nginx:alpine container_name: your-webserver-container restart: unless-stopped ports: - "80:80" volumes: - .:/var/www - ./nginx.conf:/etc/nginx/conf.d/default.conf networks: - app-network db: image: mysql:5.7 container_name: your-db-container restart: unless-stopped environment: MYSQL_DATABASE: your-database MYSQL_ROOT_PASSWORD: your-root-password volumes: - dbdata:/var/lib/mysql networks: - app-network networks: app-network: driver: bridge volumes: dbdata: -
Create an Nginx Configuration File: Create an
nginx.conffile to configure Nginx for your Laravel application.server { listen 80; index index.php index.html; server_name your-domain.com; root /var/www/public; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include fastcgi_params; fastcgi_pass app:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; } location ~ /\.ht { deny all; } } -
Build and Deploy: Use the following commands to build and deploy your application:
docker-compose -f docker-compose.prod.yml up --build -d -
Optimize Laravel for Production: Ensure your Laravel application is optimized for production by running:
php artisan config:cache php artisan route:cache php artisan view:cache
By following these steps, you can create a workflow that allows you to develop with Laravel Sail and then deploy a production-ready Docker setup. This setup should help you achieve better performance and avoid the permission issues you encountered.