Executing Build Commands (RUN - Shell vs Exec Form)
Write RUN steps in shell or exec form and know when each is safer or clearer.
In plain words
Use exec form for a direct program and shell form only when shell parsing is part of the instruction’s intent.
Think of it like…
Exec form is handing each tool a named tray; shell form is handing one supervisor a sentence that must be parsed before tools are chosen.
Where you’ve seen it
In practice, RUN ["/usr/bin/python3", "-m", "pip", "install", "--no-cache-dir", "-r", "/app/requirements.txt"] is the concrete move for this job: write RUN steps in shell or exec form and know when each is safer or clearer. The release is not accepted until docker history --no-trunc app:dev and inspect a controlled failing pipeline with and without set -o pipefail in a bash shell confirms the result.
Words you’ll hear
- Shell Form
- The implementation control in
RUN ["/usr/bin/python3", "-m", "pip", "install", "--no-cache-dir", "-r", "/app/requirements.txt"]for write RUN steps in shell or exec form and know when each is safer or clearer. - Exec Form
- The result to observe with
docker history --no-trunc app:dev and inspect a controlled failing pipeline with and without set -o pipefail in a bash shell, rather than infer from a successful command alone. - RUN
- The production boundary exposed when assuming JSON array items receive variable expansion, or chaining cat stream | grep without making pipeline failures fatal.
Visual diagram
Theory & deep dive
Shell Form control path
The concrete mechanism is RUN ["/usr/bin/python3", "-m", "pip", "install", "--no-cache-dir", "-r", "/app/requirements.txt"]. The observable result is docker history --no-trunc app:dev and inspect a controlled failing pipeline with and without set -o pipefail in a bash shell; if those disagree, inspect the runtime evidence before changing unrelated settings.
Write RUN steps in shell or exec form and know when each is safer or clearer. Keep Shell Form, Exec Form and RUN in the same deployment record so the intent and the observed state cannot drift apart.
RUN ["/usr/bin/python3", "-m", "pip", "install", "--no-cache-dir", "-r", "/app/requirements.txt"]
docker history --no-trunc app:dev and inspect a controlled failing pipeline with and without set -o pipefail in a bash shell, not with command success alone.RUN failure boundary
Shell pipelines can hide a failed producer without pipefail, while shell metacharacters in variables can become injection risks. Exec form exposes malformed JSON and argument boundaries early but does not expand $VAR by itself. The narrow failure to design for is assuming JSON array items receive variable expansion, or chaining cat stream | grep without making pipeline failures fatal.
Accept this compromise explicitly: Exec form is safer and more explicit; shell form is concise for package installation, cleanup and environment expansion. The controlled check-docker history --no-trunc app:dev and inspect a controlled failing pipeline with and without set -o pipefail in a bash shell-also identifies the point where a different design is safer.
Exercise this failure safely: assuming JSON array items receive variable expansion, or chaining cat stream | grep without making pipeline failures fatal.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: Shell pipelines can hide a failed producer without pipefail, while shell metacharacters in variables can become injection risks. Exec form exposes malformed JSON and argument boundaries early but does not expand $VAR by itself.
- Intent
- Shell Form
When to use it & trade-offs
What it is
RUN in shell form executes through /bin/sh -c, enabling pipes, expansion and &&; exec form gives the builder a JSON array and executes the named program directly without a shell. The chosen form changes signal behavior, error visibility and available syntax.
Why it matters: Shell pipelines can hide a failed producer without pipefail, while shell metacharacters in variables can become injection risks. Exec form exposes malformed JSON and argument boundaries early but does not expand $VAR by itself.
When to reach for it
- The fit is strongest for this job: write RUN steps in shell or exec form and know when each is safer or clearer. Pair it with
docker history --no-trunc app:dev and inspect a controlled failing pipeline with and without set -o pipefail in a bash shellin the runbook. - Production Shell Form changes should make Exec Form observable through
RUN ["/usr/bin/python3", "-m", "pip", "install", "--no-cache-dir", "-r", "/app/requirements.txt"], rather than trusting the runtime default. - Keep a rollback example for the case where assuming JSON array items receive variable expansion, or chaining cat stream | grep without making pipeline failures fatal.
When to avoid or be careful
- Do not accept command success while the system can still fail by assuming JSON array items receive variable expansion, or chaining cat stream | grep without making pipeline failures fatal.
- Avoid erasing RUN protection merely to dodge this cost: “Exec form is safer and more explicit; shell form is concise for package installation, cleanup and environment expansion”.
- Do not replace the evidence path-
docker history --no-trunc app:dev and inspect a controlled failing pipeline with and without set -o pipefail in a bash shell-with a screenshot or a mutable tag.
RUN ["/usr/bin/python3", "-m", "pip", "install", "--no-cache-dir", "-r", "/app/requirements.txt"]: practical choices
Strengths: RUN in shell form executes through /bin/sh -c, enabling pipes, expansion and &&; exec form gives the builder a JSON array and executes the named program directly without a shell. The chosen form changes signal behavior, error visibility and available syntax.
Watch out: Exec form is safer and more explicit; shell form is concise for package installation, cleanup and environment expansion.
Strengths: It can begin with RUN ["/usr/bin/python3", "-m", "pip", "install", "--no-cache-dir", "-r", "/app/requirements.txt"] and fewer surrounding controls.
Watch out: It silently accepts assuming JSON array items receive variable expansion, or chaining cat stream | grep without making pipeline failures fatal, which makes the result hard to reproduce after promotion.
Strengths: Shell pipelines can hide a failed producer without pipefail, while shell metacharacters in variables can become injection risks. Exec form exposes malformed JSON and argument boundaries early but does not expand $VAR by itself.
Watch out: The platform adds an ownership and debugging layer; retain docker history --no-trunc app:dev and inspect a controlled failing pipeline with and without set -o pipefail in a bash shell as the direct check.
Interview signals & numbers to know
When this topic comes up
- RUN ["/usr/bin/python3", "-m", "pip", "install", "--no-cache-dir", "-r", "/app/requirements.txt"]
- docker history --no-trunc app:dev and inspect a controlled failing pipeline with and without set -o pipefail in a bash shell
- Failure signature: assuming JSON array items receive variable expansion, or chaining cat stream | grep without making pipeline failures fatal
- Trade-off: Exec form is safer and more explicit; shell form is concise for package installation, cleanup and environment expansion.
Benchmarks & rules of thumb
- 1 direct executable
- in exec form RUN
- 0 implicit shell expansion
- in JSON exec form
- 2 instructions joined by &&
- often become one atomic cache layer
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
RUN ["/usr/bin/python3", "-m", "pip", "install", "--no-cache-dir", "-r", "/app/requirements.txt"]as proof and skippingdocker history --no-trunc app:dev and inspect a controlled failing pipeline with and without set -o pipefail in a bash shell. - Assuming JSON array items receive variable expansion, or chaining cat stream | grep without making pipeline failures fatal
- Optimizing Shell Form without accepting that exec form is safer and more explicit; shell form is concise for package installation, cleanup and environment expansion.
Likely interviewer follow-ups
- Why does
RUN ["/usr/bin/python3", "-m", "pip", "install", "--no-cache-dir", "-r", "/app/requirements.txt"]address Shell Form here rather than through application code? - How would you distinguish healthy Exec Form from the failure “assuming JSON array items receive variable expansion, or chaining cat stream | grep without making pipeline failures fatal” using
docker history --no-trunc app:dev and inspect a controlled failing pipeline with and without set -o pipefail in a bash shell? - When would this trade-off-“Exec form is safer and more explicit; shell form is concise for package installation, cleanup and environment expansion”-justify a different RUN design?
RUN ["/usr/bin/python3", "-m", "pip", "install", "--no-cache-dir", "-r", "/app/requirements.txt"]-and one proof-docker history --no-trunc app:dev and inspect a controlled failing pipeline with and without set -o pipefail in a bash shell. Then name the boundary: assuming JSON array items receive variable expansion, or chaining cat stream | grep without making pipeline failures fatal.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Executing Build Commands (RUN - Shell vs Exec Form) with generative AI interview Q&A and hands-on practice.