Skip to content

๐Ÿš€ 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