diff --git a/Dockerfile b/Dockerfile index 0d834ed..c462661 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,58 +1,68 @@ -FROM php:8.4-fpm-alpine +# 1. Use the specific PHP 8.4 Alpine image +FROM webdevops/php-nginx:8.4-alpine -# Install extension installer +# 2. Environment Configuration +ENV WEB_DOCUMENT_ROOT=/code/public +ENV APP_ENV=production +ENV COMPOSER_ALLOW_SUPERUSER=1 +ENV COMPOSER_PROCESS_TIMEOUT=2000 + +# 3. Install System Dependencies & Extensions COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/ -# Install system dependencies RUN apk add --no-cache \ - git \ - curl \ - zip \ - unzip \ - nginx \ - supervisor \ nodejs \ npm \ - bash + sqlite \ + shadow \ + && install-php-extensions pdo_sqlite mongodb redis imap pdo_mysql -# Install PHP extensions -RUN install-php-extensions pdo_mysql mbstring exif pcntl bcmath gd intl imap redis zip +# 4. Copy Custom Configs +COPY ./dockerizer/php.ini /opt/docker/etc/php/php.ini +COPY ./dockerizer/vhost.conf /opt/docker/etc/nginx/vhost.conf +COPY ./dockerizer/supervisor.laravel.conf /opt/docker/etc/supervisor.d/laravel.conf -# Install Composer -COPY --from=composer:latest /usr/bin/composer /usr/bin/composer +# 5. Set Working Directory +WORKDIR /code -# Set working directory -WORKDIR /var/www +# CRITICAL FIX: Transfer ownership of /code to the 'application' user +# WORKDIR creates folders as 'root' by default, which blocks Composer later. +RUN chown application:application /code -# Copy existing application directory contents -COPY . /var/www +# 6. Install PHP Dependencies (Layered) +COPY --chown=application:application composer.json composer.lock ./ -# Install PHP/Node dependencies and build frontend -RUN composer install --no-interaction --no-dev --optimize-autoloader \ - && npm install \ - && npm run build \ - && npm cache clean --force +USER application +RUN composer install --no-interaction --no-scripts --no-autoloader --prefer-dist --no-dev -# Nginx config -# Alpine uses http.d instead of sites-available -COPY .docker/nginx.conf /etc/nginx/http.d/default.conf +# 7. Install Node Dependencies +COPY --chown=application:application package.json package-lock.json* ./ +RUN npm install -# Supervisor config -RUN mkdir -p /etc/supervisor/conf.d /var/log/supervisor -COPY .docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf +# 8. Copy Application Code +COPY --chown=application:application . . -# Set directory permissions -RUN mkdir -p /run/nginx /var/lib/nginx \ - && chown -R www-data:www-data /var/www \ - && chown -R www-data:www-data /var/lib/nginx \ - && chown -R www-data:www-data /run/nginx \ - && chmod -R 775 /var/www/storage /var/www/bootstrap/cache +# 9. Final Builds +RUN composer dump-autoload --optimize && \ + composer run-script post-root-package-install && \ + php artisan package:discover --ansi -EXPOSE 80 +RUN npm run build -# Configure entrypoint -COPY .docker/entrypoint.sh /usr/local/bin/entrypoint.sh -RUN chmod +x /usr/local/bin/entrypoint.sh +# 10. Runtime Initialization Script +# This runs when the container STARTS, not when it builds. +USER root -# Start Supervisor -CMD ["/usr/local/bin/entrypoint.sh"] +RUN echo '#!/bin/bash' > /opt/docker/provision/entrypoint.d/99-init-laravel.sh && \ + echo 'set -e' >> /opt/docker/provision/entrypoint.d/99-init-laravel.sh && \ + # 1. Fix Permissions \ + echo 'echo "Fixing storage permissions..."' >> /opt/docker/provision/entrypoint.d/99-init-laravel.sh && \ + echo 'mkdir -p /code/storage/framework/{views,cache,sessions} /code/bootstrap/cache' >> /opt/docker/provision/entrypoint.d/99-init-laravel.sh && \ + echo 'chown -R application:application /code/storage /code/bootstrap/cache' >> /opt/docker/provision/entrypoint.d/99-init-laravel.sh && \ + echo 'chmod -R 775 /code/storage /code/bootstrap/cache' >> /opt/docker/provision/entrypoint.d/99-init-laravel.sh && \ + # 2. Run Optimization (As the application user) \ + echo 'echo "Running Laravel Optimizations..."' >> /opt/docker/provision/entrypoint.d/99-init-laravel.sh && \ + echo 'su -s /bin/sh application -c "php artisan optimize"' >> /opt/docker/provision/entrypoint.d/99-init-laravel.sh && \ + # 3. Cache Views (Optional but recommended for production) \ + echo 'su -s /bin/sh application -c "php artisan view:cache"' >> /opt/docker/provision/entrypoint.d/99-init-laravel.sh && \ + chmod +x /opt/docker/provision/entrypoint.d/99-init-laravel.sh diff --git a/dockerizer/php.ini b/dockerizer/php.ini new file mode 100644 index 0000000..a3b34d1 --- /dev/null +++ b/dockerizer/php.ini @@ -0,0 +1,6 @@ +date.time = UTC +display_errors = Off +memory_limit = 128M +max_execution_time = 60 +post_max_size = 32M +upload_max_filesize = 16M diff --git a/dockerizer/supervisor.laravel.conf b/dockerizer/supervisor.laravel.conf new file mode 100644 index 0000000..d166c16 --- /dev/null +++ b/dockerizer/supervisor.laravel.conf @@ -0,0 +1,25 @@ +[program:laravel-worker] +process_name=%(program_name)s_%(process_num)02d +command=php /code/artisan queue:work --sleep=3 --tries=3 --max-time=3600 +autostart=true +autorestart=true +stopasgroup=true +killasgroup=true +user=application +numprocs=1 +redirect_stderr=true +stdout_logfile=/docker.stdout +stdout_logfile_maxbytes=0 + +[program:laravel-scheduler] +process_name=%(program_name)s_%(process_num)02d +command=php /code/artisan schedule:work +autostart=true +autorestart=true +stopasgroup=true +killasgroup=true +user=application +numprocs=1 +redirect_stderr=true +stdout_logfile=/docker.stdout +stdout_logfile_maxbytes=0 diff --git a/dockerizer/vhost.conf b/dockerizer/vhost.conf new file mode 100644 index 0000000..5de2f13 --- /dev/null +++ b/dockerizer/vhost.conf @@ -0,0 +1,25 @@ +server { + listen 80 default_server; + + server_name _; + + root "/code/public"; + index index.php; + + client_max_body_size 50m; + + access_log /docker.stdout; + error_log /docker.stderr warn; + + location / { + try_files $uri $uri/ /index.php?$query_string; + } + + location ~ \.php$ { + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass php; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $request_filename; + fastcgi_read_timeout 600; + } +}