Docker for PHP Developers: Complete Setup Guide
Home Blog Post Details
Docker for PHP Developers: Complete Setup Guide

Containerize your PHP applications with Docker. Learn how to set up development environments, manage dependencies, and deploy efficiently.

Docker for PHP Development: Complete Setup

Docker has revolutionized how we develop and deploy applications. For PHP developers, Docker offers consistent environments, easy dependency management, and simplified deployments.

Why Docker for PHP?

  • Environment Consistency: Same environment across development, staging, and production
  • Easy Setup: New team members can get started in minutes
  • Isolation: Each project has its own environment
  • Version Management: Test different PHP versions easily

Basic Dockerfile for PHP

FROM php:8.2-fpm

# Install system dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd

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

# Set working directory
WORKDIR /var/www

# Copy application code
COPY . /var/www

# Install dependencies
RUN composer install --optimize-autoloader --no-dev

EXPOSE 9000
CMD ["php-fpm"]

Docker Compose for Laravel

Use Docker Compose to orchestrate multiple services:

version: "3.8"

services:
  app:
    build: .
    volumes:
      - ./:/var/www
    depends_on:
      - database
      - redis

  webserver:
    image: nginx:alpine
    ports:
      - "80:80"
    volumes:
      - ./:/var/www
      - ./docker/nginx:/etc/nginx/conf.d
    depends_on:
      - app

  database:
    image: mysql:8.0
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_ROOT_PASSWORD: secret
    volumes:
      - dbdata:/var/lib/mysql
    ports:
      - "3306:3306"

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

volumes:
  dbdata:

Development Workflow

Optimize your Docker workflow for development:

# Start development environment
docker-compose up -d

# Run Laravel commands
docker-compose exec app php artisan migrate
docker-compose exec app php artisan queue:work

# Install new dependencies
docker-compose exec app composer require package-name

# Run tests
docker-compose exec app php artisan test

Production Considerations

  • Multi-stage builds: Reduce image size for production
  • Security: Use non-root users and minimal base images
  • Health checks: Implement container health monitoring
  • Secrets management: Use Docker secrets for sensitive data

"Docker doesn't just solve the 'it works on my machine' problem - it transforms how we think about application deployment and scaling."

Start small with Docker in development, then gradually move to containerized production deployments as you become more comfortable with the technology.