CMD, ENTRYPOINT and PID 1

Intermediate · 12 min

How CMD and ENTRYPOINT combine, why shell form makes docker stop hang for 10 seconds, and how exec, --init and STOPSIGNAL fix it.

Before thisDockerfile instructions, from FROM to CMD

anthonywritescode · 12 min

CMD, ENTRYPOINT and PID 1

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by anthonywritescodeReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • ENTRYPOINT is the executable the container always runs. CMD is the default arguments, and anything after the image name in docker run replaces CMD.
  • In exec form, the final command is ENTRYPOINT followed by CMD. A shell-form ENTRYPOINT ignores CMD and docker run arguments entirely.
  • Shell form wraps the command in /bin/sh -c, so the shell becomes PID 1 and your app never receives the SIGTERM from docker stop.
  • docker stop sends SIGTERM, waits 10 seconds by default on Linux, then sends SIGKILL. A container that always takes 10 seconds to stop is ignoring the signal.
  • As PID 1, a process gets no default signal handling from the kernel and must reap orphaned children. docker run --init puts tini in front of your app to do both.
  • An entrypoint script should end with exec "$@" so the real process replaces the shell and becomes PID 1.
  • STOPSIGNAL changes the signal docker stop sends, for programs such as nginx that shut down gracefully on something other than SIGTERM.

The ten-second stop

You run docker stop api and wait. Ten seconds later it returns, every time. Nothing crashed. The process simply never heard the request to shut down, so Docker gave up and killed it. Open database connections were cut, in-flight requests were dropped, and a deploy that should take one second takes eleven.

The cause is nearly always the last line of the Dockerfile. CMD and ENTRYPOINT decide what process runs, and how they are written decides whether that process is PID 1 and whether it gets signals.

Who runs what

ENTRYPOINT is the program. CMD is the default arguments. Anything you type after the image name in docker run replaces CMD, and --entrypoint replaces the entrypoint. The table from the Dockerfile reference, trimmed:

No ENTRYPOINT ENTRYPOINT ["e", "p1"]
CMD ["c", "p2"] c p2 e p1 c p2
CMD c p2 /bin/sh -c c p2 e p1 /bin/sh -c c p2

A shell-form ENTRYPOINT e p1 always runs /bin/sh -c e p1 and ignores both CMD and your docker run arguments. One more rule: setting ENTRYPOINT in your Dockerfile resets any CMD inherited from the base image, so set CMD again if you need it.

A useful pattern is a fixed tool with default flags:

ENTRYPOINT ["/usr/local/bin/backup"]
CMD ["--daily"]

docker run img runs backup --daily, and docker run img --weekly runs backup --weekly.

Why shell form loses the signal

docker stop sends SIGTERM to PID 1 in the container, waits, then sends SIGKILL. The docker stop reference puts the default wait at 10 seconds for Linux containers and 30 for Windows. -t and --stop-timeout change it.

With CMD node server.js, PID 1 is /bin/sh -c "node server.js", and node is its child. The reference states it plainly: the shell form starts the command as a subcommand of /bin/sh -c, which does not pass signals. Node never sees SIGTERM. The fix is the exec form, CMD ["node", "server.js"], which makes node PID 1.

What is special about PID 1

As the namespaces lesson explains, the first process in a PID namespace is that namespace’s init, and the kernel treats it differently. Per pid_namespaces(7), a signal PID 1 has no handler for is dropped, whether it comes from inside the namespace or from the host, as docker stop’s does. Only SIGKILL and SIGSTOP from the host are forced through. That is how a real init avoids being killed by accident. An app that never installed a SIGTERM handler relies on the default action, termination, and as PID 1 it does not get one. So even exec form can leave you with a process that ignores docker stop.

PID 1 must also reap orphans. When a child’s parent dies, the orphan is re-parented to PID 1, and if PID 1 never calls wait(), exited children stay as zombies.

Two fixes, depending on whose code it is:

  • Handle the signal in your app. Most web frameworks do, if you let them run as PID 1.
  • Put a small init in front. docker run --init starts docker-init, which the reference says is backed by tini, as PID 1. It forwards signals to your process and reaps zombies. Compose has the same switch as init: true.

Entrypoint scripts and exec “$@”

Many images run a shell script first to prepare things, then start the real program. The official postgres:18 image does this. I pulled its config and the layer holding the script from Docker Hub with curl. The config says:

ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["postgres"]
STOPSIGNAL SIGINT

So the container runs docker-entrypoint.sh postgres. The end of the script’s _main function, from the extracted layer:

	if [ "$1" = 'postgres' ] && ! _pg_want_help "$@"; then
		docker_setup_env
		docker_create_db_directories
		if [ "$(id -u)" = '0' ]; then
			# then restart script as postgres user
			exec gosu postgres "$BASH_SOURCE" "$@"
		fi
		...
	fi

	exec "$@"

Two execs. The first re-runs the script as the postgres user, replacing the root shell. The second replaces the shell with postgres itself, so the database ends up as PID 1 and receives the stop signal directly. Leave out that exec and the shell stays PID 1, and you are back to the ten-second stop. Any entrypoint script you write should end the same way.

Choosing the stop signal

STOPSIGNAL changes what docker stop sends. Postgres treats SIGTERM as “wait for clients to disconnect”, which could take forever, so the image asks for SIGINT, its fast shutdown. nginx uses SIGQUIT for a graceful stop. Set it when your program’s graceful signal is not SIGTERM, and --stop-signal overrides it per container.

About the video

The embed is anthonywritescode’s “why don’t signals work in docker?”, January 2025, 12 minutes. Its chapters cover the PID 1 signal problem, setting up a demo, the failure itself, how signal handling behaves, fixing it with dumb-init, and Docker’s built-in init. It does not walk through the CMD and ENTRYPOINT combination table, which the lesson and Manuel Castellin’s video in deeper cover. English was judged from the channel, the description and the English caption track.

What was run: the postgres:18 config and its entrypoint script were fetched from Docker Hub’s registry API with curl and the layer unpacked with tar. The combination table, the shell-form signal behaviour and the PID 1 rules are from the Dockerfile reference, the docker stop and docker run --init references, and pid_namespaces(7), checked 2026-11-28. No container was stopped, since there is no Docker here.

Where this fits

The last line of the Dockerfile is settled. Next, the first thing every build does, before any instruction runs: sending the build context.

Check yourself

  1. 01An image has ENTRYPOINT ["/usr/bin/backup"] and CMD ["--daily"]. What runs for docker run img --weekly?

    /usr/bin/backup --weekly. Arguments after the image name replace CMD, and the exec-form ENTRYPOINT stays in front of them.

  2. 02Your Dockerfile ends with CMD node server.js and docker stop always takes 10 seconds. Why, and what is the smallest fix?

    Shell form makes /bin/sh -c PID 1, and the shell does not pass SIGTERM to node, so Docker waits 10 seconds and sends SIGKILL. Use CMD ["node", "server.js"].

  3. 03Your app now runs as PID 1 in exec form but still ignores SIGTERM. It never installed a handler. Why does that matter only inside a container?

    The kernel does not apply default signal actions to PID 1 of a namespace for signals it has no handler for, so an unhandled SIGTERM does nothing. Outside the container the same program would be killed by the default action.

  4. 04Why does the postgres image's docker-entrypoint.sh end with exec "$@" rather than just "$@"?

    exec replaces the shell with the postgres process, so postgres becomes PID 1 and receives docker stop's signal directly. Without exec the shell would stay PID 1 and the database would never see the signal.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark