Dockerfile CMD versus ENTRYPOINT
container startup behavior.
ENTRYPOINT sets the fixed executable; CMD sets default args or the default command; run-time args override CMD but append to ENTRYPOINT. Use together to make a fixed binary with overridable defaults.
WHAT THIS TESTS It checks whether you understand the interplay that controls a container's main process and how run-time arguments are resolved against the two instructions.
A GOOD ANSWER COVERS ENTRYPOINT declares the executable that the container always runs. CMD provides default arguments to that executable, or, when there is no ENTRYPOINT, the default command itself. The resolution rule is the crux: arguments supplied on docker run override CMD completely, but they are appended to ENTRYPOINT rather than replacing it. Both have exec form (JSON array) and shell form; exec form is preferred because it runs the binary as PID 1 directly so it receives signals like SIGTERM, whereas shell form wraps it in /bin/sh -c and can swallow signals. Used together, ENTRYPOINT fixes the program and CMD gives overridable defaults.
COMMON WRONG ANSWERS Claiming docker run arguments replace ENTRYPOINT. Treating CMD and ENTRYPOINT as interchangeable. Using shell form and then wondering why the container ignores SIGTERM on shutdown.
LIKELY FOLLOW-UPS Why does exec form matter for signal handling and graceful shutdown? How do you override ENTRYPOINT at run time? What happens if you set neither?
ONE CONCRETE EXAMPLE A Dockerfile sets ENTRYPOINT ["ping"] and CMD ["localhost"]. Running the container with no args pings localhost; running it with example.com pings example.com because the argument overrides CMD but appends to the fixed ping ENTRYPOINT. This makes the image behave like the ping command with a sensible default target.
Interview question
If a Dockerfile has ENTRYPOINT ["ping"] and CMD ["localhost"], what happens when you run the container with the argument example.com?
- a.It pings localhost and ignores example.com
- b.It errors because two commands are defined
- c.It runs example.com as a new entrypoint, ignoring ping
- d.It pings example.com, since the arg overrides CMD but appends to ENTRYPOINTCorrect
Why? this is the answer
Run-time arguments replace CMD but are appended to ENTRYPOINT, so ping example.com runs; the arg does not replace the ENTRYPOINT executable nor cause a conflict.
Just read this? Test yourself on what you have been reading.
Read the original → docs.docker.com
- #dockerfile
- #entrypoint
- #cmd
- #signals
- #build
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles