Skip to main content
Docker Basics Tutorial
CHAPTER 10 Beginner

Docker for PHP Development

Updated: May 15, 2026
25 min read

# CHAPTER 10

Docker for PHP Development

1. Introduction

Historically, PHP development involved downloading massive, cumbersome stacks like XAMPP, MAMP, or WAMP. These stacks force specific versions of PHP and Apache onto your computer, making it nearly impossible to work on Project A (which requires PHP 7.4) and Project B (which requires PHP 8.2) simultaneously. Docker destroys this limitation. In this chapter, we will learn how to containerize dynamic PHP applications, manage PHP extensions, and architect a professional local development workflow.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Identify the correct official PHP Docker image variants (CLI, FPM, Apache).
  • Host a dynamic PHP application using Docker Compose.
  • Install necessary PHP extensions (like mysqli or pdo) inside a custom Dockerfile.
  • Understand the difference between PHP-Apache and Nginx + PHP-FPM architectures.
  • Run Composer commands via Docker without installing Composer on your host machine.

3. Beginner-Friendly Explanation

Imagine owning a garage where you fix cars.
  • The XAMPP Way: You permanently weld a set of metric wrenches to your hands. You can easily fix Japanese cars. But if an American car comes in requiring imperial wrenches, you cannot fix it. You have to undergo surgery to change your hands.
  • The Docker Way: You have a closet full of mechanical robot suits. Suit 1 has metric tools (PHP 8.2). Suit 2 has imperial tools (PHP 7.4). When a car arrives, you step into the correct suit, fix the car, and step out. Your actual hands remain completely clean and unmodified.

4. The PHP Image Variants

If you visit the official PHP repository on Docker Hub, you will see confusing tags. You must choose the right variant:
  1. 1. php:8.2-cli: Just the raw PHP command-line interpreter. Used for running background scripts or Laravel Artisan commands. It cannot host a website.
  1. 2. php:8.2-apache: An all-in-one image. It contains Apache web server and PHP bolted together. It is heavy, but incredibly easy for beginners. Just drop in your files and it works.
  1. 3. php:8.2-fpm: (FastCGI Process Manager). The modern, high-performance standard. It contains ONLY PHP. It must be paired with a separate Nginx container acting as the web server to handle the HTTP requests.

5. Installing PHP Extensions

The official PHP images are incredibly barebones to save space. If your code tries to connect to a MySQL database, it will crash because the mysqli or pdo_mysql extensions are missing! You must write a custom Dockerfile using Docker's special helper scripts: docker-php-ext-install.

*Example Dockerfile for a PHP Database App:*

dockerfile
12345
FROM php:8.2-apache
# Install the MySQL PDO extension
RUN docker-php-ext-install pdo pdo_mysql
# Enable Apache mod_rewrite for modern routing
RUN a2enmod rewrite

6. Running Composer in Docker

Modern PHP relies on Composer to manage packages. You do NOT need to install Composer on your laptop. You can use an ephemeral Docker container to run it!
bash
1
docker run --rm -v $(pwd):/app composer install

*What happens:* Docker spins up a temporary container containing Composer, mounts your local project folder into /app, downloads the packages into a new vendor folder on your laptop, and instantly deletes the Composer container!

7. Mini Project: Dockerize a Dynamic PHP App

Let's build the simplest, most effective local PHP development environment.

Step-by-Step Tutorial:

  1. 1. Create a folder named php-docker-app. Open your terminal and cd into it.
  1. 2. Create an index.php file:

php
1234
<?php
$version = phpversion();
echo "<h1>Success! Running PHP Version: $version</h1>";
?>
  1. 3. Create a docker-compose.yml file:
yaml
12345678
version: &#039;3.8'
services:
  app:
    image: php:8.2-apache
    ports:
      - "8000:80"
    volumes:
      - ./:/var/www/html
  1. 4. Run the stack:
bash
1
docker-compose up -d
  1. 5. Open your browser to http://localhost:8000. You will see PHP dynamically rendering the version!
  1. 6. Because of the volume mount (./:/var/www/html), you can edit index.php, click save, and refresh the browser to see instant changes. No XAMPP required!

8. Real-World Scenarios

A freelancer inherits a legacy WordPress site from a client. The site crashes on modern PHP 8, requiring ancient PHP 5.6 to run. Instead of ruining their laptop by installing outdated, insecure software, the freelancer creates a docker-compose.yml with image: php:5.6-apache. The legacy site runs perfectly in isolation, allowing the freelancer to patch the code and upgrade it safely to PHP 8.

9. Best Practices

  • Production Architecture (Nginx + FPM): While the php-apache image is fantastic for local development, professional enterprise deployments decouple the stack. They use a custom Nginx container to handle raw HTTP traffic and serve static images/CSS, and they route .php file requests over a private Docker network to a separate php-fpm container to crunch the dynamic code. This allows the Web Server and the PHP processor to scale independently.

10. Common Mistakes

  • Missing File Permissions: When running composer install via a Docker volume mount on Linux, the files downloaded into the vendor folder might be owned by the Docker root user. If you try to delete them using your normal Linux user account, you will get "Permission Denied" errors. You must configure user ID mapping or use sudo chown to reclaim ownership of your files.

11. Exercises

  1. 1. Explain the architectural difference between the php:8.2-apache image and the php:8.2-fpm image.
  1. 2. What specific Dockerfile command is utilized to install missing database drivers into an official PHP image?

12. FAQs

Q: Does Docker make my PHP application run slower locally? A: On Linux, performance is 100% native. On macOS and Windows, because Docker utilizes a hidden Linux micro-VM and has to translate file system changes across the Bind Mounts, you might experience slight lag when loading massive frameworks like Laravel locally. (Features like Mutagen or WSL2 specific optimizations resolve this).

13. Interview Questions

  • Q: A developer complains that their newly containerized PHP application throws a "PDOException: could not find driver" error when attempting to connect to MySQL. Detail the exact steps required to resolve this issue utilizing a Dockerfile.
  • Q: Describe the architectural advantages of utilizing an Nginx + PHP-FPM multi-container architecture in production versus deploying a monolithic Apache+PHP container.

14. Summary

In Chapter 10, we permanently retired legacy, monolithic development stacks like XAMPP. We discovered how to dynamically spin up isolated, version-specific PHP environments in seconds. We learned to differentiate between PHP-CLI, Apache, and FPM image variants, selecting the correct tool for the job. Finally, we recognized the necessity of crafting custom Dockerfiles to inject missing PHP extensions, ensuring our applications can securely communicate with databases.

15. Next Chapter Recommendation

Our PHP application is running and attempting to connect to a database. But where is the database? Proceed to Chapter 11: Dockerizing Databases.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·