Testing, Performance & Deployment
Node.js Docker Deployment
A Docker image packages the application with its Node version and dependencies so it runs identically everywhere. A multi-stage build keeps the production image small and free of build tooling.
What is Docker Deployment in Node.js?
A Docker image packages the application with its Node version and dependencies so it runs identically everywhere. A multi-stage build keeps the production image small and free of build tooling.
Docker Deployment example
Output
FROM node:22-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
FROM node:22-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY --from=deps /app/node_modules ./node_modules
COPY . .
USER node # never run as root
EXPOSE 5000
CMD ["node", "src/index.js"]Key points to remember
- Copy package files and install before copying source, so the dependency layer caches.
- Use an alpine or slim base image to keep the image small.
- Run as a non-root user.
- Add a .dockerignore covering node_modules, .git and .env.
- Handle SIGTERM, or docker stop kills the process abruptly.
Common mistakes with Docker Deployment
- Copying the whole project before npm ci, invalidating the cache on every code change.
- Baking secrets into the image instead of passing them at runtime.
Node.js Docker Deployment— Interview Questions & FAQs
Why copy package.json before the rest of the code in a Dockerfile?+
Docker caches layers. Installing dependencies in its own layer means a source-code change does not trigger a full reinstall, which cuts build times dramatically.
