Senior LevelPHP 8+
How do you architect hardened, ultra-lean 40MB production Docker images for PHP-FPM using multi-stage builds?
❓ Interview Prompt: Explain multi-stage Docker builds for PHP, Alpine Linux base images, stripping dev dependencies (--no-dev), and running non-root www-data users.
Production Docker images should be minimal, hardened, and free of build compilers or dev dependencies.
Multi-Stage Build Architecture:
1
Stage 1 (Builder): Uses official Composer image to run 'composer install --no-dev --optimize-autoloader --no-scripts'.
2
Stage 2 (Runner): Uses 'php:8.3-fpm-alpine'. Copies only the compiled 'vendor/' directory from Stage 1.
3
Hardening: Configures container to run under non-root 'www-data' user, removing 'composer', Git, and build tools to minimize attack surface!
Code Implementation Example
bash
# Multi-Stage Production Dockerfile for PHP-FPM
# STAGE 1: Builder
FROM composer:2 AS builder
WORKDIR /app
COPY composer.json composer.lock ./
RUN composer install --no-dev --optimize-autoloader --no-scripts
# STAGE 2: Production Runner
FROM php:8.3-fpm-alpine
WORKDIR /var/www/html
COPY --from=builder /app/vendor ./vendor
COPY . .
USER www-data
EXPOSE 9000
CMD ["php-fpm"]💡Hardened multi-stage production Dockerfile for PHP-FPM Alpine.
🎯 Interview Key Takeaway
Use multi-stage Docker builds to copy production vendor files into a lean php:8.3-fpm-alpine runner image under www-data non-root user.
#Docker#Multi-Stage#PHP-FPM#Alpine#Senior
Back to all PHP 8+ Questions→