← Customise this setup

Docker Compose for PHP + MongoDB

Stack
PHP-FPM + Nginx + MongoDB

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.

MongoDB 7 is a document-oriented NoSQL database ideal for flexible schemas and horizontal scaling. It suits content-heavy applications, real-time data pipelines, and projects where the data model evolves rapidly during development.

When should you use this stack?

PHP with MongoDB 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.

Quick start
Start stack docker compose up -d
Web http://localhost:8080

compose.yml

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

  mongodb:
    image: mongo:7
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGODB_INITDB_ROOT_USERNAME-app}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGODB_INITDB_ROOT_PASSWORD-secret}
      MONGO_INITDB_DATABASE: ${MONGODB_INITDB_DATABASE-app}
    ports:
      - "${MONGODB_PORT-27017}:27017"
    volumes:
      - mongodb_data:/data/db
    healthcheck:
      test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
      interval: 10s
      timeout: 5s
      retries: 5

volumes:
  mongodb_data:

docker/nginx/default.conf

Run mkdir -p docker/nginx first

docker/nginx/default.conf
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

.env.example
WEB_PORT=8080

MONGODB_PORT=27017
MONGODB_INITDB_ROOT_USERNAME=app
MONGODB_INITDB_ROOT_PASSWORD=secret
MONGODB_INITDB_DATABASE=app

Frequently asked questions

How do I start this PHP stack?
Download the files, run `docker compose up -d` from the project root, then open http://localhost:8080 in your browser. The web port is configurable via the `WEB_PORT` variable in `.env`.
Which PHP version does this configuration use?
This page uses PHP 8.5 by default. Use the generator above to pick 8.1 through 8.5 and regenerate the files with your chosen version.
How do I connect to MongoDB from my host machine?
From your host, connect with `mongosh mongodb://app:secret@127.0.0.1:27017`. From other containers on the same network, use host `mongodb` and port 27017.
Is this configuration production-ready?
No — it is tuned for local development (bind-mounted source, default credentials, exposed ports). For production, remove the bind mounts, set real secrets, and add resource limits and restart policies.

Related configurations