PHP › PHP with Docker

What is a Dockerfile, and what basic instructions are typically included when building a Docker image for a PHP application?

Beginner

Answer

A Dockerfile is a text file containing a series of instructions describing exactly how to build a Docker image, typically starting with a FROM instruction specifying a base PHP image, followed by instructions to install any needed PHP extensions, copy your application code into the image, install Composer dependencies, and specify what command should run when a container is started from that image.
FROM php:8.2-fpm
RUN docker-php-ext-install pdo_mysql
COPY . /var/www/html
WORKDIR /var/www/html
RUN composer install --no-dev
Real-world example A developer writes a Dockerfile that starts from an official PHP-FPM base image, installs the specific database extension their application needs, and copies in their application code along with running Composer to install dependencies, producing a completely self contained, reproducible image.

Common follow-ups: What is the difference between the php-fpm and php-apache base images on Docker Hub?;Why is the order of instructions within a Dockerfile important for build performance?