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
FROM node:20-alpine # base image, pin the versionWORKDIR /app # working directory, created if missingCOPY package.json package-lock.json ./ # copy from build contextRUN npm ci --omit=dev # run a command, creates a layerENV NODE_ENV=production # runtime environment, never secretsARG BUILD_VERSION=unknown # build-time only, not for secretsEXPOSE 8080 # documentation only, does not publishUSER node # run as non-rootHEALTHCHECK CMD wget -q --spider http://localhost:8080/health || exit 1ENTRYPOINT ["node"] # fixed executableCMD ["dist/server.js"] # default arguments, overridableLABEL 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
CMD node server.js # shell form: /bin/sh is PID 1, signals may not reach nodeCMD ["node", "server.js"] # exec form: node is PID 1 and receives SIGTERMUse 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
# syntax=docker/dockerfile:1FROM node:20-alpine AS builderWORKDIR /appCOPY package.json package-lock.json ./RUN --mount=type=cache,target=/root/.npm npm ciCOPY . .RUN npm run build FROM node:20-alpine AS productionWORKDIR /appENV NODE_ENV=productionCOPY package.json package-lock.json ./RUN npm ci --omit=dev && npm cache clean --forceCOPY --from=builder /app/dist ./distUSER nodeEXPOSE 8080HEALTHCHECK --interval=30s --timeout=5s \ CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1CMD ["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 MistakeCleaning package caches in a separate
RUN. The earlier layer still holds the files. Install and clean up in the same instruction.
SecurityNever put secrets in
ENVorARG. Use BuildKit--mount=type=secretinstead.
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.