A container running nothing but sleep 300 was told to stop. It took 10.13 seconds and exited with code 137. A second container, built so its main process was a shell script with a three-word signal handler, stopped in 0.14 seconds with exit code 0. Neither program was doing any work. The difference came down to how the Dockerfile’s CMD and ENTRYPOINT lines were written, and what that meant for the process sitting at PID 1. A dozen small images were built and run on Linux for this post, each one isolating a single rule.
Two instructions, two jobs
CMD supplies defaults
Docker’s Dockerfile reference says “The purpose of a CMD is to provide defaults for an executing container.” The key word is defaults. Anything typed after the image name in docker run replaces CMD completely:
# Dockerfile: CMD ["echo","hello from CMD"]
$ docker run --rm ep-cmdexec
hello from CMD
$ docker run --rm ep-cmdexec echo overridden
overridden
There’s also only one: “If you list more than one CMD, only the last one takes effect.”
ENTRYPOINT sets the program
ENTRYPOINT fixes what runs. Arguments from docker run don’t replace it; they get handed to it. The reference describes the classic pairing: an exec-form entrypoint “combined with CMD for setting default arguments that can be overridden at runtime.”
# Dockerfile: ENTRYPOINT ["echo","entry:"]
# CMD ["default","args"]
$ docker run --rm ep-epexec
entry: default args
$ docker run --rm ep-epexec x y z
entry: x y z
With no arguments, CMD filled in default args. With x y z, those replaced CMD but the entrypoint stayed. This is how images that behave like a command-line tool are built: the tool is the entrypoint, and its flags are the part users change.

Exec form and shell form
Two ways to write each line
Both instructions accept a JSON array, called exec form, or a plain string, called shell form. The reference warns that “The exec form is parsed as a JSON array, which means that you must use double-quotes.” Single quotes turn an exec-form line silently into shell form.
What Docker actually stores
docker inspect shows the difference after the build:
ENTRYPOINT ["echo","entry:"] -> ["echo","entry:"]
ENTRYPOINT echo entry-shell: -> ["/bin/sh","-c","echo entry-shell:"]
CMD sleep 300 ; echo done -> ["/bin/sh","-c","sleep 300 ; echo done"]
Shell form is wrapped in /bin/sh -c. That gives it variable expansion, pipes and &&, but it also puts a shell in front of the program, and that shell becomes the container’s first process.
Shell-form ENTRYPOINT swallows arguments
CMD and run arguments both vanish
The image ep-epshell had ENTRYPOINT echo entry-shell: and CMD ["ignored"]:
$ docker run --rm ep-epshell
entry-shell:
$ docker run --rm ep-epshell extra
entry-shell:
Neither the CMD value nor the word extra reached echo. The reference documents the behavior directly: “The shell form of ENTRYPOINT ignores any CMD or docker run command line arguments.” Anyone who has added flags to docker run and watched nothing change has probably hit this.
Replacing the entrypoint
The escape hatch is a flag, as the reference notes: “You can override the ENTRYPOINT instruction using the docker run --entrypoint flag.”
$ docker run --rm --entrypoint echo ep-epexec swapped
swapped
Overriding the entrypoint also drops the image’s CMD, so swapped printed alone, without default args.

PID 1 and the 10-second stop
What docker stop sends
The docker stop reference explains the sequence: “The main process inside the container will receive SIGTERM, and after a grace period, SIGKILL.” The default grace period “is 10 seconds for Linux containers.” A program that exits on SIGTERM stops instantly. One that ignores it gets killed ten seconds later, and exit code 137 (128 plus signal 9) is the mark of that kill. Across every test in this post, each container that ignored the signal stopped between 10.12 and 10.14 seconds after the command was issued: the full grace period, plus a little over a tenth of a second for Docker to send the kill and clean up. A stop time that lands that close to ten seconds is a diagnosis on its own.
Who is PID 1
Reading /proc/1/cmdline in each running container showed the first process:
| Dockerfile line | PID 1 |
|---|---|
CMD ["sleep","300"] |
sleep 300 |
CMD sleep 300 |
/bin/sh -c sleep 300 |
CMD sleep 300 ; echo done |
/bin/sh -c sleep 300 ; echo done |
In shell form, the shell is PID 1, and the reference states that a shell-form entrypoint “will not be the container’s PID 1, and will not receive Unix signals.” The SIGTERM goes to the shell, not the program.
Exec form didn’t fix it either
The surprising result was the exec-form sleep. It was PID 1, it received SIGTERM, and it still took 10.14 seconds and died with 137. The reason is in the kernel, not Docker. The Linux kill(2) manual notes that the only signals deliverable to PID 1 are “those for which init has explicitly installed” handlers, and the pid_namespaces(7) page applies the same rule to the first process in each namespace, which is what a container’s PID 1 is. sleep never installs a SIGTERM handler, so as PID 1 the signal is simply dropped. Only SIGKILL, which the namespaces manual says is “forcibly delivered when sent from an ancestor PID namespace,” gets through.
Three fixes, tested
A real signal handler
An image whose entrypoint was a shell loop with trap 'echo caught TERM, cleaning up; exit 0' TERM stopped in 0.136 seconds with exit code 0. Its logs showed started and then caught TERM, cleaning up. Most real servers, from web servers to databases, install SIGTERM handlers and shut down cleanly as PID 1, as long as they’re launched in exec form so they actually are PID 1.
docker run –init
For programs without a handler, Docker can insert a tiny init process. The docker run reference describes --init as running “an init inside the container that forwards signals and reaps processes.” With it, PID 1 became /sbin/docker-init -- sleep 300, and the same plain sleep stopped in 0.166 seconds. The exit code was 143, which is 128 plus 15: sleep died from the forwarded SIGTERM rather than being killed.
exec in wrapper scripts
Many images use an entrypoint shell script to do setup before starting the real program. Two versions were tested, identical except for the last line. One ended with "$@", the other with exec "$@", and both were started with sleep 300 as the command:
"$@" -> PID 1: /bin/sh /entry-noexec.sh sleep 300
exec "$@" -> PID 1: sleep 300
Without exec, the shell script sat at PID 1 and sleep ran as its child, out of reach of the SIGTERM. With exec, the shell replaced itself with sleep, which became PID 1 directly.
Here’s the catch the test exposed: both containers still took about 10.13 seconds to stop and both exited 137. exec "$@" puts the program in the right place, but sleep still has no SIGTERM handler, so the kernel rule about PID 1 still applies. The fix only pays off when the program it hands control to handles SIGTERM itself, or when the container also runs with --init.

Variables and the shell
Exec form doesn’t expand anything
One more difference catches people who move a working shell-form line into exec form. With ENV GREETING=hi set in the image, three versions of the same CMD printed:
CMD ["echo","$GREETING"] -> $GREETING
CMD echo $GREETING -> hi
CMD ["sh","-c","echo $GREETING"] -> hi
Exec form passes its strings to the program exactly as written. There’s no shell, so nothing turns $GREETING into its value. Shell form expands it because /bin/sh -c does the work. The third line gets both: exec form for a predictable PID 1 layout, with an explicit shell for the expansion, though that shell then becomes PID 1 and inherits the signal problem described above. For a variable that a program needs, the cleaner route is usually to let the program read it from its environment rather than from its command line.
A pattern that works
The usual shape
ENTRYPOINT ["/usr/local/bin/myapp"]
CMD ["--port", "8080"]
Exec form for both. The program is fixed, its default flags are visible and easy to override, and it runs as PID 1 where docker stop can reach it.
When CMD alone is right
For general-purpose images, such as a base image or a toolbox, CMD alone makes more sense: users expect docker run image bash to run bash, not to pass bash as an argument to something else.
A quick check for any image
docker inspect --format '{{json .Config.Entrypoint}} {{json .Config.Cmd}}' IMAGE shows exactly what will run. If either value begins with "/bin/sh","-c", signals and arguments may not go where the Dockerfile’s author expected. If docker stop takes exactly ten seconds and ends in 137, PID 1 isn’t handling SIGTERM.
Sources
- Docker Docs, Dockerfile reference: https://docs.docker.com/reference/dockerfile/
- Docker Docs, “docker container stop”: https://docs.docker.com/reference/cli/docker/container/stop/
- Docker Docs, “docker container run”: https://docs.docker.com/reference/cli/docker/container/run/
- kill(2) manual page, man7.org: https://man7.org/linux/man-pages/man2/kill.2.html
- pid_namespaces(7) manual page, man7.org: https://man7.org/linux/man-pages/man7/pid_namespaces.7.html
