Halo, kalau kamu merasa tulisan saya ngebantu kamu, kamu bisa ucapkan terima kasih lewat saweria .

Hello, if you find this article helpful, you can express your gratitude through saweria .



Kasus

Contoh, kita punya satu container berisi nginx:latest

version : '3.8'
name: nginx

services:
  nginx:
    image: nginx:latest
    ports:
        - 99:80
    healthcheck:
      #it's for faster test only
      #for production use localhost:80
      test: ["CMD-SHELL", "curl -fSs localhost:99 || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 2
      start_period: 10s
    container_name: nginx-ctr
    restart: always
    networks:
        - nginx-net

networks:
    nginx-net:
Healthcheck Failed Tapi Container Ga Restart

Healthcheck Failed Tapi Container Ga Restart

Ternyata karena memang healthcheck only work di Swarm mode

Terus bagaimana caranya agar healthcheck bisa jalan di non Swarm mode??


How To

Untuk mengakali hal tersebut kita bisa menggunakan image Docker Autoheal

  1. Kita tambahkan label dari file compose nginx di atas
version : '3.8'
name: nginx

services:
  nginx:
    image: nginx:latest
    ports:
        - 99:80
    healthcheck:
      #it's for faster test only
      #for production use localhost:80
      test: ["CMD-SHELL", "curl -fSs localhost:99 || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 2
      start_period: 10s
    container_name: nginx-ctr
    restart: always
    #add this label
    labels:
      autoheal-app: true
    networks:
        - nginx-net

networks:
    nginx-net:
  1. Buat file docker-compose untuk service Docker Autoheal
version: "3.8"
name: autoheal

services:
  autoheal:
    image: willfarrell/autoheal:latest
    container_name: autoheal-ctr
    restart: always
    environment:
      - AUTOHEAL_CONTAINER_LABEL=all
      - AUTOHEAL_INTERVAL=5
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - /etc/localtime:/etc/localtime:ro
    networks:
      - autoheal-net

networks:
  autoheal-net:
  1. Jalankan service Docker Autoheal , lalu kita cek live log docker logs -f autoheal-ctr
Autoheal Working!

Autoheal Working!