Docker solves the “it works on my machine” problem by packaging your PHP application and all its dependencies into a container — an isolated environment that runs identically on your laptop, a CI server, and production. This tutorial builds a complete PHP Docker setup from scratch, including Nginx, PHP-FPM, and MySQL.
Table of Contents
Why Docker for PHP?
- Consistent environments — no more PHP version mismatches between development and production
- Easy PHP version switching — change one line in your Dockerfile, rebuild, done
- Reproducible setup — new team members run one command to get a working environment
- Production-ready — the same container image runs in development and on AWS/GCP/DigitalOcean
Prerequisites
Install Docker Desktop (free) from docker.com. This includes Docker Engine and Docker Compose. Verify installation:
docker --version # Docker version 26.x
docker compose version # Docker Compose version v2.x
Step 1: Write the Dockerfile
# Dockerfile
FROM php:8.3-fpm
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd opcache
# Configure OPcache for production performance
RUN echo "opcache.enable=1\nopcache.memory_consumption=256\nopcache.max_accelerated_files=20000\nopcache.validate_timestamps=0" \
>> /usr/local/etc/php/conf.d/opcache.ini
# Install Composer
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
# Set working directory
WORKDIR /var/www/html
# Copy project files
COPY . .
# Install PHP dependencies
RUN composer install --no-dev --optimize-autoloader
# Set permissions
RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache 2>/dev/null || true
EXPOSE 9000
CMD ["php-fpm"]
Step 2: Nginx Configuration
# docker/nginx/default.conf
server {
listen 80;
index index.php index.html;
server_name localhost;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
Step 3: docker-compose.yml
# docker-compose.yml
version: '3.8'
services:
# PHP-FPM application
php:
build:
context: .
dockerfile: Dockerfile
container_name: php_app
volumes:
- .:/var/www/html
networks:
- app_network
depends_on:
- mysql
- redis
# Nginx web server
nginx:
image: nginx:alpine
container_name: nginx
ports:
- "80:80"
- "443:443"
volumes:
- .:/var/www/html
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
networks:
- app_network
depends_on:
- php
# MySQL database
mysql:
image: mysql:8.0
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: myapp
MYSQL_USER: appuser
MYSQL_PASSWORD: apppassword
volumes:
- mysql_data:/var/lib/mysql
ports:
- "3306:3306"
networks:
- app_network
# Redis (caching and queues)
redis:
image: redis:7-alpine
container_name: redis
ports:
- "6379:6379"
networks:
- app_network
networks:
app_network:
driver: bridge
volumes:
mysql_data:
Step 4: Run Your Application
# Start all services:
docker compose up -d
# Check running containers:
docker compose ps
# View logs:
docker compose logs php
docker compose logs nginx
# Run Artisan commands inside the container:
docker compose exec php php artisan migrate
docker compose exec php php artisan key:generate
# Access your app:
# http://localhost
Development vs Production Docker Setup
# docker-compose.override.yml (development additions, auto-loaded with docker-compose.yml)
version: '3.8'
services:
php:
volumes:
- .:/var/www/html # Live code sync for development
environment:
APP_ENV: local
APP_DEBUG: 'true'
# Mailpit for email testing in development:
mailpit:
image: axllent/mailpit
ports:
- "8025:8025" # Web UI
- "1025:1025" # SMTP
# docker-compose.prod.yml (production overrides)
version: '3.8'
services:
php:
environment:
APP_ENV: production
APP_DEBUG: 'false'
# No volume mount — use baked-in code from image
# Run in production:
docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d
Laravel-Specific Docker Setup
# Create a new Laravel project with Docker:
docker run --rm -v $(pwd):/app composer create-project laravel/laravel my-app
cd my-app
# Add your docker-compose.yml and Dockerfile, then:
docker compose up -d
docker compose exec php php artisan key:generate
docker compose exec php php artisan migrate --seed
Frequently Asked Questions
Should I use Docker for development or only production?
Both. Development Docker ensures your local environment exactly matches production. The most common use: XAMPP for simple PHP projects, Docker for Laravel projects with multiple services (MySQL, Redis, queues). Laravel Sail (official Docker solution for Laravel) simplifies the setup with a single command.
What is the difference between PHP Apache and PHP-FPM?
PHP Apache (php:8.3-apache) runs PHP as an Apache module — simpler but less flexible. PHP-FPM (php:8.3-fpm) runs PHP as a separate process pool that Nginx connects to via FastCGI — the production standard for performance and flexibility. For serious applications, use PHP-FPM with Nginx.
Conclusion
Docker containerizes your PHP application into a portable, reproducible environment. The three-file setup (Dockerfile, nginx.conf, docker-compose.yml) gets a full PHP + Nginx + MySQL + Redis stack running locally in minutes. Once your containers work locally, deploying to AWS ECS, DigitalOcean App Platform, or any container-based cloud service is straightforward — the same image runs everywhere.
Next Step for Your Project Journey
Pick your next action to keep building: download complete source code, continue with practical tutorials, or explore more project systems.

