Skip to content

Dockerfile

A Dockerfile is a declarative recipe of instructions that Docker reads to build a container image, declaring base, deps, files, user and entrypoint.

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

  1. Pin the base image

    FROM ubuntu:24.04@sha256:... not ubuntu:latest. Latest moves; digests do not.

  2. Multi-stage always

    Build in one stage, COPY --from=builder only the artifact. Cuts image size by 80-95%.

  3. Non-root user

    USER appuser. Root in the container shares the host kernel capabilities.

  4. Group RUN commands

    RUN apt-get update && apt-get install -y X && rm -rf /var/lib/apt/lists/* = one layer, minimal size.

  5. `.dockerignore`

    Exclude node_modules, .git, *.log. A copy that pulls .git multiplies the image 10×.

  6. 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
2013 · Dockerfile nace con Docker
2014 · Sintaxis estable en Docker 1.0
2017 · Multi-stage builds (Docker 17.05)
2019 · BuildKit se vuelve default
2024 · Dockerfile frontend 1.x

Evidence

FAQ

CMD vs ENTRYPOINT?

ENTRYPOINT is the command; CMD is its default arguments. ENTRYPOINT ["node"], CMD ["app.js"] → runs node app.js. Override at runtime with docker run img other.js.

ADD vs COPY?

COPY copies files. ADD copies + auto-extracts local tarballs + fetches URLs. Prefer COPY; use ADD only when you need its magic.

How does multi-stage work?

Multiple FROM blocks. Each is a stage. COPY --from=builder /app/dist /app copies from one stage to another. Only the last stage is the final image.

Do I need a `.dockerignore`?

Yes. Without it, COPY . . includes node_modules, .git and 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

Dockerfilecontainersimage-builDockerContainerContainer Container Kubernetes

● broader ● narrower ● related

Used in the garden

No mentions yet.

Sources

Etymology

Origin: compound

  • docker (noun): port worker; metaphor for software that loads containers
  • file (noun): from Latin filum, "thread, line"

Semantic network

Dockerfilecontainersimage-builDockerContainerContainer Container Kubernetes

● broader ● narrower ● related

Sources