Docker & Deployment
๐ Covered in this chapter:
Dockerfile ยท Docker Compose ยท Health Checks ยท Cloud Deployment ยท CI/CD
Containerize a Node.js application with Docker and understand the essentials of deploying it to the cloud.
1Docker & Deployment โ What You'll Learn
Containerize a Node.js application with Docker and understand the essentials of deploying it to the cloud.
Here's everything this chapter covers, in the order you'll learn it:
- What Docker is
- Writing a Dockerfile for Node.js
- Choosing a Node.js base image
- Installing dependencies inside the image
- Building a lean production image
- Port mapping
- Docker Compose for multi-container setups (app + database)
- Health checks
- Cloud deployment options
- CI/CD pipelines
- Viewing container logs
- Monitoring a deployed app
- Graceful shutdown on container stop
2Working Example
๐ป Example: Docker & Deployment
Dockerfile
โถ Run in Compiler
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
CMD ["node", "src/server.js"]
3Best Practices & Common Pitfalls
๐ก Key things to remember:
- Use npm ci (not npm install) in Docker builds โ it installs exactly what's in package-lock.json, giving reproducible builds.
- Use a small base image like node:22-alpine to keep your production image size (and attack surface) minimal.
โ Frequently Asked Questions (FAQ)
Q What's the most important thing to understand about docker & deployment?
Focus on: Dockerfile ยท Docker Compose ยท Health Checks ยท Cloud Deployment ยท CI/CD. These are the core building blocks this chapter's examples are built around, and they show up repeatedly in later chapters of this course.
Q Do I need external npm packages for docker & deployment?
Only where explicitly shown in the code examples above (like Express, Zod, or Socket.IO) โ otherwise, this chapter relies entirely on Node.js's own built-in capabilities.