Node.js › Docker & Deployment

What is a Dockerfile, and what are the typical steps involved in containerizing a Node.js application?

Beginner

Answer

A Dockerfile is a text file with step-by-step instructions for building a Docker image -- for a Node.js app, this typically means starting from an official Node base image, setting a working directory, copying package.json and package-lock.json first (to leverage Docker's layer caching), installing dependencies, copying the rest of the application code, exposing the port the app listens on, and specifying the startup command.
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
Real-world example A team containerizes their Express API with this exact pattern, noticing that copying package.json before the rest of the code means Docker only re-runs the slow 'npm ci' step when dependencies actually change, not on every single code edit.

Common follow-ups: Why does the order of COPY instructions in a Dockerfile matter for build speed?;What's the difference between the 'node' and 'node:alpine' base images in terms of size and compatibility?