Docker and PHP: Application Containerization for Developers

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why Containerize PHP Applications with Docker?

Modern web development in PHP rarely goes without using containerization. Docker allows you to package your application with all its dependencies (web server, PHP extensions, database) into an isolated container. This guarantees that the application will work identically on a developer's local machine, on a testing server, and in a production environment. In this article, we will break down how to properly configure Docker for PHP, create a ready-made project, and review best practices.



Advantages of Using Docker with PHP

  • Environment Reproducibility: forget the "it works on my machine" problem.
  • Isolation: each application lives in its own container with the required versions of PHP and libraries.
  • Scalability: easily run multiple instances of the application using docker-compose.
  • Simplified CI/CD: containers are ideal for automated build and deployment pipelines.


Creating a Dockerfile for a PHP Application

Let's start with the most important file — the Dockerfile. It describes how the image of your PHP application will be built. Let's look at a basic example for a PHP 8.2 application with the Apache web server.



# DockerfileFROM php:8.2-apache

# Install PHP extensionsRUN docker-php-ext-install pdo pdo_mysql mysqli

# Enable mod_rewrite for Apache (useful for frameworks)RUN a2enmod rewrite

# Copy application source codeCOPY . /var/www/html/

# Change owner for correct file permissionsRUN chown -R www-data:www-data /var/www/html

# Expose port 80EXPOSE 80


This Dockerfile takes the official PHP 8.2 image with Apache, installs MySQL drivers, and copies your code. For more complex projects, such as those on Laravel or Symfony, additional configuration will be required.



Example for Laravel with Nginx and PHP-FPM

For modern frameworks, the Nginx + PHP-FPM combination is often used. Here is an example Dockerfile for PHP-FPM:



# Dockerfile.php-fpmFROM php:8.2-fpm

# Install system dependencies and extensionsRUN apt-get update && apt-get install -y \ libpq-dev \ libzip-dev \ unzip \ git \ && docker-php-ext-install pdo pdo_mysql zip

# Install ComposerCOPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Copy projectWORKDIR /var/wwwCOPY . .

# Install PHP dependenciesRUN composer install --no-interaction --optimize-autoloader

# File permissions for LaravelRUN chown -R www-data:www-data /var/www/storage /var/www/bootstrap/cache

CMD ["php-fpm"]


And here is the Nginx configuration, which will need to be placed in a separate container via docker-compose:



# default.conf (for Nginx)server {    listen 80;    server_name localhost;    root /var/www/public;

add_header X-Frame-Options "SAMEORIGIN"; add_header X-Content-Type-Options "nosniff";

index index.php;

charset utf-8;

location / { try_files $uri $uri/ /index.php?$query_string; }

location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; }

error_page 404 /index.php;

location ~ \\.php$ { fastcgi_pass php:9000; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi_params; }

location ~ /\\.(?!well-known).* { deny all; }}


Orchestration with docker-compose

To manage multiple containers (PHP, Nginx, MySQL), use docker-compose.yml. This is the standard tool for local development and testing.



# docker-compose.ymlversion: '3.8'

services: php: build: co

Blogs

Book Recommendations