Skip to content

Commit cffef1c

Browse files
committed
Refactor Dockerfile for improved build stages and update health check endpoint. Add NODE_ENV to server for environment awareness and modify health check response to include environment status.
1 parent 78e5414 commit cffef1c

3 files changed

Lines changed: 28 additions & 22 deletions

File tree

Dockerfile

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,29 @@
1-
# Build Stage - Frontend
21
FROM node:20 AS frontend-builder
32
WORKDIR /app
43

5-
# Copy root package.json for workspace support
64
COPY package*.json ./
7-
# Copy client package.json
85
COPY client/package*.json ./client/
9-
# Copy server package.json
106
COPY server/package*.json ./server/
117

12-
# Install all dependencies (workspaces will be hoisted)
138
RUN npm ci
14-
15-
# Copy client source code
169
COPY client/ ./client/
1710

18-
# Build frontend (generic - no environment variables)
1911
RUN npm run build -w client
2012

21-
# Build Stage - Backend
2213
FROM node:20 AS backend-builder
2314
WORKDIR /app
2415

25-
# Copy root package.json for workspace support
2616
COPY package*.json ./
27-
# Copy server package.json
2817
COPY server/package*.json ./server/
2918

30-
# Install only production dependencies
3119
RUN npm ci --omit=dev
32-
33-
# Copy server source code
3420
COPY server/ ./server/
3521

36-
# Final Stage
3722
FROM node:20-slim
3823
WORKDIR /app
3924

40-
# Copy built frontend
4125
COPY --from=frontend-builder /app/client/dist ./dist
4226

43-
# Copy server files
4427
COPY --from=backend-builder /app/server ./server
4528
COPY --from=backend-builder /app/node_modules ./node_modules
4629
COPY --from=backend-builder /app/package*.json ./
@@ -52,8 +35,8 @@ ENV PORT=5000
5235

5336
EXPOSE 5000
5437

55-
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
56-
CMD node -e "require('http').get('http://localhost:5000/api/health', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})"
38+
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
39+
CMD node -e "require('http').get('http://localhost:' + (process.env.PORT || 5000) + '/healthz', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})"
5740

5841
CMD ["node", "index.js"]
5942

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ dotenv.config();
1111

1212
const app = express();
1313
const PORT = process.env.PORT || 5000;
14+
const NODE_ENV = process.env.NODE_ENV || "production";
1415

1516
const __filename = fileURLToPath(import.meta.url);
1617
const __dirname = path.dirname(__filename);
@@ -21,8 +22,12 @@ app.use(express.json());
2122
app.use("/api/auth", authRoutes);
2223
app.use("/api/coolify", coolifyRoutes);
2324

24-
app.get("/api/health", (req, res) => {
25-
res.json({ status: "ok", message: "Server is running" });
25+
app.get("/healthz", (req, res) => {
26+
res.json({
27+
status: "ok",
28+
message: "Server is running",
29+
...(NODE_ENV !== "production" && { environment: NODE_ENV }),
30+
});
2631
});
2732

2833
app.use(express.static(path.join(__dirname, "../dist")));
@@ -35,4 +40,9 @@ app.use((err, req, res, _next) => {
3540
handleError(err, res);
3641
});
3742

38-
app.listen(PORT, () => {});
43+
app.listen(PORT, () => {
44+
if (NODE_ENV !== "production") {
45+
console.log(`Server running on http://localhost:${PORT}`);
46+
console.log(`Environment: ${NODE_ENV}`);
47+
}
48+
});

0 commit comments

Comments
 (0)