Lesson 47 · Section 5: Application Lifecycle Management
Lesson 47: Commands and arguments in Docker: CMD and ENTRYPOINT
What decides which process a container runs: the image's ENTRYPOINT and CMD, the shell and exec forms, and how docker run replaces each of them. This is the ground that command and args in a Pod stand on. · 20 min read · 4 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why this matters
Sooner or later you write a Pod that needs a different start command than its image has: a busybox that should
sleep 3600, an app that needs --port=8080, a debug container that must stay alive. Kubernetes gives you two
fields for that, command and args, and they are confusing until you know what they stand for. Both are
overrides of settings that live in the image, and those settings come from Docker.
So this lesson stays with Docker and answers three questions:
- What decides which process a container runs?
- How do
CMDandENTRYPOINTdiffer, and how do they combine? - What exactly does
docker runreplace when you add words or flags to it?
The exam has no Docker tasks. This is the foundation for the next lesson, where the same rules return as
command and args in a Pod, and where most mistakes come from not knowing them.
A container is one process
A virtual machine boots an operating system and then waits for you. A container does not. It is an ordinary process on the host, started with its own namespaces and resource limits (lesson 5 followed one down to the process ID). From that, two rules follow:
- The container starts by running one command.
- The container stops when that command's process exits, whatever else is going on inside it.
Try it with the plain ubuntu image:
docker run ubuntu
docker ps -a
CONTAINER ID IMAGE COMMAND STATUS NAMES
4c01db0b339c ubuntu "/bin/bash" Exited (0) 3 seconds ago happy_moore
Nothing crashed. The image's default command is bash, a shell that reads commands from a terminal. docker run
without -it attaches no terminal, so bash reads "end of input", exits with code 0, and the container is over.
An nginx container keeps running for the opposite reason: its default command is a web server that never exits
on its own.
The COMMAND column is the thing this lesson is about. Where does /bin/bash come from? From the image:
docker inspect --format '{{json .Config.Entrypoint}} {{json .Config.Cmd}}' ubuntu
null ["/bin/bash"]
An image is more than files. It also carries a small configuration, and two fields of it, Entrypoint and Cmd,
say what to run. The Dockerfile instructions ENTRYPOINT and CMD write those fields.
CMD: the default command
The quickest way to run something else is to type it after the image name. Those words replace the image's
CMD:
docker run ubuntu sleep 5 # runs "sleep 5" instead of "/bin/bash"
To make that the default, so nobody has to type it, put it in an image of your own:
FROM ubuntu
CMD ["sleep", "5"]
docker build -t sleeper-cmd .
docker run sleeper-cmd # sleeps 5 seconds
docker run sleeper-cmd sleep 10 # replaces the whole CMD: sleeps 10 seconds
Look at the last line. With CMD alone, changing one number means retyping the whole command, program name
included. CMD is a default that is replaced as a whole; you cannot append to it.
Shell form and exec form
CMD (and ENTRYPOINT) can be written in two forms. They look similar and start different processes.
Parts:
- CMD sleep 5 (shell form)
- /bin/sh -c (process 1)
- sleep 5 (child of the shell)
- CMD ["sleep", "5"] (exec form)
- sleep 5 (process 1, gets signals)
Connections:
- CMD sleep 5 to /bin/sh -c: wraps
- /bin/sh -c to sleep 5: starts
- CMD ["sleep", "5"] to sleep 5: starts directly
| Shell form | Exec form | |
|---|---|---|
| Written as | CMD sleep 5 | CMD ["sleep", "5"] |
| What runs | /bin/sh -c "sleep 5" | sleep 5 |
| Process 1 in the container | the shell | your program |
$VARIABLES, pipes, && | Work, the shell handles them | Do not work, there is no shell |
Stop signal (docker stop) | Goes to the shell, which may not pass it on | Goes to your program |
| Needs a shell in the image | Yes | No |
Exec form is a JSON array: square brackets, double quotes, one item per word. Docker runs the first item as the program and passes the rest as its arguments, with no shell in between. This is the form to use by default.
Shell form is a plain line. Docker wraps it in /bin/sh -c, which is handy when you want the shell's features
and costly in two ways. First, the shell is process 1, and docker stop sends its SIGTERM to process 1. A shell
does not forward signals to its child, so your program never hears "please stop", Docker waits out the grace period
(10 seconds by default), then kills everything. Second, minimal images (distroless, scratch) have no /bin/sh at
all, so shell form cannot even start.
ENTRYPOINT: the fixed program
ENTRYPOINT names the program that always runs. Words after the image name no longer replace it; they are
appended to it as arguments.
FROM ubuntu
ENTRYPOINT ["sleep"]
docker build -t sleeper-no-cmd .
docker run sleeper-no-cmd 10 # runs "sleep 10"
docker run sleeper-no-cmd # runs "sleep": error, missing operand
The image now behaves like the sleep command itself, which is pleasant, but it has lost its default. Put the
two instructions together and you get both:
FROM ubuntu
ENTRYPOINT ["sleep"]
CMD ["5"]
docker build -t sleeper .
docker run sleeper # sleep 5 (ENTRYPOINT + CMD)
docker run sleeper 10 # sleep 10 (ENTRYPOINT + your argument)
Parts:
- ENTRYPOINT (the program), in Stored in the image
- CMD (default arguments), in Stored in the image
- docker run args (typed after the image)
- Container process (ENTRYPOINT + CMD)
Connections:
- ENTRYPOINT to Container process: first
- CMD to Container process: appended
- docker run args to CMD: replaces
That is the whole model. ENTRYPOINT is the program, CMD is its default arguments, and the container runs one
joined to the other. When there is no ENTRYPOINT, as in the ubuntu image, CMD has to hold the program too,
which is why CMD ["sleep", "5"] worked earlier.
Every combination
This table is worth reading once slowly. It follows the one in the Dockerfile reference, with sleep as the example:
| No ENTRYPOINT | ENTRYPOINT ["sleep"] (exec) | ENTRYPOINT sleep (shell) | |
|---|---|---|---|
| No CMD | Nothing to run: error (unless the base image set one) | sleep | /bin/sh -c sleep |
CMD ["5"] (exec) | 5 (tries to run a program called 5) | sleep 5 | /bin/sh -c sleep |
CMD 5 (shell) | /bin/sh -c 5 | sleep /bin/sh -c 5 | /bin/sh -c sleep |
Three things to take from it:
- Only the middle column, first two rows, behaves the way you want. Use exec form for both instructions.
- A shell-form ENTRYPOINT ignores CMD and ignores the arguments of
docker run. The whole right column is the same command. - A shell-form CMD next to an exec-form ENTRYPOINT passes
/bin/sh -c 5as arguments to your program, which is never what you meant.
Under the hood · Details that explain odd behaviour
- Only the last one counts. A Dockerfile may contain several
CMDorENTRYPOINTlines; the last of each wins and the earlier ones are dropped without a warning. - A new ENTRYPOINT clears an inherited CMD. If your base image set a
CMDand your Dockerfile setsENTRYPOINT, the base image'sCMDis reset to empty. DefineCMDagain in your own Dockerfile if you want one. - No shell, no variables.
CMD ["echo", "$HOME"]prints the text$HOME, because expanding variables is the shell's job. When you want that in exec form, start the shell yourself:CMD ["sh", "-c", "echo $HOME"]. execrescues shell form.ENTRYPOINT exec sleep 5makes the shell replace itself withsleep, so the program becomes process 1 after all. Some shells already do this on their own for a single simple command, which is why the "10 seconds to stop" effect does not show up with every base image. Do not rely on it.- Entrypoint scripts. Many official images set
ENTRYPOINTto a small script that prepares things (config files, permissions) and ends withexec "$@", which runs whateverCMDholds as process 1. Thenginximage does this: itsEntrypointis/docker-entrypoint.shand itsCmdisnginx -g "daemon off;". - Being process 1 is special. The kernel ignores signals that process 1 has no handler for, so a program that
never set up a
SIGTERMhandler also sits out the grace period, even in exec form.docker run --initputs a tiny init process in front that forwards signals and cleans up finished child processes.
Overriding at run time
The shape of the command line tells you what goes where:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Everything before the image name is an option for Docker. Everything after it goes to the container and
replaces CMD.
Parts:
- docker run sleeper (nothing extra)
- sleep 5 (image defaults)
- docker run sleeper 10 (one argument)
- sleep 10 (CMD replaced)
- --entrypoint date (docker run ... sleeper)
- date (CMD dropped too)
Connections:
- docker run sleeper to sleep 5: runs
- docker run sleeper 10 to sleep 10: runs
- --entrypoint date to date: runs
| You type | ENTRYPOINT | CMD | The container runs |
|---|---|---|---|
docker run sleeper | from the image | from the image | sleep 5 |
docker run sleeper 10 | from the image | replaced by 10 | sleep 10 |
docker run --entrypoint date sleeper | replaced by date | dropped | date |
docker run --entrypoint echo sleeper hello | replaced by echo | replaced by hello | echo hello |
Two details about --entrypoint catch people out:
- It takes one word, the program. Its arguments go after the image name, like any other
CMDreplacement. - It clears the image's CMD.
docker run --entrypoint date sleeperrunsdate, notdate 5.
Nothing here changes the image. An override belongs to the one container you started with it.
The same two fields in a Pod
Parts:
- ENTRYPOINT (in the image)
- command (in the container spec)
- CMD (in the image)
- args (in the container spec)
Connections:
- command to ENTRYPOINT: replaces
- args to CMD: replaces
Kubernetes stores the same two settings per container in a Pod, under different names:
| In the image (Dockerfile) | In the Pod spec | On docker run |
|---|---|---|
ENTRYPOINT | command | --entrypoint |
CMD | args | the words after the image name |
Using it
Example · See what an image runs before you override it
Do not guess whether an image uses ENTRYPOINT, CMD or both. Ask it:
docker pull nginx
docker inspect --format 'Entrypoint: {{json .Config.Entrypoint}} Cmd: {{json .Config.Cmd}}' nginx
Entrypoint: ["/docker-entrypoint.sh"] Cmd: ["nginx","-g","daemon off;"]
So an nginx container runs /docker-entrypoint.sh nginx -g "daemon off;". If you start it with one extra word,
you replace all three items of Cmd, not just add to them.
The image's page on its registry usually shows the same thing in the Dockerfile. podman and nerdctl accept the
same inspect command.
Example · Build the sleeper image and try every override
# Dockerfile
FROM ubuntu
ENTRYPOINT ["sleep"]
CMD ["5"]
docker build -t sleeper .
docker run --rm sleeper # sleep 5
docker run --rm sleeper 10 # sleep 10
docker run --rm --entrypoint date sleeper # date
docker run --rm --entrypoint echo sleeper hello # echo hello
While one of them is running, docker ps --no-trunc in a second terminal shows the full command in the COMMAND
column, so you can check your reading of the rules against what really started.
Example · Watch the difference between the two forms
Build the same program twice, once in each form, and stop both:
# Dockerfile.shell
FROM ubuntu
ENTRYPOINT sleep 600
# Dockerfile.exec
FROM ubuntu
ENTRYPOINT ["sleep", "600"]
docker build -t sleep-shell -f Dockerfile.shell .
docker build -t sleep-exec -f Dockerfile.exec .
docker run -d --name s1 sleep-shell
docker run -d --name s2 sleep-exec
docker exec s1 ps -o pid,args # is PID 1 "/bin/sh -c sleep 600" or "sleep 600"?
docker exec s2 ps -o pid,args # PID 1 is "sleep 600"
docker run --rm sleep-shell 5 # the 5 is ignored: still sleeps 600 seconds
With the shell-form image, process 1 is /bin/sh -c ... on base images whose shell stays in place, and the extra
argument is always ignored. This is the reason to write ENTRYPOINT in exec form every time.
Example · Open a shell in an image that has an ENTRYPOINT
You want to look around inside the sleeper image. The obvious attempt fails:
docker run -it sleeper bash
sleep: invalid time interval 'bash'
bash replaced CMD, so the container ran sleep bash. Replace the entrypoint instead:
docker run -it --rm --entrypoint bash sleeper
The same thing happens in Kubernetes when you set only args on an image that has an ENTRYPOINT, and the fix is
the same idea: set command.
Tricks
Common mistakes
CMD ["sleep 5"]. One item means one program name. Split it:["sleep", "5"].- Single quotes in exec form.
['sleep', '5']is not JSON; Docker silently falls back to shell form. - Repeating the program after the image name. With
ENTRYPOINT ["sleep"],docker run sleeper sleep 10runssleep sleep 10. Pass only the arguments. - Putting
--entrypointafter the image name. There it is no longer a Docker option; it becomes an argument for the container's program. - Expecting
--entrypointto keep CMD. It clears the image'sCMD. Type the arguments you still need. - Shell-form ENTRYPOINT. It ignores
CMDand run-time arguments, and usually keeps your program from getting the stop signal. - Expecting
$VARto expand in exec form. No shell runs, so nothing expands. Use["sh", "-c", "..."]. - Treating a Pod's
commandas Docker'sCMD.commandis the ENTRYPOINT;argsis the CMD.
Recap
- A container runs one main process and stops when it exits.
docker run ubuntuends at once becausebashhas nothing to read. - The image stores what to run in ENTRYPOINT (the program) and CMD (default arguments, or the whole default command when there is no ENTRYPOINT). The process is ENTRYPOINT followed by CMD.
- Exec form (
["sleep", "5"]) starts the program directly as process 1. Shell form (sleep 5) wraps it in/bin/sh -c; as an ENTRYPOINT it also ignores CMD and arguments. - Words after the image name on
docker runreplace CMD.--entrypointreplaces ENTRYPOINT and clears CMD. - In a Pod the same settings are
command(ENTRYPOINT) andargs(CMD).
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.