1- # Multi-stage build for production
2- FROM composer:2.6 AS composer
1+ # Multi-service app image: Nginx + PHP-FPM + app code
32
4- # Copy composer files
5- COPY composer.json composer.lock ./
3+ FROM composer:2 AS vendor
4+ WORKDIR /app
65
7- # Install dependencies
8- RUN composer install --no-dev --optimize-autoloader --no-scripts
6+ # Only copy composer files first for better caching
7+ COPY composer.json composer.lock ./
8+ RUN composer install --no-dev --no-interaction --prefer-dist --no-scripts --no-progress
99
10- # Production stage
11- FROM php:8.1-apache
10+ # Final image
11+ FROM php:8.2-fpm-alpine
1212
13- # Install system dependencies
14- RUN apt-get update && apt-get install -y \
15- libzip-dev \
16- libpng-dev \
17- libjpeg-dev \
18- libfreetype6-dev \
19- libxml2-dev \
20- libcurl4-openssl-dev \
21- git \
22- unzip \
23- curl \
24- && rm -rf /var/lib/apt/lists/*
13+ ENV APP_ENV=production \
14+ COMPOSER_ALLOW_SUPERUSER=1 \
15+ PHP_MEMORY_LIMIT=256M \
16+ PHP_OPCACHE_VALIDATE_TIMESTAMPS=0
2517
26- # Install PHP extensions
27- RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
28- && docker-php-ext-install -j$(nproc) \
29- pdo_mysql \
30- zip \
31- gd \
32- curl \
33- xml
18+ # Install system deps, nginx, supervisor, and PHP extensions
19+ RUN set -eux; \
20+ apk add --no-cache bash curl nginx supervisor; \
21+ docker-php-ext-install pdo pdo_mysql mysqli; \
22+ mkdir -p /run/nginx
3423
35- # Set working directory
3624WORKDIR /var/www/html
3725
38- # Copy application files
39- COPY . .
40-
41- # Copy vendor directory from composer stage
42- COPY --from=composer /app/vendor ./vendor
43-
44- # Create logs directory if it doesn't exist
45- RUN mkdir -p /var/www/html/logs
26+ # Copy app source
27+ COPY . /var/www/html
4628
47- # Create config log file and set proper permissions (as root)
48- RUN touch /var/www/html/config/app.log \
49- && chown -R www-data:www-data /var/www/html \
50- && chmod -R 755 /var/www/html \
51- && chmod -R 777 /var/www/html/logs \
52- && chmod 666 /var/www/html/config/app.log
29+ # Copy vendor from composer stage
30+ COPY --from=vendor /app/vendor /var/www/html/vendor
5331
54- # Configure Apache document root
55- ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
56- RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf \
57- && sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
32+ # Nginx and Supervisor configuration
33+ COPY docker/nginx/default.conf /etc/nginx/conf.d/default.conf
34+ COPY docker/supervisord.conf /etc/supervisord.conf
5835
59- # Enable mod_rewrite
60- RUN a2enmod rewrite
36+ # Permissions
37+ RUN addgroup -g 1000 -S www && adduser -u 1000 -S www -G www; \
38+ chown -R www:www /var/www/html; \
39+ find /var/www/html -type f -exec chmod 0644 {} \; ; \
40+ find /var/www/html -type d -exec chmod 0755 {} \; ; \
41+ chmod -R 0775 /var/www/html/logs || true
6142
62- # Security: Disable Apache version and server info
63- RUN echo "ServerTokens Prod" >> /etc/apache2/apache2.conf \
64- && echo "ServerSignature Off" >> /etc/apache2/apache2.conf
65-
66- # Create non-root user for security
67- RUN useradd -m -s /bin/bash appuser \
68- && usermod -a -G www-data appuser
69-
70- # Configure Apache to run as non-root user
71- RUN sed -i 's/Listen 80/Listen 8080/' /etc/apache2/ports.conf \
72- && sed -i 's/<VirtualHost \* :80>/<VirtualHost *:8080>/' /etc/apache2/sites-available/000-default.conf
73-
74- # Expose port 8080 (non-privileged port)
7543EXPOSE 8080
7644
77- # Health check
78- HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
79- CMD curl -f http://localhost:8080/ || exit 1
45+ CMD ["/usr/bin/supervisord" , "-c" , "/etc/supervisord.conf" ]
46+
8047
81- # Start Apache as non-root user
82- USER appuser
83- CMD ["apache2-foreground" ]
0 commit comments