Dockerfile
A text file of instructions, such as FROM, COPY and RUN, that tells Docker how to build an image.
In more detail
Docker reads it top to bottom: FROM picks a base image, later instructions add files, run commands or set metadata, and each file-changing step becomes a layer. The order matters, because a change in one step invalidates the build cache for every step after it.
Examples
FROM python:3.12-alpine
WORKDIR /app
COPY app.py .
CMD ["python", "app.py"]A three-stage Dockerfile: deps installs from the lockfile, builder compiles, runner copies only the output into a minimal image.FROM golang:alpine AS build
RUN CGO_ENABLED=0 go build -o /out/app .FROM eclipse-temurin:21-jdk AS buildRUN pip install --no-cache-dir --quiet kafka-python==2.2.15The official Next.js with-docker example uses deps, builder and runner stages on node:alpine.Related terms
- docker build — The command that builds an image from a Dockerfile and a build context.
- FROM — The Dockerfile instruction that picks the base image a build starts from.
- ENTRYPOINT — The Dockerfile instruction that sets the program a container always runs, with CMD supplying its default arguments.
- instruction — One line of a Dockerfile, written as an upper-case keyword followed by its arguments, such as COPY app.py .
- build context — The set of files you hand to docker build, usually the current directory given as the final dot.
- build cache — Saved results of earlier build steps that a build tool, such as Docker, reuses when nothing a step depends on has changed.
- layer — One read-only slice of an image that records the files a single build step added, changed or deleted.
- multi-stage build — A Dockerfile with several FROM stages, where later stages copy only what they need from earlier ones.
- COPY — The Dockerfile instruction that copies files from the build context into the image.
- USER — The Dockerfile instruction that sets which user later instructions and the container run as.
- image — A read-only package containing a program and everything it needs to run, used as the template for containers.
- Containerfile — Another name for a Dockerfile, used by tools such as Podman and Buildah; the contents and instructions are the same.
- Docker — A platform for building, shipping and running software in containers.
- base image — The image a Dockerfile starts from with FROM, which supplies the operating system files and often a language runtime.
Related guides
Taught in
- Scanning Code, Dependencies And Containers — Microsoft Azure DevOps Engineer Expert (AZ-400) Exam Prep
- Workflows, Jobs, Steps, and Actions — Continuous Integration & Continuous Delivery Pipelines
- Building and Pushing Container Images — Continuous Integration & Continuous Delivery Pipelines
- Container Images The Exam Way — Certified Kubernetes Application Developer (CKAD) Exam Prep
- Design And Build: Timed Drill And Checkpoint — Certified Kubernetes Application Developer (CKAD) Exam Prep
- ConfigMaps And Secrets — Certified Kubernetes Application Developer (CKAD) Exam Prep
- Final Review: Where Your Marks Are At Risk — Certified Kubernetes Application Developer (CKAD) Exam Prep
- Secrets In Use: Leaks Through Env, Specs And Images — Certified Kubernetes Security Specialist (CKS) Exam Prep
- Minimal Base Images: Less To Attack — Certified Kubernetes Security Specialist (CKS) Exam Prep
- Container Hardening: Distroless & Rootless — Cloud Security & DevSecOps Engineering
- Union Filesystems, Images And Writable Layers — Docker Architecture & Production Containers
- Docker, OCI, The Engine And The CLI — Docker Architecture & Production Containers
- Run The Spine Service And Publish Its Port — Docker Architecture & Production Containers
- Build Context, Dockerignore And Your First Dockerfile — Docker Architecture & Production Containers
- FROM, WORKDIR, COPY And ADD — Docker Architecture & Production Containers
- RUN, SHELL And Efficient Layer Caching — Docker Architecture & Production Containers
- ARG, ENV, LABEL, EXPOSE And VOLUME — Docker Architecture & Production Containers
- CMD, ENTRYPOINT, USER And Process Signals — Docker Architecture & Production Containers
- Multi Stage Builds, Targets And Build Secrets — Docker Architecture & Production Containers
- Tags, Digests And Reproducible Image References — Docker Architecture & Production Containers
- Authenticate, Tag, Push And Pull From A Registry — Docker Architecture & Production Containers
- Minimal Bases, Non Root Users And Smaller Images — Docker Architecture & Production Containers
- Scan, Patch And Inspect The Software Supply Chain — Docker Architecture & Production Containers
- Bind Mounts, Permissions And Backups — Docker Architecture & Production Containers
- Port Publishing, Localhost And Network Troubleshooting — Docker Architecture & Production Containers
- Environment, Secret Files And Runtime Restrictions — Docker Architecture & Production Containers
- Define The Service And Database With Compose — Docker Architecture & Production Containers
- Health Checks, Dependencies And Reliable Startup — Docker Architecture & Production Containers
- Development Overrides, Hot Reloading And Debuggers — Docker Architecture & Production Containers
- Harden And Deliver The Complete Service Stack — Docker Architecture & Production Containers
- From Local Containers To Kubernetes — Docker Architecture & Production Containers
- Containerization Architecture: Multi-Stage Dockerfiles — Production Full-Stack Engineering
- The Container Registry: Build, Tag, Push, Clean Up — GitLab Certified CI/CD Associate Exam Prep
- Adding GitLab's Security Scanners To A Pipeline — GitLab Certified CI/CD Associate Exam Prep
- Installing the Go Toolchain — Concurrent Systems Programming with Go
- Shipping in Scratch and Distroless Images — Concurrent Systems Programming with Go
- Packaging Into A Container — Modern Java: Virtual Threads & High-Throughput Services
- A Producer In Python — Apache Kafka & Event Streaming
- Pin It, Lint It, Prove It: Images And Runtimes — Kubernetes and Cloud Native Associate (KCNA) Exam Prep
- Pinned For Stability: Images, Registries And Code — Kubernetes and Cloud Native Security Associate (KCSA) Exam Prep
- Threat Model Sprint: Name It, Then Stop It — Kubernetes and Cloud Native Security Associate (KCSA) Exam Prep
- Containerized Deployment: Multi-Stage Docker Builds — Next.js 15 & Modern App Router Architecture
- Production Hardening: Graceful Shutdown & Docker — Node.js Internals & Backend Services
- Container Security: Minimal Bases And Non Root — Application Security & Threat Modeling for Engineers
- Works On My Laptop: Images, Containers, Volumes And Networks — CompTIA Linux+ (XK0-006) Exam Prep
References
- Dockerfile reference
- Dockerfile overview (Docker docs)
- Docker: Building best practices
- Docker docs: Multi-stage builds
Certification courses
- AZ-400 course and practice exam: Microsoft Azure DevOps Engineer Expert
- CKAD course and practice exam: Certified Kubernetes Application Developer
- CKS course and practice exam: Certified Kubernetes Security Specialist
- GitLab CI/CD Associate course and practice exam: GitLab Certified CI/CD Associate
- KCNA course and practice exam: Kubernetes and Cloud Native Associate
- KCSA course and practice exam: Kubernetes and Cloud Native Security Associate
- CompTIA Linux+ course and practice exam: XK0-006