Containerizing a PHP Application
Write a production-ready PHP Dockerfile.
Why Containerize PHP
Shipping PHP reproducibly means freezing the exact interpreter version, extensions, and OS libraries alongside your code. A Docker image gives every environment — laptop, CI, prod — the same php -v and the same ext-* set.
In this lesson we build a production-ready image: PHP-FPM, only the extensions you need, tuned configs, a non-root user, and a health check.
FPM vs Apache Base
The official PHP images come in flavors. For a production web app behind nginx/traefik, prefer php:8.3-fpm-alpine (small) or php:8.3-fpm (Debian, glibc — fewer surprises with native libs).
cli— workers, queues, cronfpm— FastCGI process manager, pair with nginxapache— bundled Apache, convenient but heavier
Pin the minor version. Never use :latest in prod.
# Base image choice in your Dockerfile
FROM php:8.3-fpm-alpine AS base
# Why alpine? ~30MB base vs ~140MB Debian.
# Tradeoff: musl libc, occasional native-extension friction.All lessons in this course
- Containerizing a PHP Application
- Multi-Stage Builds and Optimization
- Docker Compose for Local Stacks
- CI/CD with GitHub Actions