๐ Real-World Examples โ
Scenario: Nginx + Node.js + MySQL Full-Stack Application โ
Let's deploy a real project: Nginx as a reverse proxy, Node.js as the backend API, and MySQL as the database.
Project Structure: โ
text
my-project/
โโโ docker-compose.yml
โโโ nginx/
โ โโโ nginx.conf
โโโ backend/
โ โโโ Dockerfile
โ โโโ package.json
โ โโโ app.js
โโโ frontend/
โโโ dist/
โโโ index.html
โโโ ...docker-compose.yml: โ
yaml
services:
nginx:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf
- ./frontend/dist:/usr/share/nginx/html
depends_on:
- backend
restart: unless-stopped
backend:
build: ./backend
environment:
- DB_HOST=db
- DB_PORT=3306
- DB_USER=root
- DB_PASS=secretpassword
- DB_NAME=webapp
depends_on:
db:
condition: service_healthy
restart: unless-stopped
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: secretpassword
MYSQL_DATABASE: webapp
volumes:
- mysql-data:/var/lib/mysql
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 10s
timeout: 5s
retries: 5
restart: unless-stopped
volumes:
mysql-data:nginx.conf: โ
nginx
server {
listen 80;
server_name example.com;
# Static files
location / {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ /index.html;
}
# API reverse proxy
location /api/ {
proxy_pass http://backend:3000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}One-Command Launch: โ
bash
# Build and start all services
docker compose up -d --build
[+] Building 25.3s (12/12) FINISHED
[+] Running 4/4
โ Network my-project_default Created
โ Container my-project-db-1 Healthy
โ Container my-project-backend-1 Started
โ Container my-project-nginx-1 Started
# Verify all services
docker compose ps
curl http://localhost
curl http://localhost/api/users