From 09b13b55880222dbb9da1ac7307d56fe41fc5b8c Mon Sep 17 00:00:00 2001 From: Daniel Hiller <46579393+daniel-hiller@users.noreply.github.com> Date: Tue, 6 Feb 2024 00:48:21 +0100 Subject: [PATCH] feat: Add healthcheck for docker deployments fix: Missing healthcheck.js fix: missing healthcheck.js fix: HEALTHCHECK command --- Dockerfile | 4 ++++ docker-compose-dev.yml | 25 ++++++++++++++----------- docker-compose.yml | 20 +++++++++----------- healthcheck.js | 23 +++++++++++++++++++++++ 4 files changed, 50 insertions(+), 22 deletions(-) create mode 100644 healthcheck.js diff --git a/Dockerfile b/Dockerfile index b0775413..c1b26e23 100644 --- a/Dockerfile +++ b/Dockerfile @@ -38,6 +38,7 @@ WORKDIR /app COPY --chown=node:node start.sh . COPY --chown=node:node server . +COPY --chown=node:node healthcheck.js . RUN mv .env.sample .env @@ -52,4 +53,7 @@ VOLUME /app/private/attachments EXPOSE 1337 +HEALTHCHECK --interval=10s --timeout=2s --start-period=15s \ + CMD node ./healthcheck.js + CMD ["./start.sh"] diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml index 4f867d6f..48c501ae 100644 --- a/docker-compose-dev.yml +++ b/docker-compose-dev.yml @@ -3,15 +3,7 @@ version: '3' services: planka: image: ghcr.io/plankanban/planka:master - command: > - bash -c - "for i in `seq 1 30`; do - ./start.sh && - s=$$? && break || s=$$?; - echo \"Tried $$i times. Waiting 5 seconds...\"; - sleep 5; - done; (exit $$s)" - restart: unless-stopped + restart: on-failure volumes: - user-avatars:/app/public/user-avatars - project-background-images:/app/public/project-background-images @@ -44,19 +36,30 @@ services: # - OIDC_CLIENT_SECRET= # - OIDC_SCOPES=openid email profile # - OIDC_ADMIN_ROLES=admin + # - OIDC_EMAIL_ATTRIBUTE=email + # - OIDC_NAME_ATTRIBUTE=name + # - OIDC_USERNAME_ATTRIBUTE=preferred_username # - OIDC_ROLES_ATTRIBUTE=groups + # - OIDC_IGNORE_USERNAME=true # - OIDC_IGNORE_ROLES=true + # - OIDC_ENFORCED=true depends_on: - - postgres + postgres: + condition: service_healthy postgres: image: postgres:14-alpine - restart: unless-stopped + restart: on-failure volumes: - db-data:/var/lib/postgresql/data environment: - POSTGRES_DB=planka - POSTGRES_HOST_AUTH_METHOD=trust + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres -d planka"] + interval: 10s + timeout: 5s + retries: 5 volumes: user-avatars: diff --git a/docker-compose.yml b/docker-compose.yml index 52606970..40897709 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,15 +3,7 @@ version: '3' services: planka: image: ghcr.io/plankanban/planka:latest - command: > - bash -c - "for i in `seq 1 30`; do - ./start.sh && - s=$$? && break || s=$$?; - echo \"Tried $$i times. Waiting 5 seconds...\"; - sleep 5; - done; (exit $$s)" - restart: unless-stopped + restart: on-failure volumes: - user-avatars:/app/public/user-avatars - project-background-images:/app/public/project-background-images @@ -55,16 +47,22 @@ services: # - SLACK_BOT_TOKEN=xoxb-xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxx # - SLACK_CHANNEL_ID=xxxxxxxxxx depends_on: - - postgres + postgres: + condition: service_healthy postgres: image: postgres:14-alpine - restart: unless-stopped + restart: on-failure volumes: - db-data:/var/lib/postgresql/data environment: - POSTGRES_DB=planka - POSTGRES_HOST_AUTH_METHOD=trust + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres -d planka"] + interval: 10s + timeout: 5s + retries: 5 volumes: user-avatars: diff --git a/healthcheck.js b/healthcheck.js new file mode 100644 index 00000000..ed3dbce2 --- /dev/null +++ b/healthcheck.js @@ -0,0 +1,23 @@ +const http = require('http'); +const options = { + host: 'localhost', + port: 1337, + timeout: 2000 +}; + +const healthCheck = http.request(options, (res) => { + console.log(`HEALTHCHECK STATUS: ${res.statusCode}`); + if (res.statusCode == 200) { + process.exit(0); + } + else { + process.exit(1); + } +}); + +healthCheck.on('error', function (err) { + console.error('ERROR'); + process.exit(1); +}); + +healthCheck.end();