|
1 |
| -# Stage 1: Compile Frontend Assets |
2 |
| -FROM node:20-alpine AS node_builder |
3 |
| - |
4 |
| -WORKDIR /app/frontend |
5 |
| - |
6 |
| -# Copy package files and essential build configuration |
7 |
| -COPY package.json package-lock.json* webpack.mix.js tailwind.config.js postcss.config.js* .babelrc* ./ |
8 |
| -# Ensure postcss.config.js and .babelrc are optional by using * in case they don't exist |
9 |
| - |
10 |
| -# Install all dependencies (including devDependencies like laravel-mix) |
11 |
| -RUN npm ci |
12 |
| - |
13 |
| -# Copy frontend source code (js, css, images, etc.) |
14 |
| -COPY resources/js ./resources/js |
15 |
| -COPY resources/css ./resources/css |
16 |
| -COPY resources/img ./resources/img |
17 |
| -# Add other relevant resource directories if needed |
18 |
| - |
19 |
| -# Compile assets for production |
20 |
| -RUN npm run production |
21 |
| - |
22 |
| -# Stage 2: Setup PHP Application Environment using richarvey/nginx-php-fpm |
23 |
| -FROM richarvey/nginx-php-fpm:3.1.6 AS app |
24 |
| - |
25 |
| -# Set Laravel Environment Variables as per the article |
26 |
| -ENV APP_ENV production |
27 |
| -ENV APP_DEBUG false |
28 |
| -ENV LOG_CHANNEL stderr |
29 |
| -ENV APP_KEY ${APP_KEY} |
30 |
| -ENV SKIP_COMPOSER 1 |
31 |
| -ENV WEBROOT /var/www/html/public |
32 |
| -ENV PHP_ERRORS_STDERR 1 # Send PHP errors to stderr for Docker logging |
33 |
| -ENV RUN_SCRIPTS 1 # Enable execution of scripts in /scripts directory |
34 |
| -ENV REAL_IP_HEADER 1 # If behind a proxy, trust X-Forwarded-For |
35 |
| -ENV COMPOSER_ALLOW_SUPERUSER 1 # Allow composer to run as root if needed by scripts |
36 |
| - |
37 |
| -# Set working directory |
| 1 | +# Stage 1: Base with PHP, Composer, and common extensions |
| 2 | +# We'll use an official PHP image as a base for PHP-FPM |
| 3 | +FROM php:8.2-fpm-alpine AS php_base |
| 4 | + |
| 5 | +# Install system dependencies for PHP extensions and other tools |
| 6 | +RUN apk add --no-cache \ |
| 7 | + bash \ |
| 8 | + curl \ |
| 9 | + git \ |
| 10 | + supervisor \ |
| 11 | + nginx \ |
| 12 | + # For common PHP extensions |
| 13 | + libzip-dev \ |
| 14 | + libpng-dev \ |
| 15 | + libjpeg-turbo-dev \ |
| 16 | + freetype-dev \ |
| 17 | + icu-dev \ |
| 18 | + libxml2-dev \ |
| 19 | + # For pdo_mysql |
| 20 | + mysql-client |
| 21 | + |
| 22 | +# Install PHP extensions |
| 23 | +# You can customize this list based on Laradock's PHP-FPM Dockerfile or your app's needs |
| 24 | +RUN docker-php-ext-configure gd --with-freetype --with-jpeg \ |
| 25 | + && docker-php-ext-install -j$(nproc) \ |
| 26 | + gd \ |
| 27 | + intl \ |
| 28 | + opcache \ |
| 29 | + pdo_mysql \ |
| 30 | + zip \ |
| 31 | + xml \ |
| 32 | + bcmath \ |
| 33 | + pcntl \ |
| 34 | + exif \ |
| 35 | + sockets |
| 36 | + |
| 37 | +# Get latest Composer |
| 38 | +COPY --from=composer:latest /usr/bin/composer /usr/bin/composer |
| 39 | + |
| 40 | +# Set working directory for the application |
38 | 41 | WORKDIR /var/www/html
|
39 | 42 |
|
40 |
| -# Copy all application files |
| 43 | +# Copy application code |
41 | 44 | COPY . .
|
42 | 45 |
|
43 |
| -# Copy compiled assets from the node_builder stage |
44 |
| -COPY --from=node_builder /app/frontend/public ./public |
| 46 | +# Install Composer dependencies |
| 47 | +RUN composer install --no-dev --no-interaction --no-progress --optimize-autoloader |
| 48 | + |
| 49 | +# Optimize Laravel (optional here, can also be done at runtime start, but better in image) |
| 50 | +RUN php artisan config:cache |
| 51 | +RUN php artisan route:cache |
| 52 | +# RUN php artisan view:cache # view:cache can sometimes be problematic if paths change |
| 53 | + |
| 54 | +# Fix permissions for storage and bootstrap cache |
| 55 | +RUN chown -R www-data:www-data storage bootstrap/cache public \ |
| 56 | + && chmod -R 775 storage bootstrap/cache public |
| 57 | + |
| 58 | +# Stage 2: Final image with Nginx and Supervisor |
| 59 | +FROM alpine:latest |
45 | 60 |
|
46 |
| -# Copy our Nginx site configuration to the standard location for richarvey images |
47 |
| -COPY nginx-site.conf /etc/nginx/sites-available/default |
| 61 | +# Copy PHP-FPM and application from the php_base stage |
| 62 | +COPY --from=php_base /usr/local/etc/php-fpm.d/ /usr/local/etc/php-fpm.d/ |
| 63 | +COPY --from=php_base /usr/local/etc/php/ /usr/local/etc/php/ |
| 64 | +COPY --from=php_base /usr/local/sbin/php-fpm /usr/local/sbin/php-fpm |
| 65 | +COPY --from=php_base /usr/local/bin/php /usr/local/bin/php |
| 66 | +COPY --from=php_base /usr/bin/composer /usr/bin/composer |
| 67 | +COPY --from=php_base /var/www/html /var/www/html |
| 68 | +COPY --from=php_base /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ |
| 69 | + |
| 70 | +# Install Nginx and Supervisor from Alpine packages (if not already in php_base or if we want a clean stage) |
| 71 | +RUN apk add --no-cache nginx supervisor mysql-client bash |
| 72 | + |
| 73 | +# Create Nginx log directory if it doesn't exist from package install |
| 74 | +RUN mkdir -p /var/log/nginx && \ |
| 75 | + chown -R nginx:nginx /var/log/nginx && \ |
| 76 | + mkdir -p /var/lib/nginx && \ |
| 77 | + chown -R nginx:nginx /var/lib/nginx && \ |
| 78 | + # Create run directory for php-fpm and nginx pid |
| 79 | + mkdir -p /run/php && \ |
| 80 | + mkdir -p /run/nginx |
| 81 | + |
| 82 | +# Copy Nginx configuration |
| 83 | +# We will create these files next: nginx.conf and supervisord.conf |
| 84 | +COPY nginx-prod.conf /etc/nginx/nginx.conf |
| 85 | +COPY laravel-site.conf /etc/nginx/http.d/default.conf |
| 86 | + |
| 87 | +# Copy Supervisor configuration |
| 88 | +COPY supervisord.conf /etc/supervisord.conf |
| 89 | + |
| 90 | +# Application root |
| 91 | +WORKDIR /var/www/html |
48 | 92 |
|
49 |
| -# Copy our deploy script to the location where richarvey image expects to run scripts |
50 |
| -# Ensure deploy.sh has necessary commands (composer install, migrations, cache) |
51 |
| -RUN mkdir -p /scripts |
52 |
| -COPY deploy.sh /scripts/00-laravel-deploy.sh |
53 |
| -# Install dos2unix, convert line endings, and ensure the script is executable |
54 |
| -RUN apk add --no-cache dos2unix && \ |
55 |
| - dos2unix /scripts/00-laravel-deploy.sh && \ |
56 |
| - chmod +x /scripts/00-laravel-deploy.sh |
| 93 | +# Expose port 80 for Nginx |
| 94 | +EXPOSE 80 |
57 | 95 |
|
58 |
| -# The base image (richarvey/nginx-php-fpm) handles starting Nginx, PHP-FPM, |
59 |
| -# and running scripts from /scripts. Its default CMD is /start.sh. |
60 |
| -CMD ["/start.sh"] |
| 96 | +# Start Supervisor |
| 97 | +CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"] |
0 commit comments