Definition
What it is
A Dockerfile is not a shell script. It is a small DSL: each instruction (FROM, RUN, COPY, CMD, ENTRYPOINT, WORKDIR, ENV, EXPOSE, USER, HEALTHCHECK, ARG, LABEL, VOLUME) contributes one layer to the image. Docker builds the image layer by layer; layers are cached, and cache misses invalidate every subsequent layer.
Why it matters
A 2 GB image takes 30s to pull on a fast network; a 200 MB image takes 3s. A Dockerfile that runs apt-get update && apt-get install -y nodejs npm curl git in a single layer keeps 500 MB of tools the app never uses. A Dockerfile without USER runs as root on the host kernel. The recipe is the difference between a lightweight deploy and a security incident.
Multi-stage vs single-stage
Aspect | Single-stage | Multi-stage |
|---|---|---|
Final image size | Includes build tools (GBs) | Only runtime (MBs) |
Attack surface | Compilers, package managers | Runtime only |
Build cache reuse | Harder | Natural (one stage per concern) |
Complexity | Low | Moderate |
Best for | Dev/scratch images | Production images |
The build flow
graph LR DF[Dockerfile] --> F[FROM base] F --> R[RUN install] R --> C[COPY source] C --> B[Build stage] B --> M[Multi-stage copy] M --> IMG[Final image] style IMG fill:#E8F5E9
How to write one well
A Dockerfile that ages well
Pin the base image
FROM ubuntu:24.04@sha256:...notubuntu:latest. Latest moves; digests do not.Multi-stage always
Build in one stage,
COPY --from=builderonly the artifact. Cuts image size by 80-95%.Non-root user
USER appuser. Root in the container shares the host kernel capabilities.Group RUN commands
RUN apt-get update && apt-get install -y X && rm -rf /var/lib/apt/lists/*= one layer, minimal size.`.dockerignore`
Exclude
node_modules,.git,*.log. A copy that pulls.gitmultiplies the image 10×.HEALTHCHECK
HEALTHCHECK CMD curl -f http://localhost/ || exit 1. Kubernetes and CI rely on it.
Common failures
Origin
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
— Docker, *Dockerfile reference*, 2024
Evidence
FAQ
- CMD vs ENTRYPOINT?
ENTRYPOINTis the command;CMDis its default arguments.ENTRYPOINT ["node"],CMD ["app.js"]→ runsnode app.js. Override at runtime withdocker run img other.js.- ADD vs COPY?
COPYcopies files.ADDcopies + auto-extracts local tarballs + fetches URLs. PreferCOPY; useADDonly when you need its magic.- How does multi-stage work?
Multiple
FROMblocks. Each is a stage.COPY --from=builder /app/dist /appcopies from one stage to another. Only the last stage is the final image.- Do I need a `.dockerignore`?
Yes. Without it,
COPY . .includesnode_modules,.gitand logs. The image grows 10×.- How to reduce image size?
Alpine/distroless base, multi-stage, group RUN commands, clean apt caches in the same RUN.
Connections
● broader ● narrower ● related
Used in the garden
No mentions yet.
Sources
Docker (2024). Dockerfile reference. https://docs.docker.com/engine/reference/builder/
Docker (2024). Best practices for writing Dockerfiles. https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
OCI (2024). OCI Image Format Specification. https://github.com/opencontainers/image-spec
Etymology
Origin: compound
docker(noun): port worker; metaphor for software that loads containersfile(noun): from Latin filum, "thread, line"
Semantic network
● broader ● narrower ● related
Sources
Docker (2024). Dockerfile reference. https://docs.docker.com/engine/reference/builder/
Docker (2024). Best practices. https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
OCI (2024). OCI Image Format Specification. https://github.com/opencontainers/image-spec