Sub topic 8
Setting the default command with CMD and ENTRYPOINT
This chapter assumes you can write a basic Dockerfile with FROM, WORKDIR, COPY, and RUN, and that you can run docker build and docker run. It covers how an image decides what to run when a container starts.
Build time versus run time
RUN executes during docker build and its result is saved in a layer. CMD and ENTRYPOINT do not execute during the build. They record metadata in the image that says what the container should run when it starts.
CMD: a default that is easy to replace
CMD sets the default command. Anything you put after the image name in docker run replaces it.
FROM python:3.12-slim
WORKDIR /app
COPY . .
CMD ["python", "app.py"]docker build -t myapp:1.0 .
docker run myapp:1.0
docker run myapp:1.0 python --versionThe first docker run starts python app.py. The second ignores CMD and prints the Python version.
ENTRYPOINT: the fixed executable
ENTRYPOINT names the program that always runs. Arguments from docker run are appended to it instead of replacing it.
FROM python:3.12-slim
WORKDIR /app
COPY . .
ENTRYPOINT ["python", "app.py"]
CMD ["--port", "8000"]docker run myapp:2.0
docker run myapp:2.0 --port 9000The first command runs python app.py --port 8000. The second runs python app.py --port 9000. ENTRYPOINT supplies the program and CMD supplies default arguments that the user can replace.
To replace the entrypoint itself, for example to open a shell for debugging, use the --entrypoint flag:
docker run --rm -it --entrypoint sh myapp:2.0Exec form versus shell form
Both instructions accept two syntaxes.
| Form | Example | How it runs |
|---|---|---|
| Exec form | `CMD ["python", "app.py"]` | Runs the program directly as the main process (PID 1). |
| Shell form | `CMD python app.py` | Runs `/bin/sh -c "python app.py"`, so the shell is PID 1. |
Prefer exec form. With shell form, signals such as SIGTERM from docker stop go to the shell, which may not pass them on. The container then waits for the stop timeout and is killed. Exec form also needs valid JSON, so use double quotes, not single quotes.
Exec form does not run a shell, so it does not expand variables such as $HOME. If you need expansion, call the shell yourself: CMD ["sh", "-c", "echo $HOME"].
Checking what the image stores
docker image inspect shows the recorded values, which helps when a container does not start as you expect.
docker image inspect myapp:2.0 --format '{{.Config.Entrypoint}} {{.Config.Cmd}}'This prints [python app.py] [--port 8000].
Common errors
- Using shell form for
ENTRYPOINT. TheCMDvalue and anydocker runarguments are then ignored, because they become arguments tosh -c, not to your program. - Using single quotes in exec form. Docker cannot parse it as JSON and falls back to shell form.
- Expecting
docker run myapp:2.0 shto open a shell whenENTRYPOINTis set. It runspython app.py sh. Use--entrypoint.
Exercise
An image has ENTRYPOINT ["python", "app.py"] and CMD ["--port", "8000"]. What command runs for docker run myapp:2.0 --debug, and why?
Show answer
It runs python app.py --debug. The argument --debug replaces CMD, so --port 8000 is dropped. ENTRYPOINT stays fixed and receives the new arguments.
Exercise
Why is CMD ["python", "app.py"] usually better than CMD python app.py?
Show answer
Exec form runs Python directly as PID 1, so signals like SIGTERM from docker stop reach the application. Shell form runs sh -c, and the shell may not forward signals.