Docker Compose Production Ready: Restart Policy & Resource Limit

191 words • 1,462 chars Markdown
Raw

Docker Compose Anti-Tumbang di Jam 3 Pagi

Nih resep docker-compose.yml buat lu pada yang hobi deploy asal-asalan terus heran kenapa server megap-megap pas traffic loncat. Jangan cuma modal docker compose up -d doang kayak, tolong lah, pake otak dikit biar prod aman.

version: '3.8'

services:
  web-app:
    image: my-app:v1.0.0
    container_name: prod_web_app
    restart: unless-stopped
    ports:
      - "8080:80"
    environment:
      - NODE_ENV=production
    deploy:
      resources:
        limits: 
          cpus: '0.50'
          memory: 512M
        reservations:
          cpus: '0.25'
          memory: 256M
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

Catatan buat Lu:

  • restart: unless-stopped: Biar kalau kontainer mokad karena OOM atau server reboot, dia idup lagi sendiri. Lu nggak perlu melek malem-malem cuma buat ngetik docker start.
  • limits (CPU & Memory): Wajib pasang! Biar aplikasi rakus lu nggak ngabisin RAM VPS yang pas-pasan, berujung kernel panic trus VPS lu koma.
  • healthcheck: Biar Docker tahu aplikasi lu beneran sehat atau cuma pura-pura idup padahal bengong.
  • logging max-size: Biar disk VPS lu nggak jebol gara-gara log ngedumel mulu tanpa rem.

Udah, salin sono, deploy yang bener, jangan bikin onar di prod!