Docker Compose Basics: Script Your First Multi-Container App
September 18, 2022
If you’ve ever run multiple services (like a database + a web server), you know how messy it gets to start each container manually. That’s where Docker Compose comes in. With one simple YAML file, you can define and run multi-container apps in seconds.
In this guide, you’ll learn the basics of Docker Compose by creating a simple PHP + MySQL app.
🐳 Step 1: Install Docker & Docker Compose
Make sure Docker is installed:
docker -v
docker compose version
If you see versions, you’re good to go.
⚡ Step 2: Create Your Project Files
We’ll make a folder with two files:
mkdir myapp && cd myapp
touch docker-compose.yml index.php
index.php (just a test page):
<?php
$host = "db";
$user = "root";
$pass = "example";
$dbname = "testdb";
$conn = new mysqli($host, $user, $pass, $dbname);
if($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected to MySQL successfully!";
?>
⚙️ Step 3: Docker Compose Script
Create docker-compose.yml:
version: "3.8"
services:
web:
image: php:8.2-apache
volumes:
- ./index.php:/var/www/html/index.php
ports:
- "8080:80"
depends_on:
- db
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: testdb
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:
🚀 Step 4: Run It
Start your app with:
docker compose up -d
Open http://localhost:8080 you should see Connected to MySQL successfully!.
🎯 Why This Matters
- One command to start multiple containers
- Easy to scale and manage apps
- Standard for modern development
✅ You just learned Docker Compose basics and built your first multi-container app. Try extending it with phpMyAdmin or a Redis cache next!