← Back to Node.js Course | Chapter 10: Deployment & Best Practices | Lesson 4 of 7

Docker basics for Node

A Docker image packages your app and its dependencies so it runs the same everywhere.

In this page:

  1. Docker basics for Node
Syntax
javascript
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

bash
# 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
  1. Copying node_modules into the image
  2. Running as root
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.