Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 22/ 160
02. Dockerfiles & Image Building

Anatomy of a Dockerfile & Build Context

Structure Dockerfiles and understand what the build context sends to the daemon.

BeginnerDockerfile · Context3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

The Dockerfile says what to build; the context is the box of source material the builder is allowed to open.

Think of it like…

A Dockerfile is a factory recipe; the build context is the loading dock inventory, and .dockerignore is the gate that rejects unnecessary crates.

Where you’ve seen it

In practice, docker build --file deploy/Dockerfile --tag acme/api:2.1 --progress=plain deploy is the concrete move for this job: structure Dockerfiles and understand what the build context sends to the daemon. The release is not accepted until use the BuildKit transfer summary to confirm context size and then inspect the resulting provenance/label metadata confirms the result.

Words you’ll hear

Dockerfile
The implementation control in docker build --file deploy/Dockerfile --tag acme/api:2.1 --progress=plain deploy for structure Dockerfiles and understand what the build context sends to the daemon.
Context
The result to observe with use the BuildKit transfer summary to confirm context size and then inspect the resulting provenance/label metadata, rather than infer from a successful command alone.
Instructions
The production boundary exposed when placing a Dockerfile under deploy but building with . while COPY expects deploy-relative paths, or exposing .git and .env in the context.

Visual diagram

Loading interactive diagram

Theory & deep dive

Dockerfile control path

The concrete mechanism is docker build --file deploy/Dockerfile --tag acme/api:2.1 --progress=plain deploy. The observable result is use the BuildKit transfer summary to confirm context size and then inspect the resulting provenance/label metadata; if those disagree, inspect the runtime evidence before changing unrelated settings.

Structure Dockerfiles and understand what the build context sends to the daemon. Keep Dockerfile, Context and Instructions in the same deployment record so the intent and the observed state cannot drift apart.

Concrete Dockerfile setup

docker build --file deploy/Dockerfile --tag acme/api:2.1 --progress=plain deploy

Key takeaway: Prove Context with use the BuildKit transfer summary to confirm context size and then inspect the resulting provenance/label metadata, not with command success alone.

Instructions failure boundary

Sending a repository root as context can upload secrets, dependencies and build artifacts even when the Dockerfile never copies them. A remote Git context changes cache identity and credential behavior, so build boundaries should be explicit rather than accidental. The narrow failure to design for is placing a Dockerfile under deploy but building with . while COPY expects deploy-relative paths, or exposing .git and .env in the context.

Accept this compromise explicitly: A small focused context is cacheable and safer; a broad context is convenient for shared monorepo files but needs strict ignore rules. The controlled check-use the BuildKit transfer summary to confirm context size and then inspect the resulting provenance/label metadata-also identifies the point where a different design is safer.

Deliberate failure test

Exercise this failure safely: placing a Dockerfile under deploy but building with . while COPY expects deploy-relative paths, or exposing .git and .env in the context.

Key takeaway: A small focused context is cacheable and safer; a broad context is convenient for shared monorepo files but needs strict ignore rules.

How it works, step by step

Step 1 of 4
Design

1. State the boundary

Write down why the workload needs this: Sending a repository root as context can upload secrets, dependencies and build artifacts even when the Dockerfile never copies them. A remote Git context changes cache identity and credential behavior, so build boundaries should be explicit rather than accidental.

The Dockerfile says what to build; the context is the box of source material the builder is allowed to open.
Key numbers at this step
Intent
Dockerfile

When to use it & trade-offs

What it is

A Dockerfile is an ordered build script, while the build context is the files and directories sent or made available to the builder. FROM selects a base, later instructions produce layers or configuration, and only files under the declared context are available to COPY.

Why it matters: Sending a repository root as context can upload secrets, dependencies and build artifacts even when the Dockerfile never copies them. A remote Git context changes cache identity and credential behavior, so build boundaries should be explicit rather than accidental.

When to reach for it

  • The fit is strongest for this job: structure Dockerfiles and understand what the build context sends to the daemon. Pair it with use the BuildKit transfer summary to confirm context size and then inspect the resulting provenance/label metadata in the runbook.
  • Production Dockerfile changes should make Context observable through docker build --file deploy/Dockerfile --tag acme/api:2.1 --progress=plain deploy, rather than trusting the runtime default.
  • Keep a rollback example for the case where placing a Dockerfile under deploy but building with . while COPY expects deploy-relative paths, or exposing .git and .env in the context.

When to avoid or be careful

  • Do not accept command success while the system can still fail by placing a Dockerfile under deploy but building with . while COPY expects deploy-relative paths, or exposing .git and .env in the context.
  • Avoid erasing Instructions protection merely to dodge this cost: “A small focused context is cacheable and safer; a broad context is convenient for shared monorepo files but needs strict ignore rules”.
  • Do not replace the evidence path-use the BuildKit transfer summary to confirm context size and then inspect the resulting provenance/label metadata-with a screenshot or a mutable tag.

docker build --file deploy/Dockerfile --tag acme/api:2.1 --progress=plain deploy: practical choices

Explicit Dockerfile
Best for: Structure Dockerfiles and understand what the build context sends to the daemon, with a reviewed runbook.

Strengths: A Dockerfile is an ordered build script, while the build context is the files and directories sent or made available to the builder. FROM selects a base, later instructions produce layers or configuration, and only files under the declared context are available to COPY.

Watch out: A small focused context is cacheable and safer; a broad context is convenient for shared monorepo files but needs strict ignore rules.

Ambient default
Best for: Short-lived Context experiments with low blast radius.

Strengths: It can begin with docker build --file deploy/Dockerfile --tag acme/api:2.1 --progress=plain deploy and fewer surrounding controls.

Watch out: It silently accepts placing a Dockerfile under deploy but building with . while COPY expects deploy-relative paths, or exposing .git and .env in the context, which makes the result hard to reproduce after promotion.

Managed Instructions
Best for: Teams that centralize Instructions policy across many workloads.

Strengths: Sending a repository root as context can upload secrets, dependencies and build artifacts even when the Dockerfile never copies them. A remote Git context changes cache identity and credential behavior, so build boundaries should be explicit rather than accidental.

Watch out: The platform adds an ownership and debugging layer; retain use the BuildKit transfer summary to confirm context size and then inspect the resulting provenance/label metadata as the direct check.

Interview signals & numbers to know

When this topic comes up

  • docker build --file deploy/Dockerfile --tag acme/api:2.1 --progress=plain deploy
  • use the BuildKit transfer summary to confirm context size and then inspect the resulting provenance/label metadata
  • Failure signature: placing a Dockerfile under deploy but building with . while COPY expects deploy-relative paths, or exposing .git and .env in the context
  • Trade-off: A small focused context is cacheable and safer; a broad context is convenient for shared monorepo files but needs strict ignore rules.

Benchmarks & rules of thumb

1 context root -
COPY sources cannot escape it
0 remote URLs -
needed for ordinary local context files
2 inputs -
Dockerfile instructions plus the transferred context

Common mistakes & follow-ups

Common interview mistakes

  • Treating a successful docker build --file deploy/Dockerfile --tag acme/api:2.1 --progress=plain deploy as proof and skipping use the BuildKit transfer summary to confirm context size and then inspect the resulting provenance/label metadata.
  • Placing a Dockerfile under deploy but building with . while COPY expects deploy-relative paths, or exposing .git and .env in the context
  • Optimizing Dockerfile without accepting that a small focused context is cacheable and safer; a broad context is convenient for shared monorepo files but needs strict ignore rules.

Likely interviewer follow-ups

  • Why does docker build --file deploy/Dockerfile --tag acme/api:2.1 --progress=plain deploy address Dockerfile here rather than through application code?
  • How would you distinguish healthy Context from the failure “placing a Dockerfile under deploy but building with . while COPY expects deploy-relative paths, or exposing .git and .env in the context” using use the BuildKit transfer summary to confirm context size and then inspect the resulting provenance/label metadata?
  • When would this trade-off-“A small focused context is cacheable and safer; a broad context is convenient for shared monorepo files but needs strict ignore rules”-justify a different Instructions design?
Interview tip: Give one concrete operation-docker build --file deploy/Dockerfile --tag acme/api:2.1 --progress=plain deploy-and one proof-use the BuildKit transfer summary to confirm context size and then inspect the resulting provenance/label metadata. Then name the boundary: placing a Dockerfile under deploy but building with . while COPY expects deploy-relative paths, or exposing .git and .env in the context.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Anatomy of a Dockerfile & Build Context with generative AI interview Q&A and hands-on practice.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
How Docker Images Work (Read-Only Layers & Copy-on-Write)
Next topic
Base Images (FROM, Official Images, scratch)