Article

Containerize Legacy PHP Applications

Use Docker to pin PHP versions and extensions for legacy apps when it improves parity—without pretending containers alone modernize the code.

Containers help when the pain is environment drift: “works on my PHP 8 laptop, production is 7.4 with three missing extensions.” They do not remove SQL injection or abandoned packages. Use Docker to pin the runtime ladder and give developers production-like SAPIs—then keep modernizing the code itself.

When Docker helps

  • Pinning PHP minor versions and extensions across laptops and CI
  • Reproducing FPM vs CLI differences
  • Providing Xdebug without polluting host PHP

When it does not

  • As a substitute for fixing platform constraints or autoloading
  • When the team cannot operate images in production and local-only containers diverge again
MODERN — pin the runtime
FROM php:8.2-fpm-bookworm
RUN docker-php-ext-install pdo_mysql opcache
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
WORKDIR /var/www/app

Cover volumes and permissions (www-data vs host UIDs), Apache vs nginx+FPM, Composer install during build vs runtime, and database service linking. Generate Xdebug snippets with Docker PHP Xdebug Generator; size pools with PHP-FPM Pool Calculator.

Related reading