Container Startup Commands (CMD vs ENTRYPOINT & Combining Both)
Combine ENTRYPOINT and CMD so images behave like CLIs with sensible defaults.
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
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.
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, 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.
Exercise this failure safely: using shell-form ENTRYPOINT /start.sh && server so PID 1 is a shell, or forgetting exec "$@" in a wrapper script.
How it works, step by step
Step 1 of 41. 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.
- 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.1in 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
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.
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.
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 skippingdocker 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?
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
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.