PHP › PHP with Docker

What is Docker, and what benefits does containerizing a PHP application provide compared to running it directly on a server?

Beginner

Answer

Docker lets you package an application along with all of its dependencies, including a specific PHP version, required extensions, and system libraries, into a portable, self contained container that runs consistently across any environment, whether on a developer's laptop, a testing server, or production, eliminating the common problem of an application working fine in one environment but failing in another due to subtle differences in installed software versions.
FROM php:8.2-fpm
COPY . /var/www/html
RUN docker-php-ext-install pdo_mysql
Real-world example A development team eliminates a long standing class of bugs caused by developers running different local PHP versions than production, by having everyone run the exact same Dockerized PHP environment both locally and in production.

Common follow-ups: What is the difference between a Docker image and a Docker container?;How does Docker compare to using a virtual machine for the same purpose?