
Table of Contents
What is Docker Compose?
Docker Compose is a tool for defining and running multi-container Docker applications. Instead of typing multiple lengthy docker run commands for each container, you write a single docker-compose.yml file and start everything with one command: docker compose up.
For example, a typical web application consists of three parts: a web server, an application server, and a database. Docker Compose lets all three services work together without manually configuring the network for each one.
💡 Docker Compose is part of the Docker ecosystem. Since Docker v20.10, Compose v2 is bundled as a standard plugin — no separate installation required.
Docker vs Docker Compose
Beginners often confuse the two. Here's a clear comparison:
| Feature | Docker | Docker Compose |
|---|---|---|
| Manages | Single container | Multiple containers simultaneously |
| Main command | docker run | docker compose up |
| Config file | Dockerfile | docker-compose.yml |
| Networking | Manual flags | Auto-created network |
| Best for | Single service, CI/CD | Multi-service apps, Dev/Prod |
| Dependency order | None | Supported via depends_on |
Installing Docker + Compose on Ubuntu VPS
For VPS running Ubuntu 22.04 or 24.04, follow these steps:
Step 1: Update packages and install dependencies
sudo apt update && sudo apt upgrade -y
sudo apt install -y ca-certificates curl gnupg lsb-release
Step 2: Add Docker GPG key and repository
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Step 3: Install Docker Engine + Compose plugin
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io \
docker-buildx-plugin docker-compose-plugin
Step 4: Add user to docker group
sudo usermod -aG docker $USER
newgrp docker
Verify installation
docker --version
docker compose version
⚠️ If docker compose (v2) is not found, verify that docker-compose-plugin is installed — don't confuse it with docker-compose (v1, hyphenated, deprecated).
docker-compose.yml File Structure
The docker-compose.yml file uses YAML format with three main sections:
services: # Define each container
web:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html
depends_on:
- app
app:
build: .
environment:
- DB_HOST=db
networks:
- backend
db:
image: mysql:8
environment:
MYSQL_ROOT_PASSWORD: secret
volumes:
- db_data:/var/lib/mysql
networks:
- backend
volumes: # Define named volumes
db_data:
networks: # Define custom networks
backend:
| Key | Description |
|---|---|
services | All containers in the stack |
image | Docker image to use |
build | Build from Dockerfile |
ports | Map host:container ports |
volumes | Mount files/directories |
environment | Set environment variables |
depends_on | Start after another service |
networks | Assign to a network |
restart | Auto-restart policy |
Example: WordPress + MySQL on VPS
This is one of the most common Docker Compose use cases — running WordPress with a MySQL database:
version: '3.9'
services:
wordpress:
image: wordpress:latest
ports:
- "8080:80"
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wpuser
WORDPRESS_DB_PASSWORD: ${DB_PASSWORD}
WORDPRESS_DB_NAME: wordpress
volumes:
- wp_data:/var/www/html
depends_on:
- db
db:
image: mysql:8.0
restart: always
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: wordpress
MYSQL_USER: wpuser
MYSQL_PASSWORD: ${DB_PASSWORD}
volumes:
- db_data:/var/lib/mysql
volumes:
wp_data:
db_data:
Save as docker-compose.yml and run:
docker compose up -d
docker compose ps # Check status
Common Commands
| Command | Description |
|---|---|
docker compose up -d | Start all services (detached) |
docker compose down | Stop and remove containers |
docker compose down -v | Stop and remove containers + volumes |
docker compose ps | Show status of all services |
docker compose logs -f | Stream real-time logs |
docker compose logs web | Show logs for service "web" |
docker compose exec app bash | Open shell inside container |
docker compose restart db | Restart service "db" only |
docker compose pull | Pull latest images |
docker compose build --no-cache | Rebuild images fresh |
Networking and Volumes
Networking
When you run docker compose up, Docker automatically creates a default network named <foldername>_default. Every service can communicate with others using their service name as the hostname — so a service named db is reachable at hostname db from any other container in the same stack.
Volume Types
| Type | Example | Best for |
|---|---|---|
| Named volume | db_data:/var/lib/mysql | Databases, persistent data |
| Bind mount | ./src:/app/src | Development, hot reload |
| Anonymous volume | /tmp/cache | Temporary data |
Managing Environment Variables with .env
Sensitive values like passwords should be stored in a separate .env file:
# .env
DB_PASSWORD=myS3cur3P@ss
MYSQL_ROOT_PASSWORD=r00tS3cr3t
Docker Compose automatically loads .env when it's in the same directory, and ${DB_PASSWORD} in the YAML is replaced with the actual value.
🔒 Always add .env to your .gitignore before committing to prevent credential leaks.
Production Tips for VPS
- Add
restart: alwaysto critical services so containers automatically restart after a VPS reboot. - Limit resources with
deploy.resources.limitsto prevent any container from consuming excessive CPU or RAM. - Use Nginx as a reverse proxy in front for SSL termination and routing.
- Add health checks with
healthcheck:so Compose waits for a service to be truly ready before starting dependents. - Back up volumes regularly — named volume data lives at
/var/lib/docker/volumes/. - Use override files —
docker-compose.override.ymlfor dev-specific settings, keeping the base file clean for production.
📌 AsiaGB VPS features SSD storage and 99% uptime — ideal for running Docker Compose stacks in production. Choose from Thailand (BKK) or Singapore VPS at asiagb.com/vps.html
Frequently Asked Questions
docker run. Docker Compose lets you define and run multi-container applications using a single YAML file, making service orchestration much simpler.docker-compose (Python-based, deprecated). Compose v2 is a Docker CLI plugin using docker compose (no hyphen), offering better performance and support for the latest Compose Specification.docker compose up again, volumes are remounted and your database data persists. Only docker compose down -v deletes volumes.-d flag means detached mode — containers run in the background without printing logs to the terminal, leaving the shell free for other commands.Summary
Docker Compose is an essential tool for developers who want to run multi-container apps on a VPS in an organized, repeatable way. Key benefits include:
- Define your entire stack in one YAML file — great for team collaboration
- Automatic network creation — services communicate by name
- Easy volume management — data persists across restarts
- One command
docker compose up -dstarts everything
Get started with AsiaGB VPS featuring SSD storage and 99% uptime — suitable for Docker Compose stacks from development to production.