Skip to main content

Dockerfile

A Dockerfile is a text file of ordered instructions that Docker follows to build an image. Each instruction that changes the filesystem creates a layer, and the order controls both build speed and image contents.

What a Dockerfile does

Running docker build . reads the Dockerfile in the build context and produces an image. FROM selects the base, COPY and RUN add files and run commands, and CMD or ENTRYPOINT define what starts when a container runs.

Instructions you will use most

Dockerfile
FROM node:20-alpine # base image, pin the version
WORKDIR /app # working directory, created if missing
COPY package.json package-lock.json ./ # copy from build context
RUN npm ci --omit=dev # run a command, creates a layer
ENV NODE_ENV=production # runtime environment, never secrets
ARG BUILD_VERSION=unknown # build-time only, not for secrets
EXPOSE 8080 # documentation only, does not publish
USER node # run as non-root
HEALTHCHECK CMD wget -q --spider http://localhost:8080/health || exit 1
ENTRYPOINT ["node"] # fixed executable
CMD ["dist/server.js"] # default arguments, overridable
LABEL org.opencontainers.image.source="https://github.com/myorg/payment-api"

Prefer COPY over ADD for local files. ADD also extracts local tar archives and fetches URLs, which is rarely what you want.

Shell form versus exec form

Dockerfile
CMD node server.js # shell form: /bin/sh is PID 1, signals may not reach node
CMD ["node", "server.js"] # exec form: node is PID 1 and receives SIGTERM

Use exec form for CMD and ENTRYPOINT so graceful shutdown works.

Layer cache order

Put rarely changing steps first and frequently changing steps last, so edits to source code do not reinstall dependencies.

A production-ready example

Dockerfile
# syntax=docker/dockerfile:1
FROM node:20-alpine AS builder
WORKDIR /app
COPY package.json package-lock.json ./
RUN --mount=type=cache,target=/root/.npm npm ci
COPY . .
RUN npm run build
FROM node:20-alpine AS production
WORKDIR /app
ENV NODE_ENV=production
COPY package.json package-lock.json ./
RUN npm ci --omit=dev && npm cache clean --force
COPY --from=builder /app/dist ./dist
USER node
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=5s \
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1
CMD ["node", "dist/server.js"]

Troubleshooting

Problem Cause Fix
Build slow every time COPY . . before install Copy manifests first
SIGTERM ignored Shell form CMD Use exec form
Image too large Build tools in final image Multi-stage build
Secret in history ENV or ARG used BuildKit secret mount
Common Mistake

Cleaning package caches in a separate RUN. The earlier layer still holds the files. Install and clean up in the same instruction.

Security

Never put secrets in ENV or ARG. Use BuildKit --mount=type=secret instead.

Frequently Asked Questions

Why does instruction order affect build speed?

Docker caches each layer, but once one layer is invalidated all later layers rebuild. Copying dependency manifests and installing before copying source keeps the slow install layer cached across most edits.

What is the difference between CMD and ENTRYPOINT?

ENTRYPOINT defines the executable that always runs, while CMD supplies default arguments or a default command that docker run arguments can replace. They are often combined, with ENTRYPOINT as the program and CMD as its default arguments.

Does EXPOSE publish a port?

No. EXPOSE only documents the port. You must publish with -p or ports in Compose to reach it from the host.

Found a mistake, an outdated step or a broken link?