What is a Dockerfile and what are its most important instructions?
Learn what a Dockerfile is and its most important instructions — FROM, WORKDIR, COPY, RUN, ENV, EXPOSE, CMD and ENTRYPOINT — plus layer caching tips.
Expected Interview Answer
A Dockerfile is a plain-text script of ordered instructions that Docker reads to build an image automatically and reproducibly, with each instruction typically creating a cached layer.
The most important instructions are FROM (the base image), WORKDIR (the working directory), COPY/ADD (bring files in), RUN (execute build-time commands like installing packages), ENV (environment variables), EXPOSE (document ports), and CMD/ENTRYPOINT (the default process to run). Order matters: because Docker caches layers, you place rarely changing steps (like dependency installs) before frequently changing ones (like copying source) to maximize cache hits and speed up rebuilds.
- Reproducible, version-controlled image builds
- Layer caching speeds up incremental rebuilds
- Self-documenting environment and dependencies
- Enables multi-stage builds for small final images
- Consistent images across dev, CI, and production
AI Mentor Explanation
A Dockerfile is like a captain's written match-preparation plan: pick the ground (FROM), set up the nets (WORKDIR), carry in the kit (COPY), run fitness drills (RUN), and name who opens the batting (CMD). Following the plan top to bottom builds an identical, ready team every time, and reusing unchanged early steps saves warm-up effort.
Step-by-Step Explanation
Step 1
Choose a base with FROM
Every Dockerfile starts from a base image, e.g. FROM node:20-alpine, which the rest of the build extends.
Step 2
Set the working directory
WORKDIR /app defines where subsequent COPY, RUN, and CMD instructions operate.
Step 3
Install dependencies first
COPY package files then RUN the installer before copying source, so dependency layers stay cached across code changes.
Step 4
Copy source and configure
COPY the application code, set ENV variables, and EXPOSE the ports the app listens on.
Step 5
Define the default process
Use ENTRYPOINT for the fixed executable and CMD for default arguments that start the container.
What Interviewer Expects
- Dockerfile defined as an ordered build script
- Knowledge of FROM, WORKDIR, COPY, RUN, ENV, EXPOSE, CMD/ENTRYPOINT
- Understanding of layer caching and instruction order
- Difference between CMD and ENTRYPOINT
- Awareness of multi-stage builds and small images
Common Mistakes
- Copying source before installing dependencies, busting the cache
- Confusing CMD with ENTRYPOINT
- Using ADD when a simple COPY would do
- Running apt-get update in a separate layer from install, causing stale packages
- Forgetting that EXPOSE only documents ports and does not publish them
Best Answer (HR Friendly)
“A Dockerfile is a recipe written in a text file that tells Docker step by step how to build an application's image. Key steps include choosing a starting point, copying in the code, installing what it needs, and stating the command that runs when the app starts.”
Code Example
# Base image
FROM node:20-alpine
# Working directory for later instructions
WORKDIR /app
# Install deps first so this layer caches across code changes
COPY package*.json ./
RUN npm ci --omit=dev
# Now copy the rest of the source
COPY . .
# Environment and documented port
ENV NODE_ENV=production
EXPOSE 3000
# Fixed executable + default args
ENTRYPOINT ["node"]
CMD ["server.js"]Follow-up Questions
- What is the difference between CMD and ENTRYPOINT?
- How does Docker's build cache decide which layers to reuse?
- When would you use a multi-stage build?
- What is the difference between COPY and ADD?
MCQ Practice
1. Which instruction must (in practice) begin a typical Dockerfile?
FROM sets the base image the rest of the build extends and is the first instruction of a standard Dockerfile stage.
2. Why copy package.json and install dependencies before copying the full source?
Ordering stable steps first maximizes layer cache hits, so changing source code does not force a reinstall of dependencies.
3. What does the EXPOSE instruction actually do?
EXPOSE is documentation only; you still need -p or ports mapping to publish the port to the host.
Flash Cards
What is a Dockerfile? — An ordered text script of instructions Docker reads to build an image reproducibly, with each instruction typically creating a layer.
Name the key Dockerfile instructions. — FROM, WORKDIR, COPY/ADD, RUN, ENV, EXPOSE, and CMD/ENTRYPOINT.
CMD vs ENTRYPOINT? — ENTRYPOINT sets the fixed executable; CMD provides default arguments (and can be overridden at run time).
Why does instruction order matter? — Docker caches layers, so placing stable steps before changing ones maximizes cache reuse and speeds rebuilds.