Content · Web Development · PHP

Running PHP, Nginx, and MySQL with Docker Compose

Run separate web, PHP-FPM and database services with explicit service discovery and persistent database storage. Use Docker secrets or environment injection for credentials and never publish the database port

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Run separate web, PHP-FPM and database services with explicit service discovery and persistent database storage.

The objective is to understand the behavior and operational limits, not only the method name.

What It Does


Run separate web, PHP-FPM and database services with explicit service discovery and persistent database storage.

Prerequisites


Use a dedicated development environment and sample data. Where a snippet uses an injected connection, repository, request value or mailer, supply it from your own application setup.

Example 1: Basic Use


A focused working pattern for this topic. Names such as $pdo, $authContext and $repo represent dependencies provided by the application unless initialized in the snippet.

<?php
$dsn = 'mysql:host=' . (getenv('DB_HOST') ?: 'db') . ';dbname=app;charset=utf8mb4';
$db = new PDO($dsn, getenv('DB_USER'), getenv('DB_PASSWORD'));
echo $db->query('SELECT VERSION()')->fetchColumn();

Example 2: A Second Scenario


A second use case that adds a different operation or data flow specific to this topic.

<?php
$dbHost = getenv('DB_HOST') ?: 'db';
$dsn = "mysql:host=$dbHost;port=3306;dbname=app;charset=utf8mb4";
$pdo = new PDO($dsn, getenv('DB_USER'), getenv('DB_PASSWORD'));
echo $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);

Example 3: Practical Checks


A practical validation, failure or deployment concern specific to this topic.

<?php
$trustedHost = getenv('DB_HOST');
if (!$trustedHost || $trustedHost === 'localhost') throw new RuntimeException('Use the Compose database service hostname');
$db = new PDO("mysql:host=$trustedHost;dbname=app;charset=utf8mb4", getenv('DB_USER'), getenv('DB_PASSWORD'));

Important Behavior and Edge Cases


Use Docker secrets or environment injection for credentials and never publish the database port unnecessarily.

For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.

Comparison with Related Approaches


ApproachImportant distinction
Running PHP, Nginx, and MySQL with Docker ComposeRun separate web, PHP-FPM and database services with explicit service discovery and persistent database storage.
Related API / patternUse this workflow as a focused starting point rather than a substitute for application-level tests.

Common Beginner Mistakes


MistakeCorrect approach
Assuming the happy path covers edge casesUse Docker secrets or environment injection for credentials and never publish the database port unnecessarily.
Using a related API interchangeablyUse this workflow as a focused starting point rather than a substitute for application-level tests.

Key Takeaways


  • Run separate web, PHP-FPM and database services with explicit service discovery and persistent database storage.
  • Use Docker secrets or environment injection for credentials and never publish the database port unnecessarily.
  • Use this workflow as a focused starting point rather than a substitute for application-level tests.

Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.

Share this resourceSend this page to someone who may find it useful.