Skip to content
LF Cert Trainer

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

1/12Commands and arguments in Docker
0:00 / 4:26

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:

  1. What decides which process a container runs?
  2. How do CMD and ENTRYPOINT differ, and how do they combine?
  3. What exactly does docker run replace 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.

Shell form and exec form, as processeswrapsstartsstarts directlyCMD sleep 5shell form/bin/sh -cprocess 1sleep 5child of the shellCMD ["sleep", "5"]exec formsleep 5process 1, gets signals
Shell form and exec form, as processes. Shell form puts a shell between the container and your program. Exec form starts the program itself, so it is process 1 and receives the stop signal.

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 formExec form
Written asCMD sleep 5CMD ["sleep", "5"]
What runs/bin/sh -c "sleep 5"sleep 5
Process 1 in the containerthe shellyour program
$VARIABLES, pipes, &&Work, the shell handles themDo not work, there is no shell
Stop signal (docker stop)Goes to the shell, which may not pass it onGoes to your program
Needs a shell in the imageYesNo

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)
How the final command is put togetherStored in the imagefirstappendedreplacesENTRYPOINTthe programCMDdefault argumentsdocker run argstyped after the imageContainer processENTRYPOINT + CMD
How the final command is put together. The process a container runs is ENTRYPOINT followed by CMD. Anything you type after the image name on docker run takes the place of CMD, and leaves ENTRYPOINT alone.

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 ENTRYPOINTENTRYPOINT ["sleep"] (exec)ENTRYPOINT sleep (shell)
No CMDNothing 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 5sleep /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 5 as 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 CMD or ENTRYPOINT lines; 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 CMD and your Dockerfile sets ENTRYPOINT, the base image's CMD is reset to empty. Define CMD again 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"].
  • exec rescues shell form. ENTRYPOINT exec sleep 5 makes the shell replace itself with sleep, 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 ENTRYPOINT to a small script that prepares things (config files, permissions) and ends with exec "$@", which runs whatever CMD holds as process 1. The nginx image does this: its Entrypoint is /docker-entrypoint.sh and its Cmd is nginx -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 SIGTERM handler also sits out the grace period, even in exec form. docker run --init puts 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.

Three ways to run the same imagerunsrunsrunsdocker run sleepernothing extrasleep 5image defaultsdocker run sleeper 10one argumentsleep 10CMD replaced--entrypoint datedocker run ... sleeperdateCMD dropped too
Three ways to run the same image. The image has ENTRYPOINT ["sleep"] and CMD ["5"]. Arguments after the image name replace CMD. Only the --entrypoint flag replaces ENTRYPOINT, and it drops the image's CMD as well.

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 typeENTRYPOINTCMDThe container runs
docker run sleeperfrom the imagefrom the imagesleep 5
docker run sleeper 10from the imagereplaced by 10sleep 10
docker run --entrypoint date sleeperreplaced by datedroppeddate
docker run --entrypoint echo sleeper helloreplaced by echoreplaced by helloecho hello

Two details about --entrypoint catch people out:

  • It takes one word, the program. Its arguments go after the image name, like any other CMD replacement.
  • It clears the image's CMD. docker run --entrypoint date sleeper runs date, not date 5.

Nothing here changes the image. An override belongs to the one container you started with it.

The same two fields in a Pod

The same two fields in a PodreplacesreplacesENTRYPOINTin the imagecommandin the container specCMDin the imageargsin the container spec
The same two fields in a Pod. A Pod spec has one field for each Docker instruction, under different names. The next lesson is about these two fields.

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 specOn docker run
ENTRYPOINTcommand--entrypoint
CMDargsthe 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 10 runs sleep sleep 10. Pass only the arguments.
  • Putting --entrypoint after the image name. There it is no longer a Docker option; it becomes an argument for the container's program.
  • Expecting --entrypoint to keep CMD. It clears the image's CMD. Type the arguments you still need.
  • Shell-form ENTRYPOINT. It ignores CMD and run-time arguments, and usually keeps your program from getting the stop signal.
  • Expecting $VAR to expand in exec form. No shell runs, so nothing expands. Use ["sh", "-c", "..."].
  • Treating a Pod's command as Docker's CMD. command is the ENTRYPOINT; args is the CMD.

Recap

  • A container runs one main process and stops when it exits. docker run ubuntu ends at once because bash has 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 run replace CMD. --entrypoint replaces ENTRYPOINT and clears CMD.
  • In a Pod the same settings are command (ENTRYPOINT) and args (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.