diff --git a/Dockerfile b/Dockerfile index a8fc872..6102855 100644 --- a/Dockerfile +++ b/Dockerfile @@ -47,7 +47,8 @@ USER node EXPOSE 5150 5151 # Healthcheck: perform lightweight HTTP request to ensure app responding +COPY docker/healthcheck.js /usr/local/bin/healthcheck.js HEALTHCHECK --interval=30s --timeout=5s --start-period=45s --retries=5 \ - CMD curl -fsS http://127.0.0.1:5150/config/appVersion?health || exit 1 + CMD node /usr/local/bin/healthcheck.js ENTRYPOINT ["node", "dist/app.js"] diff --git a/docker-compose.yml b/docker-compose.yml index 3ce5650..813431e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -28,7 +28,7 @@ services: - njspc-dash-logs:/app/logs - njspc-dash-uploads:/app/uploads healthcheck: - test: [ "CMD", "node", "-e", "require('net').createConnection({host:'127.0.0.1',port:5150},c=>{c.end();process.exit(0)}).on('error',()=>process.exit(1))" ] + test: ["CMD", "node", "/usr/local/bin/healthcheck.js"] interval: 60s timeout: 5s start_period: 30s diff --git a/docker/healthcheck.js b/docker/healthcheck.js new file mode 100644 index 0000000..febb79a --- /dev/null +++ b/docker/healthcheck.js @@ -0,0 +1,43 @@ +const fs = require("fs"); +const http = require("http"); + +const CONFIG_FILE = "/app/config.json"; +const DEFAULT_PORT = 5150; +const HEALTH_PATH = "/config/appVersion?health"; + +let port = DEFAULT_PORT; + +try { + const config = JSON.parse(fs.readFileSync(CONFIG_FILE, "utf8")); + + port = config.web?.servers?.http?.port ?? DEFAULT_PORT; +} catch { + // Use the default port if the config is unavailable or invalid. +} + +const request = http.get( + { + host: "127.0.0.1", + port, + path: HEALTH_PATH, + timeout: 5000, + }, + (response) => { + response.resume(); + + if (response.statusCode >= 200 && response.statusCode < 300) { + process.exit(0); + } + + process.exit(1); + } +); + +request.on("error", () => { + process.exit(1); +}); + +request.on("timeout", () => { + request.destroy(); + process.exit(1); +});