Docker Tutorial · Building Images · Lesson 12 of 26

CMD vs ENTRYPOINT

How ENTRYPOINT and CMD combine, shell form versus exec form, and why it matters for signals.

Create a free account to save this page, take notes and track your progress. Sign in

How they combine

  • ENTRYPOINT is the executable that always runs.
  • CMD provides default arguments (or the default command when there is no ENTRYPOINT).
  • Arguments after the image name in docker run replace CMD; --entrypoint replaces the entrypoint.

Exec form versus shell form

CMD ["node", "server.js"] (exec form) runs the process directly as PID 1, so docker stop's SIGTERM reaches it. CMD node server.js (shell form) runs it under /bin/sh -c, and the shell may not forward signals, so the container waits 10 seconds and gets killed.

CMD supplies default arguments to ENTRYPOINT
trap 'docker image rm -f rc-entry:1 > /dev/null 2>&1' EXIT
printf 'FROM alpine:3.24\nENTRYPOINT ["echo", "greeting:"]\nCMD ["hello"]\n' > Dockerfile
docker build -q -t rc-entry:1 . > /dev/null
docker run --rm rc-entry:1
docker run --rm rc-entry:1 good morning
docker run --rm --entrypoint date rc-entry:1 -u -d @0 +%Y-%m-%d

Output

greeting: hello
greeting: good morning
1970-01-01

Run this on your computer — see “Get Started” for setup.

Test yourself

docker stop takes 10 seconds and then kills your container. A likely cause?

  1. The image is too big
  2. The app runs under a shell (shell-form CMD) that doesn't forward SIGTERM
  3. Docker is slow
  4. Missing EXPOSE
Show answer

Answer: The app runs under a shell (shell-form CMD) that doesn't forward SIGTERM

Use exec form so the app is PID 1 and receives SIGTERM, and handle it to shut down gracefully.

Was this page helpful?