Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions docker/healthcheck.js
Original file line number Diff line number Diff line change
@@ -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);
});