Docker basics for Node
A Docker image packages your app and its dependencies so it runs the same everywhere.
In this page:
Syntax
FROM node:version
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
CMD ["node", "app.js"]
Docker basics for Node
A Dockerfile starts from a Node base image, copies package files, runs npm ci, copies the source and sets the start command. Copying package.json first lets Docker cache the dependency layer.
Use a .dockerignore for node_modules and secrets and run as a non-root user.
Note:
Use a slim or alpine base image and a multi-stage build to keep images small.
Example: Docker basics for Node
# Dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
USER node
EXPOSE 3000
CMD ["node", "server.js"]
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
Common Mistakes
- Copying node_modules into the image
- Running as root
- Installing dev dependencies in production
Chapter Summary
- Dockerfile describes the image
- Copy package files first for caching
- Use .dockerignore
- Run as non-root
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: