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 26/ 160
02. Dockerfiles & Image Building

Setting Working Directories & Metadata (WORKDIR, LABEL, ARG)

Set WORKDIR, attach LABELs, and pass build-time ARG values cleanly.

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

In plain words

Use WORKDIR for stable paths, LABEL for provenance and ARG for controlled build inputs that are not secrets.

Think of it like…

WORKDIR is setting the factory workbench, LABEL is stamping the finished part, and ARG is a temporary assembly slip that may remain in paperwork.

Where you’ve seen it

In practice, ARG APP_VERSION=2.1.0; WORKDIR /opt/acme; LABEL org.opencontainers.image.version=$APP_VERSION is the concrete move for this job: set WORKDIR, attach LABELs, and pass build-time ARG values cleanly. The release is not accepted until docker image inspect app:2.1.0 --format {{json .Config.Labels}} and docker run --rm app:2.1.0 pwd confirms the result.

Words you’ll hear

WORKDIR
The implementation control in ARG APP_VERSION=2.1.0; WORKDIR /opt/acme; LABEL org.opencontainers.image.version=$APP_VERSION for set WORKDIR, attach LABELs, and pass build-time ARG values cleanly.
LABEL
The result to observe with docker image inspect app:2.1.0 --format {{json .Config.Labels}} and docker run --rm app:2.1.0 pwd, rather than infer from a successful command alone.
ARG
The production boundary exposed when putting an API token in ARG and claiming it is secret, or relying on RUN cd /opt/acme for later COPY and CMD steps.

Visual diagram

Loading interactive diagram

Theory & deep dive

WORKDIR control path

The concrete mechanism is ARG APP_VERSION=2.1.0; WORKDIR /opt/acme; LABEL org.opencontainers.image.version=$APP_VERSION. The observable result is docker image inspect app:2.1.0 --format {{json .Config.Labels}} and docker run --rm app:2.1.0 pwd; if those disagree, inspect the runtime evidence before changing unrelated settings.

Set WORKDIR, attach LABELs, and pass build-time ARG values cleanly. Keep WORKDIR, LABEL and ARG in the same deployment record so the intent and the observed state cannot drift apart.

Concrete WORKDIR setup

ARG APP_VERSION=2.1.0; WORKDIR /opt/acme; LABEL org.opencontainers.image.version=$APP_VERSION

Key takeaway: Prove LABEL with docker image inspect app:2.1.0 --format {{json .Config.Labels}} and docker run --rm app:2.1.0 pwd, not with command success alone.

ARG failure boundary

Relative cd state in a RUN shell cannot survive to the next instruction, so WORKDIR prevents fragile paths. ARG values can still appear in build metadata and cache keys, and a label without a standard key becomes hard for automation to query. The narrow failure to design for is putting an API token in ARG and claiming it is secret, or relying on RUN cd /opt/acme for later COPY and CMD steps.

Accept this compromise explicitly: Build arguments make variants easy but increase cache dimensions; environment variables are more appropriate for runtime settings. The controlled check-docker image inspect app:2.1.0 --format {{json .Config.Labels}} and docker run --rm app:2.1.0 pwd-also identifies the point where a different design is safer.

Deliberate failure test

Exercise this failure safely: putting an API token in ARG and claiming it is secret, or relying on RUN cd /opt/acme for later COPY and CMD steps.

Key takeaway: Build arguments make variants easy but increase cache dimensions; environment variables are more appropriate for runtime settings.

How it works, step by step

Step 1 of 4
Design

1. State the boundary

Write down why the workload needs this: Relative cd state in a RUN shell cannot survive to the next instruction, so WORKDIR prevents fragile paths. ARG values can still appear in build metadata and cache keys, and a label without a standard key becomes hard for automation to query.

Use WORKDIR for stable paths, LABEL for provenance and ARG for controlled build inputs that are not secrets.
Key numbers at this step
Intent
WORKDIR

When to use it & trade-offs

What it is

WORKDIR creates or changes the build’s current directory; LABEL stores image metadata; ARG accepts a build-time input only for available instructions and does not automatically persist as a runtime environment variable. WORKDIR should be absolute and LABEL keys should follow a namespace.

Why it matters: Relative cd state in a RUN shell cannot survive to the next instruction, so WORKDIR prevents fragile paths. ARG values can still appear in build metadata and cache keys, and a label without a standard key becomes hard for automation to query.

When to reach for it

  • The fit is strongest for this job: set WORKDIR, attach LABELs, and pass build-time ARG values cleanly. Pair it with docker image inspect app:2.1.0 --format {{json .Config.Labels}} and docker run --rm app:2.1.0 pwd in the runbook.
  • Production WORKDIR changes should make LABEL observable through ARG APP_VERSION=2.1.0; WORKDIR /opt/acme; LABEL org.opencontainers.image.version=$APP_VERSION, rather than trusting the runtime default.
  • Keep a rollback example for the case where putting an API token in ARG and claiming it is secret, or relying on RUN cd /opt/acme for later COPY and CMD steps.

When to avoid or be careful

  • Do not accept command success while the system can still fail by putting an API token in ARG and claiming it is secret, or relying on RUN cd /opt/acme for later COPY and CMD steps.
  • Avoid erasing ARG protection merely to dodge this cost: “Build arguments make variants easy but increase cache dimensions; environment variables are more appropriate for runtime settings”.
  • Do not replace the evidence path-docker image inspect app:2.1.0 --format {{json .Config.Labels}} and docker run --rm app:2.1.0 pwd-with a screenshot or a mutable tag.

ARG APP_VERSION=2.1.0; WORKDIR /opt/acme; LABEL org.opencontainers.image.version=$APP_VERSION: practical choices

Explicit WORKDIR
Best for: Set WORKDIR, attach LABELs, and pass build-time ARG values cleanly, with a reviewed runbook.

Strengths: WORKDIR creates or changes the build’s current directory; LABEL stores image metadata; ARG accepts a build-time input only for available instructions and does not automatically persist as a runtime environment variable. WORKDIR should be absolute and LABEL keys should follow a namespace.

Watch out: Build arguments make variants easy but increase cache dimensions; environment variables are more appropriate for runtime settings.

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

Strengths: It can begin with ARG APP_VERSION=2.1.0; WORKDIR /opt/acme; LABEL org.opencontainers.image.version=$APP_VERSION and fewer surrounding controls.

Watch out: It silently accepts putting an API token in ARG and claiming it is secret, or relying on RUN cd /opt/acme for later COPY and CMD steps, which makes the result hard to reproduce after promotion.

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

Strengths: Relative cd state in a RUN shell cannot survive to the next instruction, so WORKDIR prevents fragile paths. ARG values can still appear in build metadata and cache keys, and a label without a standard key becomes hard for automation to query.

Watch out: The platform adds an ownership and debugging layer; retain docker image inspect app:2.1.0 --format {{json .Config.Labels}} and docker run --rm app:2.1.0 pwd as the direct check.

Interview signals & numbers to know

When this topic comes up

  • ARG APP_VERSION=2.1.0; WORKDIR /opt/acme; LABEL org.opencontainers.image.version=$APP_VERSION
  • docker image inspect app:2.1.0 --format {{json .Config.Labels}} and docker run --rm app:2.1.0 pwd
  • Failure signature: putting an API token in ARG and claiming it is secret, or relying on RUN cd /opt/acme for later COPY and CMD steps
  • Trade-off: Build arguments make variants easy but increase cache dimensions; environment variables are more appropriate for runtime settings.

Benchmarks & rules of thumb

1 build scope -
ARG values are intended for construction
2 path forms -
absolute WORKDIR is safest and relative forms can accumulate
org.opencontainers.image -
standard namespace for useful labels

Common mistakes & follow-ups

Common interview mistakes

  • Treating a successful ARG APP_VERSION=2.1.0; WORKDIR /opt/acme; LABEL org.opencontainers.image.version=$APP_VERSION as proof and skipping docker image inspect app:2.1.0 --format {{json .Config.Labels}} and docker run --rm app:2.1.0 pwd.
  • Putting an API token in ARG and claiming it is secret, or relying on RUN cd /opt/acme for later COPY and CMD steps
  • Optimizing WORKDIR without accepting that build arguments make variants easy but increase cache dimensions; environment variables are more appropriate for runtime settings.

Likely interviewer follow-ups

  • Why does ARG APP_VERSION=2.1.0; WORKDIR /opt/acme; LABEL org.opencontainers.image.version=$APP_VERSION address WORKDIR here rather than through application code?
  • How would you distinguish healthy LABEL from the failure “putting an API token in ARG and claiming it is secret, or relying on RUN cd /opt/acme for later COPY and CMD steps” using docker image inspect app:2.1.0 --format {{json .Config.Labels}} and docker run --rm app:2.1.0 pwd?
  • When would this trade-off-“Build arguments make variants easy but increase cache dimensions; environment variables are more appropriate for runtime settings”-justify a different ARG design?
Interview tip: Give one concrete operation-ARG APP_VERSION=2.1.0; WORKDIR /opt/acme; LABEL org.opencontainers.image.version=$APP_VERSION-and one proof-docker image inspect app:2.1.0 --format {{json .Config.Labels}} and docker run --rm app:2.1.0 pwd. Then name the boundary: putting an API token in ARG and claiming it is secret, or relying on RUN cd /opt/acme for later COPY and CMD steps.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Setting Working Directories & Metadata (WORKDIR, LABEL, ARG) with generative AI interview Q&A and hands-on practice.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Copying Files (COPY vs ADD & When to Use Each)
Next topic
Runtime Configuration (ENV, EXPOSE, USER)