"It works on my machine" is the line Docker came to retire. You package the app with the exact version of the runtime, the libraries and the configuration, and the same package runs on the laptop, in CI and on the server.
This article starts with what a container really is, moves through a well-written Dockerfile (the difference between a 2-second build and a 2-minute one), Compose for development, and ends with what to watch in production. Versions checked on November 21, 2026: Docker Engine 29.10.0, Docker Desktop 4.100.0 and Compose v5.8.0.

What a Container Really Is
The Docker documentation puts it plainly: a container is a regular process on your computer, like a Node.js server, running in isolation.
The isolation comes from two Linux kernel features:
- Namespaces make the process think it has the system to itself: its own process tree, network, file system and hostname. One container can't see another's processes.
- cgroups limit and measure resource usage: how much memory, CPU and disk each group of processes can use.
The difference from a VM is the kernel. Each virtual machine boots a full operating system with its own kernel. A container doesn't: it shares the host's kernel, so there's no system to boot. On Mac and Windows, Docker Desktop runs a Linux VM underneath, and the containers live inside it.
One consequence few people remember: namespace isolation is thinner than a VM's. Containers help a lot, but they're not as strong a security boundary as a separate machine.
Image, Container, Registry: The Vocabulary
| Term | What it is | Analogy |
|---|---|---|
| Image | Immutable, layered package with everything the app needs | A frozen meal in a sealed container |
| Container | An image running, with its own writable layer | The meal heated up and served |
| Dockerfile | The instruction file that produces the image | The recipe |
| Registry | Service that stores images (Docker Hub, GHCR, ECR) | The restaurant freezer |
| Volume | Storage managed by Docker that outlives the container | The pantry, outside the meal container |
You can start as many containers as you want from the same image, and what a container changes doesn't touch the image.
The First Dockerfile
A simple Node app:
FROM node:24-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY . .
USER node
EXPOSE 3000
CMD ["node", "src/index.js"]docker build -t my-api:1.0.0 .
docker run --rm -p 3000:3000 my-api:1.0.0The . at the end of build is the context: the folder whose files the Dockerfile can copy. Create a .dockerignore from day one:
node_modules
.git
.env
dist
Without it, node_modules and .git go into the build (slow), and .env can end up inside the image (dangerous).
Layers and Cache: The Golden Rule
Each instruction that changes files creates a layer, a list of files added, changed or removed. The image is the stack of those layers. Docker caches each one and reuses it on the next build.
The rule in the docs is short: when a layer changes, every layer after it is rebuilt, even if the result would be the same.
That's why the example copies package.json before everything else. Compare:
# Bad: any code change reinstalls the dependencies
COPY . .
RUN npm ci
# Good: dependencies are reinstalled only when package.json changes
COPY package.json package-lock.json ./
RUN npm ci
COPY . .The right order: what rarely changes on top (system, dependencies), what always changes at the bottom (your code).
Multi-stage: Build with Everything, Ship Only What's Needed
To compile TypeScript or build the front end, you need a compiler, dev dependencies and tools that are useless in production. A multi-stage build separates the two: one stage builds, the other receives only the result.
# Stage 1: build
FROM node:24 AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build && npm prune --omit=dev
# Stage 2: only what runs in production
FROM node:24-slim
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build --chown=node:node /app/node_modules ./node_modules
COPY --from=build --chown=node:node /app/dist ./dist
COPY package.json ./
USER node
EXPOSE 3000
CMD ["node", "dist/index.js"]In the official example in the Docker docs, a Node app with front end and back end dropped from 1.87 GB to 401 MB with this technique alone. A smaller image means faster downloads, faster deploys and less surface for vulnerabilities.
You can go further with minimal base images: Google's distroless images have no shell and no package manager, and Docker Hardened Images, free and open source since December 2025, already come non-root and with a signed SBOM.
The PID 1 Detail and docker stop
When you run docker stop, Docker sends a SIGTERM to the main process and waits (10 seconds by default) before killing it with SIGKILL. The app should use that time to finish in-flight requests and close connections.
That only works if the signal reaches the app. Two common mistakes block it:
CMDin shell form (CMD node src/index.js): the command runs inside/bin/sh -c, which doesn't forward signals. Use the JSON form:CMD ["node", "src/index.js"].npm startas the command: npm sits in the middle and doesn't forwardSIGTERMproperly. The official Node image best practices say to callnodedirectly.
There's one more detail: Node wasn't built to run as PID 1, the initial process that Linux treats in a special way. The fix is docker run --init (or init: true in Compose), which puts a minimal process in front to forward signals and reap orphan processes.
Data: Volumes and Bind Mounts
Whatever the container writes to its own layer is gone when it's removed. To keep data:
| Type | How it works | When to use it |
|---|---|---|
| Volume | Area managed by Docker, outlives the container | Database data in development, uploads |
| Bind mount | Links a host folder to a container folder | Source code in development |
| tmpfs | In memory, never goes to disk | Sensitive temporary files |
docker run -d -v pg-data:/var/lib/postgresql/data postgres:18Compose: Several Containers at Once
A real app has an APIContract that defines how one program asks another for data or actions: which endpoints to call, what to send and what comes back. On the web, usually HTTP with JSON., a database, Redis. Docker Compose describes everything in a compose.yaml file (the preferred name today; the version: field at the top is obsolete and only triggers a warning):
services:
api:
build: .
ports:
- "3000:3000"
environment:
DATABASE_URL: postgres://app:app@db:5432/app
depends_on:
db:
condition: service_healthy
init: true
db:
image: postgres:18
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: app
POSTGRES_DB: app
volumes:
- pg-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 10s
retries: 5
start_period: 30s
volumes:
pg-data:Notice db:5432 in the URL: Compose creates its own network where containers find each other by service name. That doesn't happen on Docker's default network.
And notice the healthcheck. The docs are clear: without a condition, Compose waits for the container to be running, not ready. Postgres takes a few seconds to accept connections, and an API that starts first crashes with "connection refused".
For day-to-day development, docker compose watch syncs saved code into the container or rebuilds when package.json changes.

Production: What Changes
Pin the version. The latest tag is just the name used when you don't specify one; it doesn't mean "stable" and can change at any time. Use explicit versions (node:24.11-slim) and, for full reproducibility, the digest (@sha256:...), with Dependabot or Renovate keeping it updated.
Don't run as root. The docs recommend a non-root USER whenever the service can run without privileges. The official Node image already ships with the node user.
Secrets don't go in the image. ARG and ENV are recorded in the image history (docker history shows them). If the build needs a token (to download a private package, for example), use a build secret:
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm cidocker build --secret id=npmrc,src=$HOME/.npmrc .At runtime, the secret comes from the platform's environment (injected variable, secrets manager), never from the Dockerfile.
Limit resources. By default, a container has no limit and can consume all the server's memory. Use --memory and --cpus (or deploy.resources.limits in Compose).
Scan the image. docker scout cves my-api:1.0.0 or trivy image my-api:1.0.0 list the known vulnerabilities in the packages inside it.
Watch the Docker Hub limit. Anonymous users can make 100 pulls every 6 hours per IP. In CI with shared runners, log in or use a mirror.
License: Is Docker Free?
Docker Engine, on Linux, is open source (Apache 2.0) and free for any use. Docker Desktop, the app for Mac and Windows, is free only for personal use, education, open source and companies with fewer than 250 employees and less than $10 million in annual revenue. Go past either limit and you need a subscription.
Compatible alternatives exist: Podman (6.2) runs the same images, because they all follow the open OCI standard, and there are options like Rancher Desktop and OrbStack for Mac.
Quick check
Frequently Asked Questions
Docker or Podman?
If you're starting out, Docker, because of the amount of material out there. Podman runs the same images and is a good option where the Docker Desktop license is a burden.
Do I need Kubernetes?
Not to get started. Docker with Compose handles a single server very well. Kubernetes makes sense when there are many machines and services to orchestrate.
Alpine or slim?
Alpine is smaller, but it uses musl instead of glibc, which sometimes breaks native dependencies. slim (trimmed-down Debian) is the safer choice for Node and Python.
How do I see what's inside an image?
docker history my-api:1.0.0 shows the layers and the size of each. To explore the files, docker run --rm -it my-api:1.0.0 sh (if the image has a shell).
Can I use Docker in production without Kubernetes?
Yes. Platforms like Coolify, Render and Fly.io, and Compose itself on a server, run containers in production. The CI/CD article shows how to automate the build and the deploy.
References
Checked on November 21, 2026:
- Docker, what is a container, what is an image, layers and build cache
- Docker, Dockerfile best practices, Dockerfile reference, multi-stage and build secrets
- Docker, storage, bridge network, resource constraints and security
- Docker Compose, application model, startup order and watch
- Docker, Desktop license, Docker Hub limits, Docker Scout and Docker Hardened Images
- Node.js, Docker image best practices
- Linux man-pages, namespaces(7) and cgroups(7)
- Open Container Initiative, Podman, distroless, Trivy