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.
How they combine
ENTRYPOINTis the executable that always runs.CMDprovides default arguments (or the default command when there is noENTRYPOINT).- Arguments after the image name in
docker runreplaceCMD;--entrypointreplaces 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.
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.
See ENTRYPOINT.
Test yourself
docker stop takes 10 seconds and then kills your container. A likely cause?
The image is too bigThe app runs under a shell (shell-form CMD) that doesn't forward SIGTERMDocker is slowMissing 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.