Express · Lesson 15 of 15
Deploy with Docker
A production Node image, health checks, graceful shutdown and env-based config.
- Advanced
- 15 min read
- 3 objectives
Before this lessonLesson 14: Logging and Request Ids
What you will learn
- Write a Dockerfile
- Handle SIGTERM
- Expose /health
Your Progress
0 of 15 lessons 0%
- Lessons0 / 15
- Completed0
- Est. time left~ 4 hours
Create a free account to keep your progress on every device.
Node in production is a small Docker image, a health endpoint, and a process that leaves the load balancer before it dies.
Dockerfile
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
USER node
EXPOSE 3000
CMD ["node", "src/server.js"]Graceful shutdown
const server = app.listen(process.env.PORT || 3000);
function shutdown() {
server.close(() => process.exit(0));
setTimeout(() => process.exit(1), 10_000).unref();
}
process.on("SIGTERM", shutdown);
process.on("SIGINT", shutdown);Health
app.get("/health", async (_req, res) => {
await pool.query("SELECT 1");
res.json({ status: "ok" });
});