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

Executing Build Commands (RUN - Shell vs Exec Form)

Write RUN steps in shell or exec form and know when each is safer or clearer.

BeginnerShell Form · Exec Form3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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

Loading interactive 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.

Concrete Shell Form setup

RUN ["/usr/bin/python3", "-m", "pip", "install", "--no-cache-dir", "-r", "/app/requirements.txt"]

Key takeaway: Prove Exec Form with 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.

Deliberate failure test

Exercise this failure safely: assuming JSON array items receive variable expansion, or chaining cat stream | grep without making pipeline failures fatal.

Key takeaway: Exec form is safer and more explicit; shell form is concise for package installation, cleanup and environment expansion.

How it works, step by step

Step 1 of 4
Design

1. 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.

Use exec form for a direct program and shell form only when shell parsing is part of the instruction’s intent.
Key numbers at this step
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 shell in 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

Explicit Shell Form
Best for: Write RUN steps in shell or exec form and know when each is safer or clearer, with a reviewed runbook.

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.

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

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.

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

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 skipping docker 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?
Interview tip: Give one concrete operation-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

Loading quiz

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.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Base Images (FROM, Official Images, scratch)
Next topic
Copying Files (COPY vs ADD & When to Use Each)