Express.js Docker

Docker packages an app together with everything it needs to run. The package is called a container. A container holds the Node.js version, the installed packages, and the app code. The same container runs the same way on a laptop, a test server, and a cloud platform. Docker ends the familiar complaint that an app works on one machine and fails on another.

The Lunch Box Analogy

A lunch box holds a complete meal, sealed and ready. Carry it to school, to the office, or to a park, and the meal stays the same. A Docker container is a sealed lunch box for an app. The image is the recipe that fills the box. Each container is one box made from that recipe.

Key Terms

TermMeaning
ImageA read-only template that holds the app and its tools
ContainerA running copy of an image
DockerfileA text file with the steps to build an image
RegistryAn online store for images, such as Docker Hub
VolumeStorage that survives when a container stops

Docker Workflow Diagram

Dockerfile
    |  docker build
    v
Image  ---- docker push ---->  Registry
    |                              |
    | docker run                   | docker pull
    v                              v
Container on laptop         Container on server

Prepare a Sample App

// server.js
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.json({ message: 'Hello from Docker' });
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log('Listening on ' + PORT));

The package.json file needs a start script that runs node server.js.

Write the Dockerfile

FROM node:20-alpine

WORKDIR /app

COPY package*.json ./
RUN npm ci --omit=dev

COPY . .

EXPOSE 3000
CMD ["npm", "start"]

Line-by-Line Meaning

FROM

This line picks the base image. The alpine version of Node.js stays small, which speeds up downloads.

WORKDIR

This line sets the folder inside the container where the next commands run.

COPY package files, then RUN npm ci

Copying the package files first lets Docker cache the installed packages. Docker rebuilds that layer only when the dependency list changes, so later builds finish faster.

COPY the code

This line copies the project files into the image after the packages install.

EXPOSE and CMD

EXPOSE documents the port. CMD names the command that starts the app.

Add a .dockerignore File

This file keeps unwanted files out of the image. A smaller image builds and ships faster.

node_modules
npm-debug.log
.env
.git

Build and Run

docker build -t my-express-app .
docker run -p 3000:3000 --env-file .env my-express-app

The -t flag names the image. The -p flag maps port 3000 on the computer to port 3000 in the container. The --env-file flag passes secrets at run time so they stay out of the image. Open http://localhost:3000 to see the reply.

Run the App With a Database Using Compose

Docker Compose starts several containers from one file. The example below runs the Express app and a MongoDB database together.

services:
  api:
    build: .
    ports:
      - "3000:3000"
    environment:
      - MONGO_URI=mongodb://mongo:27017/mydb
    depends_on:
      - mongo

  mongo:
    image: mongo:7
    volumes:
      - mongo-data:/data/db

volumes:
  mongo-data:

Start everything with docker compose up. The api container reaches the database through the service name mongo.

Best Practices

  • Pin the Node.js version in the FROM line.
  • Run the app as a non-root user for extra safety.
  • Keep secrets in environment variables, never in the Dockerfile.
  • Use volumes for data that must survive restarts.
  • Rebuild images often to receive security patches.