Docker interview question · Most asked

CMD or ENTRYPOINT: what is the difference, and what are exec and shell forms?

Summary

ENTRYPOINT sets the executable the container runs; CMD provides default arguments (or a default command if there is no entrypoint), which docker run image args overrides. The exec form (["node", "server.js"]) runs the process directly; the shell form (node server.js) wraps it in /bin/sh -c.

Longer Answer

Prefer the exec form: with the shell form, the shell becomes PID 1 and typically does not forward signals, so docker stop (SIGTERM) does not reach your application, which is killed after the timeout instead of shutting down gracefully. A common pattern is an ENTRYPOINT script that prepares the environment and ends with exec "$@" so the real process replaces the shell, plus CMD for default arguments.

Key Points

  • ENTRYPOINT = executable; CMD = default args.
  • Exec form runs the process directly.
  • Shell form breaks signal handling.
  • Entrypoint scripts end with exec "$@".

More most-asked Docker questions

← All Docker questions