Development Workflow

Run PHP Applications with Docker Compose

Define PHP, web server and database services with Docker Compose for repeatable project environments.

Docker Compose describes a multi-container development environment in a YAML file. It can make PHP, a web server and a database reproducible across developer machines, but it also introduces image, volume, network and container lifecycle concepts.

Model services explicitly

A typical project may contain a PHP application service, a web server and a database. Pin image versions rather than using an unqualified latest tag. Use a named volume for database data and a bind mount for source code during development. Publish database ports only when a host-side client needs access.

Example Compose outline

services:
  app:
    build: .
    working_dir: /var/www/html
    volumes:
      - ./:/var/www/html
  web:
    image: nginx:stable
    ports:
      - "8080:80"
    volumes:
      - ./:/var/www/html:ro
      - ./docker/nginx.conf:/etc/nginx/conf.d/default.conf:ro
    depends_on:
      - app
  db:
    image: mariadb:11.4
    environment:
      MARIADB_DATABASE: app
      MARIADB_USER: app
      MARIADB_PASSWORD: ${DB_PASSWORD}
      MARIADB_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
    volumes:
      - db_data:/var/lib/mysql
volumes:
  db_data:

This is an architectural outline, not a complete production configuration. The PHP-FPM service, Dockerfile, Nginx fastcgi settings and secret handling must be supplied for the chosen project.

Understand lifecycle and persistence

docker compose up creates or starts services. Container filesystems are disposable; named volumes preserve database data across container recreation. docker compose down -v removes named volumes and can delete local database contents, so do not run it casually.

Handle credentials and ports

Use an untracked local environment file for development secrets and provide a safe example file. Avoid publishing the database to all network interfaces. Keep production secrets in a proper deployment secret store.

Further reading

Share this page Help other developers discover this resource.