Docker for PHP Developers

matt
Matthew Gros · Jan 2, 2026

TLDR

Use Docker Compose for local dev, keep images small, mount source code as volumes, use multi-stage builds for production.

Docker for PHP Developers

Why Docker for PHP?

"Works on my machine" stops being a problem. Docker containers run identically everywhere.

Basic Setup

Create a docker-compose.yml:

version: '3.8'
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - .:/var/www/html
    ports:
      - "8000:80"
    depends_on:
      - mysql
      - redis

  mysql:
    image: mysql:8.0
    environment:
      MYSQL_DATABASE: app
      MYSQL_ROOT_PASSWORD: secret
    volumes:
      - mysql_data:/var/lib/mysql
    ports:
      - "3306:3306"

  redis:
    image: redis:alpine
    ports:
      - "6379:6379"

volumes:
  mysql_data:

The Dockerfile

FROM php:8.3-apache

# Install extensions
RUN docker-php-ext-install pdo pdo_mysql

# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Enable Apache modules
RUN a2enmod rewrite

WORKDIR /var/www/html

Running It

docker-compose up -d
docker-compose exec app composer install
docker-compose exec app php artisan migrate

Tips

  1. Use volumes for source code - Changes reflect immediately
  2. Persist database data - Named volumes survive container rebuilds
  3. Match production versions - Use the same PHP/MySQL versions
  4. Keep images small - Use Alpine variants where possible

Common Commands

# View logs
docker-compose logs -f app

# Shell into container
docker-compose exec app bash

# Rebuild after Dockerfile changes
docker-compose up -d --build

# Clean up
docker-compose down -v

About the Author

matt

I build and ship automation-driven products using Laravel and modern frontend stacks (Vue/React), with a focus on scalability, measurable outcomes, and tight user experience. I’m based in Toronto, have 13+ years in PHP, and I also hold a pilot’s license. I enjoy working on new tech projects and generally exploring new technology.