Docker Compose for PHP + PostgreSQL + Redis + Mailpit
A plain PHP setup gives complete control over your application with no framework overhead — ideal for learning Docker with PHP, running legacy codebases, or building lightweight applications with minimal dependencies.
PostgreSQL 15 is a powerful, ACID-compliant relational database with strong support for JSON, full-text search, and complex queries. Both Symfony (Doctrine) and Laravel (Eloquent) support PostgreSQL natively and it is increasingly preferred for new projects.
Redis is an in-memory data store used for caching, sessions, and message queuing. Symfony Cache and Messenger, and Laravel Cache and Queues, all support Redis natively — making it the most common addition to any PHP development stack.
When should you use this stack?
PHP with PostgreSQL and Redis is a solid default for local development when your production environment matches it. If you're evaluating alternatives, the related configurations below swap one piece at a time — same framework with a different database, or the same data layer under a different framework — so you can compare without starting from scratch.
docker compose up -d
http://localhost:8080
http://localhost:8025
compose.yml
services:
nginx:
image: nginx:latest
ports:
- "${WEB_PORT-8080}:80"
volumes:
- .:/var/www/html
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
depends_on:
- php
php:
image: php:8.5-fpm
volumes:
- .:/var/www/html
postgres:
image: postgres:15
environment:
POSTGRES_DB: ${POSTGRES_DB-app}
POSTGRES_USER: ${POSTGRES_USER-app}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD-secret}
ports:
- "${POSTGRES_PORT-5432}:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD", "pg_isready"]
interval: 10s
timeout: 5s
retries: 5
redis:
image: redis:7
ports:
- "${REDIS_PORT-6379}:6379"
mailpit:
image: axllent/mailpit
ports:
- "${MAILPIT_SMTP_PORT-1025}:1025"
- "${MAILPIT_UI_PORT-8025}:8025"
volumes:
postgres_data:
docker/nginx/default.conf
Run mkdir -p docker/nginx first
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
.env.example
Optional — Compose uses ${VAR-default} fallbacks
WEB_PORT=8080 POSTGRES_PORT=5432 POSTGRES_DB=app POSTGRES_USER=app POSTGRES_PASSWORD=secret REDIS_PORT=6379 MAILPIT_SMTP_PORT=1025 MAILPIT_UI_PORT=8025