Setting Working Directories & Metadata (WORKDIR, LABEL, ARG)
Set WORKDIR, attach LABELs, and pass build-time ARG values cleanly.
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_VERSIONfor 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
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.
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, 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.
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.
How it works, step by step
Step 1 of 41. 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.
- 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 pwdin 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
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.
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.
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_VERSIONas proof and skippingdocker 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_VERSIONaddress 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?
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
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.