Docker for Node Developers: Dockerfile, Compose and Small Images

Node.js and Express: Build a Back End API from Scratch · Testing, Debugging and Deployment · 5 min read · Updated

Image to container

  1. The Dockerfile lists the steps: base image, install packages, copy code.
  2. The build produces an image. A multi-stage build keeps build tools out of it.
  3. A container is one running copy of the image, with limited rights.
  4. Compose starts the app together with its database and cache on a private network.

"It works on my machine" is a classic problem. The server has a different Node version, a missing package or a different setting. Docker removes the problem by packaging your app together with everything it needs, into an image. A container is a running copy of an image. The same image runs the same way on your laptop, on a colleague's laptop and on a server, which is why almost every company uses containers in some form.

Words to know

  • Image: a read-only package: an operating system layer, Node, your code and its dependencies.
  • Container: a running instance of an image. You can start many from one image.
  • Dockerfile: a text file with the recipe to build an image.
  • Registry: a place that stores images, such as Docker Hub or the registry of your cloud.
  • Volume: storage that outlives a container, used for database files.
  • Compose: a tool that starts several containers together from one file.

A good Dockerfile

# --- build stage: install all dependencies ---
FROM node:22-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev

# --- run stage: small image with only what is needed ---
FROM node:22-alpine
ENV NODE_ENV=production
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# never run as root
USER node
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s CMD wget -qO- http://127.0.0.1:3000/api/health || exit 1
CMD ["node", "src/server.js"]

This file uses a multi-stage build. The first stage installs the production dependencies. The second stage starts again from a clean small image and copies in only the result, so build tools do not end up in the final image. Several details matter. node:22-alpine is a small base image. npm ci --omit=dev installs exactly what the lock file says and leaves out the dev tools. COPY package*.json comes before COPY . ., so Docker can reuse the cached layer of installed packages when only your code changed, which makes rebuilds fast. USER node runs the app without root rights, which limits the damage if the app is attacked. The HEALTHCHECK lets Docker see whether the app is alive.

Also create a .dockerignore file so that node_modules, .git and especially .env are never copied into the image. A secret inside an image can be read by anyone who gets the image.

# .dockerignore
node_modules
.git
.env
*.log
coverage

Compose: app, database and cache together

services:
  api:
    build: .
    ports:
      - "3000:3000"
    environment:
      MONGODB_URI: mongodb://mongo:27017/app
      REDIS_HOST: redis
      JWT_SECRET: ${JWT_SECRET}       # comes from your shell or a .env file, never written here
    depends_on:
      - mongo
      - redis
    restart: unless-stopped

  mongo:
    image: mongo:7
    volumes:
      - mongo-data:/data/db           # keeps the data when the container is replaced
    restart: unless-stopped

  redis:
    image: redis:7-alpine
    restart: unless-stopped

volumes:
  mongo-data:

Compose starts three containers. They can reach each other by service name: inside the app container, the database address is mongo, not localhost. The volumes entry stores the database files outside the container, so removing the container does not delete your data. The secret JWT_SECRET is read from your shell or a .env file, not written in the file.

  1. Build and start everything: docker compose up --build. Add -d to run in the background.
  2. See the logs with docker compose logs -f api.
  3. Stop with docker compose down. Add -v only if you really want to delete the data volume.
  • Pin a major Node version in FROM, and update it on purpose.
  • Never put secrets in the Dockerfile or the image. Pass them at run time.
  • Keep one process per container, and log to the console, because Docker collects it.
  • Scan images with a tool such as docker scout or Trivy.
  • Do not store uploads or database files inside a container without a volume.

Your task

  1. Build the image for your own project and compare its size with a version that uses the full node:22 image.
  2. Add a .env.example file that lists the variables without real values.
  3. Change the Compose file so that it also starts an nginx container in front of the app.

How to check it worked

  • The image runs as the node user and not as root.
  • Changing one source file does not reinstall every package on rebuild.
  • Your .env file is listed in .dockerignore.

Frequently asked questions

What is the difference between an image and a container?

An image is the packaged recipe. A container is a running instance of it. You can start many containers from one image.

Where should I keep database data?

In a volume. Anything written inside a container is lost when the container is removed.

Do I need Docker to deploy Node?

No. A VPS with PM2 works well, as in the previous lesson. Docker helps with identical environments and with larger teams.

Practise with interview questions