What is a Dockerfile
A Dockerfile is a text file containing instructions for building a container image. Each instruction is executed in order, and each one that modifies the filesystem creates a new layer.
How it works
Key instructions:
FROM— sets the base image. Every Dockerfile starts with FROM.FROM ubuntu:22.04starts from Ubuntu.FROM scratchstarts from an empty filesystem.RUN— executes a command during the build and saves the result as a layer.RUN apt-get install -y nginxinstalls nginx into the image.COPY— adds files from the build context into the image.COPY app.js /app/creates a layer with that file.CMD— sets the default command when the container starts. Can be overridden at runtime.ENTRYPOINT— sets the executable that always runs. CMD provides default arguments to it.ENV— sets environment variables.EXPOSE— documents which ports the container listens on (does not actually publish them).WORKDIR— sets the working directory for subsequent instructions.
docker build processes the Dockerfile, executes each instruction, and produces an image. Layer caching means unchanged instructions reuse cached layers — only changed instructions (and everything after them) are rebuilt.
Why it matters
Dockerfiles are the standard way to define reproducible container builds. They encode the exact steps to go from a base OS to a running application. Multi-stage builds (multiple FROM instructions) separate build dependencies from runtime, producing minimal production images.
See How Container Images Work for layer caching strategies and image optimization.