Dockerfile Basics
A Dockerfile is a text recipe describing how to build an image.
Introduction
A Dockerfile is a text recipe describing how to build an image. Each instruction creates a new layer; reading top-to-bottom shows exactly what's inside your final image.
Purpose of this lesson
This lesson teaches Dockerfile Basics as an engineering decision: what problem it solves, when to use it, how to implement it safely, and what signals tell you it is failing.
Understanding the topic
Use this whenever you create or review an application image. Build quality affects deploy speed, security exposure, reproducibility, CI cost, and how quickly engineers can diagnose failures.
Core concepts to understand:
FROM— base image.WORKDIR— set working directory (creates if missing).COPY/ADD— copy files into the image.RUN— execute a build-time command (e.g.apt-get install).ENV,EXPOSE,CMD,ENTRYPOINT— runtime metadata.
Visual explanation
Architecture or command flow to keep in mind:
# Dockerfile for a Node appFROM node:20-alpineWORKDIR /appCOPY package*.json ./RUN npm ci --omit=devCOPY . .EXPOSE 3000CMD ["node", "server.js"]
Step-by-step explanation
- Start from a trusted, pinned base image and document why it fits the runtime.
- Order Dockerfile instructions from least-changing dependency metadata to most-changing source files so the cache stays useful.
- Build locally with BuildKit, inspect layers with
docker history, and run the image with the same command CI will use. - Test shutdown behavior, health checks, non-root execution, and runtime configuration before pushing the image.
- Tag with an immutable version or git SHA, scan the image, and promote the same artifact through environments.
Informative example
Use the example below as a working baseline, then verify the runtime behavior instead of assuming the command or file is correct.
# Dockerfile for a Node appFROM node:20-alpineWORKDIR /appCOPY package*.json ./RUN npm ci --omit=devCOPY . .EXPOSE 3000CMD ["node", "server.js"]
A production-minded check usually includes docker ps, docker logs, docker inspect, and one validation from outside the container such as curl, a database connection, or a registry pull.
# Verification loop for Dockerfile Basicsdocker ps -adocker logs --tail=100 <container-name>docker inspect <container-or-image-name>docker system df
Real-world use
Every container image you'll ever pull was built from a Dockerfile (or equivalent). Reading other people's Dockerfiles is the fastest way to learn idioms.
Enterprise use cases
In a mature engineering organization, Dockerfile Basics is documented as a repeatable pattern with approved base images, ownership labels, CI checks, security expectations, rollback notes, and troubleshooting commands. The difference between a tutorial and production practice is that every container decision must be observable, reviewable, and reversible.
Best practices
- One Dockerfile per service; commit it next to the code.
- Order layers from least- to most-frequently-changed (deps first, source last).
Common mistakes
- Using
ADDwhenCOPYsuffices (ADDhas surprising features).
Debugging tips
- Use
docker build --progress=plainwhen BuildKit output hides the failing build step. - Run the image with a temporary shell or overridden command to inspect files, users, environment, and entrypoint behavior.
- Check
docker historyfor accidental secrets, unexpectedly large layers, and cache-busting instructions.
Optimization strategies
- Copy dependency lock files before source files so dependency installation remains cached during code edits.
- Use multi-stage builds to keep compilers, package managers, test tools, and source maps out of runtime images.
- Adopt BuildKit cache mounts for npm, Maven, pip, Go, or cargo dependencies in CI.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1QuestionWhat does <code>FROM</code> do?+
Answer
2QuestionDifference between <code>COPY</code> and <code>ADD</code>?+
Answer
3QuestionWhy does instruction order matter?+
Answer
Hands-on exercise
Create a small lab for Dockerfile Basics: run the example, inspect the created Docker object, intentionally introduce one mistake, and record the command that reveals the failure. The goal is not just to make the happy path work; it is to build operational reflexes.
# Hands-on lab scaffoldmkdir -p docker-dockerfile-basics-labcd docker-dockerfile-basics-lab# Add the Dockerfile, compose.yml, or command from this lesson.# Then run one happy-path test and one broken-path test.docker versiondocker infodocker system df
Summary
Dockerfile Basics matters because Docker is not only a packaging tool; it is a runtime, build, networking, storage, and delivery workflow. Treat each lesson as a production habit: make it repeatable, inspectable, secure, and easy to debug.