From 24e26be2e1920de0c579ec4efc7c0e1ef9c47c61 Mon Sep 17 00:00:00 2001 From: zstadler Date: Thu, 3 Nov 2022 09:52:03 +0200 Subject: [PATCH] Add Docker Healthcheck Fixes #635 --- .gitattributes | 11 +++++++++++ Dockerfile | 1 + Dockerfile_light | 1 + src/healthcheck.js | 18 ++++++++++++++++++ 4 files changed, 31 insertions(+) create mode 100644 .gitattributes create mode 100644 src/healthcheck.js diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..210d79d --- /dev/null +++ b/.gitattributes @@ -0,0 +1,11 @@ +############################################################################### +# Set default behavior to automatically normalize line endings. +############################################################################### +* text=auto + +############################################################################### +# behavior for Unix scripts +# +# Unix scripts are treated as binary by default. +############################################################################### +*.sh eol=lf diff --git a/Dockerfile b/Dockerfile index 490749d..f7675fd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,7 @@ VOLUME /data WORKDIR /data EXPOSE 80 ENTRYPOINT ["/bin/bash", "/usr/src/app/run.sh"] +HEALTHCHECK CMD node /usr/src/app/src/healthcheck.js RUN apt-get -qq update \ && DEBIAN_FRONTEND=noninteractive apt-get -y install \ diff --git a/Dockerfile_light b/Dockerfile_light index 3312543..79639c2 100644 --- a/Dockerfile_light +++ b/Dockerfile_light @@ -5,6 +5,7 @@ EXPOSE 80 VOLUME /data WORKDIR /data ENTRYPOINT ["node", "/usr/src/app/", "-p", "80"] +HEALTHCHECK CMD node /usr/src/app/src/healthcheck.js RUN mkdir -p /usr/src/app COPY / /usr/src/app diff --git a/src/healthcheck.js b/src/healthcheck.js new file mode 100644 index 0000000..775ba03 --- /dev/null +++ b/src/healthcheck.js @@ -0,0 +1,18 @@ +var http = require("http"); +var options = { + timeout: 2000, +}; +var url = "http://localhost:80/health"; +var request = http.request(url, options, (res) => { + console.log(`STATUS: ${res.statusCode}`); + if (res.statusCode == 200) { + process.exit(0); + } else { + process.exit(1); + } +}); +request.on("error", function (err) { + console.log("ERROR"); + process.exit(1); +}); +request.end();