Docker · Containers in practice
What is the Docker build context and why does it matter?
Short answer
The build context is the set of files sent to the builder. A large context slows every build and can leak files, so keep it small and point Docker at the right directory.
When you run docker build ., the client packages the current directory and sends it to the daemon or BuildKit. COPY and ADD can only read from that context. A context containing node_modules, .git or large datasets makes every build slower even if those files are not copied.
docker build -f docker/Dockerfile -t api:1.0.0 .
docker build -t api:1.0.0 https://github.com/example/api.git#main
docker build -t api:1.0.0 - < DockerfileKeeping the context under control
- Add a .dockerignore for node_modules, .git, build output and local env files
- Build from the smallest directory that contains what you copy
- Use -f to keep the Dockerfile outside the context root when useful
- A remote Git context can be used in CI without a local checkout
The context is also a data boundary. A careless COPY . . can bake credentials, test fixtures or customer data into an image layer that is hard to remove later.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why is a large build context a problem even if nothing is copied?
- 2What is the difference between -f and the context argument?
- 3How do you build from a remote Git repository?
- 4What files should never be in the context?
Say it in the interview
The build context is everything the client sends to the builder, and COPY can only read from it. A context full of node_modules or .git slows every build and risks baking in secrets, so I keep it small with .dockerignore and build from the narrowest directory possible. I also remember that -f only changes which Dockerfile is read, not the context.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Docker mock interview and get scored feedback on depth, clarity and confidence.