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

Container Startup Commands (CMD vs ENTRYPOINT & Combining Both)

Combine ENTRYPOINT and CMD so images behave like CLIs with sensible defaults.

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

In plain words

Make a reusable tool image expose one entrypoint and a task image provide overridable defaults, without hiding signal and exit behavior behind a shell.

Think of it like…

ENTRYPOINT is the program name printed at the top of a command; CMD is the suggested phrase after it that callers may cross out and replace.

Where you’ve seen it

In practice, ENTRYPOINT ["/usr/local/bin/acme"]; CMD ["serve", "--port=8080"] is the concrete move for this job: combine ENTRYPOINT and CMD so images behave like CLIs with sensible defaults. The release is not accepted until docker run --rm app:2.1 --help and docker inspect --format {{json .Config.Entrypoint}}:{{json .Config.Cmd}} app:2.1 confirms the result.

Words you’ll hear

CMD
The implementation control in ENTRYPOINT ["/usr/local/bin/acme"]; CMD ["serve", "--port=8080"] for combine ENTRYPOINT and CMD so images behave like CLIs with sensible defaults.
ENTRYPOINT
The result to observe with docker run --rm app:2.1 --help and docker inspect --format {{json .Config.Entrypoint}}:{{json .Config.Cmd}} app:2.1, rather than infer from a successful command alone.
Defaults
The production boundary exposed when using shell-form ENTRYPOINT /start.sh && server so PID 1 is a shell, or forgetting exec "$@" in a wrapper script.

Visual diagram

Loading interactive diagram

Theory & deep dive

CMD control path

The concrete mechanism is ENTRYPOINT ["/usr/local/bin/acme"]; CMD ["serve", "--port=8080"]. The observable result is docker run --rm app:2.1 --help and docker inspect --format {{json .Config.Entrypoint}}:{{json .Config.Cmd}} app:2.1; if those disagree, inspect the runtime evidence before changing unrelated settings.

Combine ENTRYPOINT and CMD so images behave like CLIs with sensible defaults. Keep CMD, ENTRYPOINT and Defaults in the same deployment record so the intent and the observed state cannot drift apart.

Concrete CMD setup

ENTRYPOINT ["/usr/local/bin/acme"]; CMD ["serve", "--port=8080"]

Key takeaway: Prove ENTRYPOINT with docker run --rm app:2.1 --help and docker inspect --format {{json .Config.Entrypoint}}:{{json .Config.Cmd}} app:2.1, not with command success alone.

Defaults failure boundary

A shell-form wrapper changes PID 1 and can swallow signals; a malformed combination can cause an executable to receive the entire string as one argument. A clear entrypoint script must exec the real process after setup. The narrow failure to design for is using shell-form ENTRYPOINT /start.sh && server so PID 1 is a shell, or forgetting exec "$@" in a wrapper script.

Accept this compromise explicitly: The fixed entrypoint supports image-as-CLI behavior; a fully overridable CMD is simpler when callers need arbitrary runtime commands. The controlled check-docker run --rm app:2.1 --help and docker inspect --format {{json .Config.Entrypoint}}:{{json .Config.Cmd}} app:2.1-also identifies the point where a different design is safer.

Deliberate failure test

Exercise this failure safely: using shell-form ENTRYPOINT /start.sh && server so PID 1 is a shell, or forgetting exec "$@" in a wrapper script.

Key takeaway: The fixed entrypoint supports image-as-CLI behavior; a fully overridable CMD is simpler when callers need arbitrary runtime commands.

How it works, step by step

Step 1 of 4
Design

1. State the boundary

Write down why the workload needs this: A shell-form wrapper changes PID 1 and can swallow signals; a malformed combination can cause an executable to receive the entire string as one argument. A clear entrypoint script must exec the real process after setup.

Make a reusable tool image expose one entrypoint and a task image provide overridable defaults, without hiding signal and exit behavior behind a shell.
Key numbers at this step
Intent
CMD

When to use it & trade-offs

What it is

ENTRYPOINT is the fixed executable contract of an image; CMD supplies default arguments that can be replaced at docker run time. With both in exec form, Docker executes the ENTRYPOINT array followed by CMD, while overriding --entrypoint changes the executable itself.

Why it matters: A shell-form wrapper changes PID 1 and can swallow signals; a malformed combination can cause an executable to receive the entire string as one argument. A clear entrypoint script must exec the real process after setup.

When to reach for it

  • The fit is strongest for this job: combine ENTRYPOINT and CMD so images behave like CLIs with sensible defaults. Pair it with docker run --rm app:2.1 --help and docker inspect --format {{json .Config.Entrypoint}}:{{json .Config.Cmd}} app:2.1 in the runbook.
  • Production CMD changes should make ENTRYPOINT observable through ENTRYPOINT ["/usr/local/bin/acme"]; CMD ["serve", "--port=8080"], rather than trusting the runtime default.
  • Keep a rollback example for the case where using shell-form ENTRYPOINT /start.sh && server so PID 1 is a shell, or forgetting exec "$@" in a wrapper script.

When to avoid or be careful

  • Do not accept command success while the system can still fail by using shell-form ENTRYPOINT /start.sh && server so PID 1 is a shell, or forgetting exec "$@" in a wrapper script.
  • Avoid erasing Defaults protection merely to dodge this cost: “The fixed entrypoint supports image-as-CLI behavior; a fully overridable CMD is simpler when callers need arbitrary runtime commands”.
  • Do not replace the evidence path-docker run --rm app:2.1 --help and docker inspect --format {{json .Config.Entrypoint}}:{{json .Config.Cmd}} app:2.1-with a screenshot or a mutable tag.

ENTRYPOINT ["/usr/local/bin/acme"]; CMD ["serve", "--port=8080"]: practical choices

Explicit CMD
Best for: Combine ENTRYPOINT and CMD so images behave like CLIs with sensible defaults, with a reviewed runbook.

Strengths: ENTRYPOINT is the fixed executable contract of an image; CMD supplies default arguments that can be replaced at docker run time. With both in exec form, Docker executes the ENTRYPOINT array followed by CMD, while overriding --entrypoint changes the executable itself.

Watch out: The fixed entrypoint supports image-as-CLI behavior; a fully overridable CMD is simpler when callers need arbitrary runtime commands.

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

Strengths: It can begin with ENTRYPOINT ["/usr/local/bin/acme"]; CMD ["serve", "--port=8080"] and fewer surrounding controls.

Watch out: It silently accepts using shell-form ENTRYPOINT /start.sh && server so PID 1 is a shell, or forgetting exec "$@" in a wrapper script, which makes the result hard to reproduce after promotion.

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

Strengths: A shell-form wrapper changes PID 1 and can swallow signals; a malformed combination can cause an executable to receive the entire string as one argument. A clear entrypoint script must exec the real process after setup.

Watch out: The platform adds an ownership and debugging layer; retain docker run --rm app:2.1 --help and docker inspect --format {{json .Config.Entrypoint}}:{{json .Config.Cmd}} app:2.1 as the direct check.

Interview signals & numbers to know

When this topic comes up

  • ENTRYPOINT ["/usr/local/bin/acme"]; CMD ["serve", "--port=8080"]
  • docker run --rm app:2.1 --help and docker inspect --format {{json .Config.Entrypoint}}:{{json .Config.Cmd}} app:2.1
  • Failure signature: using shell-form ENTRYPOINT /start.sh && server so PID 1 is a shell, or forgetting exec "$@" in a wrapper script
  • Trade-off: The fixed entrypoint supports image-as-CLI behavior; a fully overridable CMD is simpler when callers need arbitrary runtime commands.

Benchmarks & rules of thumb

1 executable -
ENTRYPOINT selects it
0 permanent defaults -
CMD arguments can be replaced after the image name
exec "$@" -
wrapper pattern that preserves argument and signal flow

Common mistakes & follow-ups

Common interview mistakes

  • Treating a successful ENTRYPOINT ["/usr/local/bin/acme"]; CMD ["serve", "--port=8080"] as proof and skipping docker run --rm app:2.1 --help and docker inspect --format {{json .Config.Entrypoint}}:{{json .Config.Cmd}} app:2.1.
  • Using shell-form ENTRYPOINT /start.sh && server so PID 1 is a shell, or forgetting exec "$@" in a wrapper script
  • Optimizing CMD without accepting that the fixed entrypoint supports image-as-CLI behavior; a fully overridable CMD is simpler when callers need arbitrary runtime commands.

Likely interviewer follow-ups

  • Why does ENTRYPOINT ["/usr/local/bin/acme"]; CMD ["serve", "--port=8080"] address CMD here rather than through application code?
  • How would you distinguish healthy ENTRYPOINT from the failure “using shell-form ENTRYPOINT /start.sh && server so PID 1 is a shell, or forgetting exec "$@" in a wrapper script” using docker run --rm app:2.1 --help and docker inspect --format {{json .Config.Entrypoint}}:{{json .Config.Cmd}} app:2.1?
  • When would this trade-off-“The fixed entrypoint supports image-as-CLI behavior; a fully overridable CMD is simpler when callers need arbitrary runtime commands”-justify a different Defaults design?
Interview tip: Give one concrete operation-ENTRYPOINT ["/usr/local/bin/acme"]; CMD ["serve", "--port=8080"]-and one proof-docker run --rm app:2.1 --help and docker inspect --format {{json .Config.Entrypoint}}:{{json .Config.Cmd}} app:2.1. Then name the boundary: using shell-form ENTRYPOINT /start.sh && server so PID 1 is a shell, or forgetting exec "$@" in a wrapper script.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Container Startup Commands (CMD vs ENTRYPOINT & Combining Both) with generative AI interview Q&A and hands-on practice.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Runtime Configuration (ENV, EXPOSE, USER)
Next topic
Using .dockerignore to Speed Up Builds & Prevent Leaks