From 5fd91a96e8e45fc6e012cceaead46851b630774f Mon Sep 17 00:00:00 2001 From: Slawomir Koszewski Date: Sun, 19 Apr 2026 23:10:40 +0200 Subject: [PATCH] feat: add health check script and update Dockerfile for health check integration --- Dockerfile | 4 +++- healthcheck.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 healthcheck.js diff --git a/Dockerfile b/Dockerfile index f345109..98f291a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,7 +26,9 @@ COPY --from=build /app/backend/node_modules dist/backend/node_modules WORKDIR /app COPY entrypoint.sh entrypoint.sh +COPY healthcheck.js healthcheck.js RUN chmod +x entrypoint.sh EXPOSE 3000 -CMD ["./entrypoint.sh"] +HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 CMD ["node", "/app/healthcheck.js"] +ENTRYPOINT ["./entrypoint.sh"] diff --git a/healthcheck.js b/healthcheck.js new file mode 100644 index 0000000..9febd4a --- /dev/null +++ b/healthcheck.js @@ -0,0 +1,39 @@ +#!/usr/bin/env node +"use strict"; + +const http = require("node:http"); + +const port = Number.parseInt(process.env.PORT || "3000", 10); +const path = process.env.HEALTHCHECK_PATH || "/api/templates"; +const timeoutMs = Number.parseInt(process.env.HEALTHCHECK_TIMEOUT_MS || "3000", 10); + +const req = http.request( + { + host: "127.0.0.1", + port, + path, + method: "GET", + timeout: timeoutMs + }, + (res) => { + // Drain the response so the socket can close cleanly. + res.resume(); + + if (res.statusCode && res.statusCode >= 200 && res.statusCode < 400) { + process.exit(0); + return; + } + + process.exit(1); + } +); + +req.on("timeout", () => { + req.destroy(new Error("healthcheck timeout")); +}); + +req.on("error", () => { + process.exit(1); +}); + +req.end();