Sub topic 7
Starting the app: CMD, ENTRYPOINT, and exec form
This chapter covers how a container decides what to run when it starts. It assumes you can write a basic Dockerfile with FROM, COPY, and RUN, and that you can run docker build and docker run.
What CMD and ENTRYPOINT do
RUN executes during the build. CMD and ENTRYPOINT do nothing during the build. They record the command that runs when a container starts from the image.
CMDsets the default command or default arguments. A user can replace it by putting a command after the image name indocker run.ENTRYPOINTsets the executable that always runs. Arguments after the image name are appended to it. Replacing it requires the--entrypointflag.
If a Dockerfile has several CMD lines, only the last one counts. The same is true for ENTRYPOINT.
Exec form and shell form
Both instructions accept two syntaxes.
| Form | Example | How it runs |
|---|---|---|
| Exec form | `CMD ["node", "server.js"]` | Runs the program directly as process 1 (PID 1). No shell is involved. |
| Shell form | `CMD node server.js` | Runs as `/bin/sh -c "node server.js"`. The shell is PID 1 and your program is its child. |
Exec form is a JSON array, so you must use double quotes. Single quotes make Docker treat the line as shell form, and the container usually fails with a confusing error.
Prefer exec form. Docker sends stop signals such as SIGTERM to PID 1. With shell form, that is the shell, and many shells do not pass the signal on to your program. The result is that docker stop waits 10 seconds and then kills the container with SIGKILL, so your app never gets to shut down cleanly.
Exec form has one trade-off. Because no shell runs, variables are not expanded. CMD ["echo", "$HOME"] prints the literal text $HOME. If you need expansion, call a shell explicitly, for example CMD ["sh", "-c", "echo $HOME"], or use a startup script that ends with exec.
Combining ENTRYPOINT and CMD
When both are set in exec form, CMD supplies default arguments to ENTRYPOINT.
FROM python:3.12-slim
ENTRYPOINT ["python", "-m", "http.server"]
CMD ["8000"]The container starts python -m http.server 8000. The image behaves like a single tool with a default argument.
docker build -t webserve .
docker run --rm webserve
docker run --rm webserve 9000The first docker run serves on port 8000. The second replaces CMD, so the container runs python -m http.server 9000. The ENTRYPOINT part stays the same.
Choosing between them
- Use
CMDalone when the image is an application that users might want to override, or when you want people to be able to rundocker run image bashfor debugging. - Use
ENTRYPOINTplusCMDwhen the image is a fixed tool and users should only change the arguments. - Use
ENTRYPOINTas a wrapper script when you need setup work, such as generating a config file, before the main process starts. End the script withexec "$@"so the real program replaces the shell and becomes PID 1.
Common errors
- Writing
CMD ['node', 'server.js']with single quotes. Docker falls back to shell form and the command breaks. - Using shell form and wondering why
docker stopis slow. - Expecting
$VARto expand in exec form. - Combining
ENTRYPOINTin shell form withCMD. TheCMDvalue is ignored, because shell-formENTRYPOINTdoes not receive extra arguments. - Forgetting that
docker run image somethingreplacesCMDentirely. It does not add to it.
Exercise
A Dockerfile contains ENTRYPOINT ["python", "-m", "http.server"] and CMD ["8000"]. What command does the container run for docker run myimage 9000, and what would it run for docker run myimage with no arguments?
Show answer
With 9000 it runs python -m http.server 9000, because the argument replaces CMD and is appended to ENTRYPOINT. With no arguments it runs python -m http.server 8000, using the CMD default.
Exercise
Your Node.js container takes about 10 seconds to stop and the app never logs its shutdown message. The Dockerfile ends with CMD node server.js. What is the likely cause, and what is the fix?
Show answer
Shell form runs the app as a child of /bin/sh -c. The shell is PID 1 and receives SIGTERM but does not forward it, so Docker waits and then sends SIGKILL. Switch to exec form: CMD ["node", "server.js"].