Docker Compose for PHP + MySQL + Elasticsearch
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.
MySQL 8 is the world's most widely deployed open-source relational database. It integrates seamlessly with Symfony via Doctrine ORM and with Laravel via Eloquent, and benefits from extensive tooling and hosting support.
Elasticsearch provides distributed full-text search and real-time analytics. It requires at least 1 GB of dedicated memory — ensure your Docker host has sufficient RAM before starting the stack.
When should you use this stack?
PHP with MySQL and Elasticsearch 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
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
mysql:
image: mysql:8
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD-root}
MYSQL_DATABASE: ${MYSQL_DATABASE-app}
MYSQL_USER: ${MYSQL_USER-app}
MYSQL_PASSWORD: ${MYSQL_PASSWORD-secret}
ports:
- "${MYSQL_PORT-3306}:3306"
volumes:
- mysql_data:/var/lib/mysql
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 10s
timeout: 5s
retries: 5
elasticsearch:
image: elasticsearch:8.11.0
environment:
discovery.type: single-node
xpack.security.enabled: "false"
ES_JAVA_OPTS: "-Xms512m -Xmx512m"
ports:
- "${ELASTICSEARCH_PORT-9200}:9200"
volumes:
- elasticsearch_data:/usr/share/elasticsearch/data
healthcheck:
test: ["CMD-SHELL", "curl -s http://localhost:9200/_cluster/health | grep -q status"]
interval: 30s
timeout: 10s
retries: 5
volumes:
mysql_data:
elasticsearch_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 MYSQL_PORT=3306 MYSQL_DATABASE=app MYSQL_USER=app MYSQL_PASSWORD=secret ELASTICSEARCH_PORT=9200