CodePark/ questions / Dockerfile instructions
START PRACTICINGPRACTICE
Backend question · Docker › Dockerfile instructions

Explain the difference between CMD and ENTRYPOINT, and between their exec and shell forms.

ENTRYPOINT sets the program the container always runs. CMD sets the default arguments. If there is no ENTRYPOINT, CMD sets the default command instead.

How to override them:

  • Arguments after the image name in docker run replace CMD.
  • Only the --entrypoint flag replaces ENTRYPOINT.
  • When both are in exec form, Docker appends CMD to ENTRYPOINT.

Exec form is a JSON array like ["node", "server.js"]. Docker runs the program directly, so it becomes PID 1 (the main process). It receives signals such as SIGTERM from docker stop, so it can shut down cleanly.

Shell form looks like CMD node server.js. Docker wraps it in /bin/sh -c. So the shell becomes PID 1, and it may not pass signals to your app. Also, a shell-form ENTRYPOINT ignores CMD and docker run arguments.

In short: prefer the exec form for both.

  • ENTRYPOINT is the fixed program, and CMD gives its default arguments.
  • docker run arguments replace CMD, and --entrypoint replaces ENTRYPOINT.
  • Exec form runs the program directly as PID 1.
  • Shell form wraps the command in /bin/sh -c and can break signal handling.