feat: implement Dockerfile, entrypoint, and health check for Node.js application
This commit is contained in:
34
app-new/Dockerfile
Normal file
34
app-new/Dockerfile
Normal file
@@ -0,0 +1,34 @@
|
||||
FROM node:24-trixie-slim AS build
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY app-new/backend/package*.json backend/
|
||||
COPY app-new/frontend/package*.json frontend/
|
||||
RUN cd backend && npm ci
|
||||
RUN cd frontend && npm ci
|
||||
|
||||
COPY app-new .
|
||||
|
||||
RUN cd backend && npm run build
|
||||
RUN cd frontend && npm run build
|
||||
RUN cd backend && npm prune --omit=dev
|
||||
|
||||
FROM node:24-trixie-slim AS runtime
|
||||
|
||||
WORKDIR /app
|
||||
ENV NODE_ENV=production
|
||||
ENV PORT=3000
|
||||
|
||||
COPY --from=build /app/dist dist
|
||||
COPY --from=build /app/templates templates
|
||||
COPY --from=build /app/templates.json templates.json
|
||||
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
|
||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 CMD ["node", "/app/healthcheck.js"]
|
||||
ENTRYPOINT ["./entrypoint.sh"]
|
||||
4
app-new/entrypoint.sh
Executable file
4
app-new/entrypoint.sh
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env sh
|
||||
set -eu
|
||||
|
||||
exec node /app/dist/backend/server.js
|
||||
39
app-new/healthcheck.js
Normal file
39
app-new/healthcheck.js
Normal file
@@ -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/health";
|
||||
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();
|
||||
Reference in New Issue
Block a user