Content · Web Development · PHP

Using Docker Compose for PHP and MySQL

Show how application code connects to the Compose database service over the private service network. The host name inside the PHP container is the Compose service name, not

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

Show how application code connects to the Compose database service over the private service network.

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

What It Does


Show how application code connects to the Compose database service over the private service network.

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
$db = new PDO('mysql:host=db;dbname=app;charset=utf8mb4', getenv('DB_USER'), getenv('DB_PASSWORD'));
$stmt = $db->query('SELECT NOW()');
echo $stmt->fetchColumn();

Example 2: A Second Scenario


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

<?php
$db = new PDO('mysql:host=db;port=3306;dbname=app;charset=utf8mb4', getenv('DB_USER'), getenv('DB_PASSWORD'));
$result = $db->query('SELECT DATABASE()')->fetchColumn();
echo $result;

Example 3: Practical Checks


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

<?php
$host = getenv('DB_HOST') ?: 'db';
if ($host === '127.0.0.1') throw new RuntimeException('Separate containers cannot use loopback for service discovery');
$db = new PDO('mysql:host=' . $host . ';dbname=app;charset=utf8mb4', getenv('DB_USER'), getenv('DB_PASSWORD'));

Important Behavior and Edge Cases


The host name inside the PHP container is the Compose service name, not localhost.

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

Comparison with Related Approaches


ApproachImportant distinction
Using Docker Compose for PHP and MySQLShow how application code connects to the Compose database service over the private service network.
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 casesThe host name inside the PHP container is the Compose service name, not localhost.
Using a related API interchangeablyUse this workflow as a focused starting point rather than a substitute for application-level tests.

Key Takeaways


  • Show how application code connects to the Compose database service over the private service network.
  • The host name inside the PHP container is the Compose service name, not localhost.
  • 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.