Skip to content

Instantly share code, notes, and snippets.

@nkhil
Created August 6, 2026 23:40
Show Gist options
  • Select an option

  • Save nkhil/28fbf84b69f58316a7e8682e95c018d0 to your computer and use it in GitHub Desktop.

Select an option

Save nkhil/28fbf84b69f58316a7e8682e95c018d0 to your computer and use it in GitHub Desktop.
Docker compose healthcheck
services:
  db:
    image: postgres:18
    container_name: db
    environment:
      POSTGRES_USER: hjkl
      POSTGRES_PASSWORD: hjkl
      POSTGRES_DB: hjkl
    volumes:
      - data:/var/lib/postgresql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U wyrm -d wyrm"]
      interval: 5s
      timeout: 5s
      retries: 5
    restart: unless-stopped
  api:
    image: node:20-alpine
    container_name: app_api
    command: >
      sh -c "node -e \"
        const http = require('http');
        const server = http.createServer((req, res) => {
          if (req.url === '/health') {
            res.writeHead(200);
            res.end('ok');
            return;
          }
          res.writeHead(200);
          res.end('Hello from the API');
        });
        server.listen(8080);
      \""
    ports:
      - "8080:8080"
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://localhost:8080/health > /dev/null 2>&1"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    depends_on:
      - db
    restart: unless-stopped

  app:
    image: node:20-alpine
    container_name: app_frontend
    depends_on:
      api:
        condition: service_healthy
    command: ["node", "-e", "console.log('app started'); setInterval(() => {}, 1000)"]
    restart: unless-stopped
volumes:
  data:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment