Docker · Containers in practice
What is the difference between ENTRYPOINT and CMD?
Short answer
ENTRYPOINT sets the fixed executable, while CMD supplies default arguments or a default command. Runtime arguments replace CMD but are appended to ENTRYPOINT.
If you define only CMD, docker run with a command replaces it entirely. If you define ENTRYPOINT, the container always runs that executable, and CMD becomes the default argument list that callers can override. Combining both is the usual pattern for a CLI-like image.
ENTRYPOINT ["node", "server.js"]
CMD ["--port", "3000"]Shell form versus exec form
- Exec form runs the binary directly, so it becomes PID 1 and receives signals
- Shell form runs through /bin/sh -c, which may not forward SIGTERM
- A shell-form CMD can cause slow shutdowns and lost graceful-drain logic
- Use exec form unless you genuinely need shell expansion
Signal handling matters in production. If the process runs under a shell, docker stop sends SIGTERM to the shell, which may exit without passing it on. The container then waits for the timeout and is killed, so in-flight requests are dropped.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What happens to CMD when you pass a command to docker run?
- 2Why does exec form matter for graceful shutdown?
- 3How do you add an init process for signal handling?
- 4When would an entrypoint script be justified?
Say it in the interview
ENTRYPOINT defines the executable that always runs, and CMD provides default arguments or a default command. If I pass a command to docker run it replaces CMD, but with an entrypoint it becomes arguments to that entrypoint. I use the exec form so the application is PID 1 and receives SIGTERM, and any entrypoint script ends with exec so signals are not swallowed.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Docker mock interview and get scored feedback on depth, clarity and confidence.