Sub topic 13
Combining ENTRYPOINT and CMD
This chapter assumes you can write a basic Dockerfile, run docker build and docker run, and know the difference between exec form and shell form. Earlier chapters covered CMD and ENTRYPOINT separately. Here you will see how they work together.
The working model
When Docker starts a container, it builds one command line from both instructions:
- ENTRYPOINT is the executable. It is fixed unless you override it on purpose.
- CMD supplies default arguments. Anything you type after the image name in
docker runreplaces CMD.
The final command is the ENTRYPOINT followed by either the CMD or the arguments you passed.
A worked example
This Dockerfile fixes the program as ping and sets default arguments.
FROM alpine:3.20
ENTRYPOINT ["ping"]
CMD ["-c", "3", "localhost"]Build and run it two ways.
docker build -t pinger .
docker run --rm pinger
docker run --rm pinger -c 1 127.0.0.1The first run executes ping -c 3 localhost. The second replaces the CMD, so it executes ping -c 1 127.0.0.1. In both cases ping runs, because ENTRYPOINT was not changed.
Use exec form for both
The combination only works as described when both instructions use exec form (the JSON array syntax). If ENTRYPOINT uses shell form, Docker runs it as /bin/sh -c "...", and CMD and run arguments are not appended to it. They are silently ignored.
The table shows how the form of each instruction affects the result for ENTRYPOINT plus CMD ["a"].
| ENTRYPOINT | CMD | Resulting command |
|---|---|---|
| `["ping"]` (exec) | `["-c", "3"]` (exec) | `ping -c 3` |
| `ping` (shell) | `["-c", "3"]` (exec) | `/bin/sh -c ping`, CMD ignored |
| `["ping"]` (exec) | `-c 3` (shell) | `ping /bin/sh -c "-c 3"`, almost never intended |
Common errors
- Passing a command instead of arguments. With
ENTRYPOINT ["ping"], runningdocker run pinger lsexecutesping ls, notls. Arguments after the image name go to the ENTRYPOINT. - Expecting CMD to be appended. Run arguments replace CMD completely. To keep part of the defaults, repeat them yourself.
- Forgetting that ENTRYPOINT can be overridden.
docker run --entrypoint sh pingerreplaces it. This is useful for debugging, such as opening a shell in the image. - Putting the program in CMD only. Then
docker run image -c 1tries to run-cas a program and fails, because the arguments replace the whole command.
Choosing a pattern
Use ENTRYPOINT with CMD when the image behaves like a command-line tool and users mostly change the options. Use CMD alone when users may want to run something entirely different, such as a shell. Use ENTRYPOINT alone when the image has no sensible default arguments.
Exercise
A Dockerfile contains ENTRYPOINT ["python", "app.py"] and CMD ["--port", "8000"]. What command runs for docker run myimage and for docker run myimage --port 9000?
Show answer
The first runs python app.py --port 8000. The second runs python app.py --port 9000, because the run arguments replace CMD while ENTRYPOINT stays the same.
Exercise
A Dockerfile has ENTRYPOINT python app.py (shell form) and CMD ["--debug"]. Why is --debug never passed to the program?
Show answer
Shell-form ENTRYPOINT runs as /bin/sh -c "python app.py", and Docker does not append CMD or run arguments to it. Switching to ENTRYPOINT ["python", "app.py"] fixes it.