Sub topic 11
CMD: the default command
This chapter assumes you can write a basic Dockerfile, run docker build and docker run, and know the difference between shell form and exec form for RUN. It covers what CMD does, how to override it, and the mistakes that trip people up most often.
What CMD does
CMD sets the default command a container runs when it starts. It does not run during the build. RUN executes at build time and bakes its result into a layer. CMD only records metadata in the image, and Docker uses that metadata at docker run time.
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]When you run docker run myimage, the container starts python app.py. The command is a default, not a fixed rule.
Overriding CMD
Anything you put after the image name in docker run replaces CMD entirely.
docker run --rm myimage
docker run --rm myimage python --version
docker run --rm -it myimage bashThe first line runs python app.py. The second runs python --version instead. The third starts a shell, which is handy for debugging an image.
Three forms of CMD
CMD has three forms.
| Form | Syntax | Behavior |
|---|---|---|
| Exec form | `CMD ["python", "app.py"]` | Runs the program directly, with no shell. |
| Shell form | `CMD python app.py` | Runs `/bin/sh -c "python app.py"`. |
| Default arguments | `CMD ["-c", "3", "localhost"]` | Supplies arguments to an `ENTRYPOINT`. |
The exec form must be a JSON array, so use double quotes. Single quotes make Docker treat the line as shell form, which usually fails in confusing ways.
The shell form expands variables because a shell is running. The exec form does not.
# Shell form: run through /bin/sh -c, so $HOME is expanded
CMD echo "home is $HOME"
# Exec form: no shell, so $HOME is printed literally
# CMD ["echo", "home is $HOME"]If you need variable expansion in exec form, call a shell yourself: CMD ["sh", "-c", "echo home is $HOME"].
Only the last CMD counts
A Dockerfile can contain several CMD lines, but only the last one takes effect. Earlier ones are ignored without any warning. This often happens when you build on a base image that already has a CMD: your own CMD replaces the inherited one.
How CMD is chosen at startup
Common errors
- Using shell form and expecting clean shutdown. The container takes about 10 seconds to stop, because Docker waits and then force-kills it.
- Writing exec form with single quotes.
CMD ['python', 'app.py']is not valid JSON, so Docker treats it as shell form. - Expecting
$VARto expand in exec form. It prints literally, as shown above. - Forgetting that
docker run image argsreplaces the wholeCMD. If you only want to change one argument, you must repeat the others, or move the fixed part intoENTRYPOINT. The chapters that follow cover that pattern. - Relying on
CMDfor setup work. Anything that should happen once at build time belongs inRUN.
Exercise
A Dockerfile ends with CMD ["python", "app.py"]. You run docker run myimage python --version. What does the container run, and why?
Show answer
It runs python --version and does not start app.py. Any command given after the image name in docker run replaces the image's CMD completely.
Exercise
What does this line print, and how would you change it so $HOME expands? CMD ["echo", "home is $HOME"]
Show answer
It prints home is $HOME literally, because exec form runs without a shell. Use shell form (CMD echo "home is $HOME") or call a shell explicitly: CMD ["sh", "-c", "echo home is $HOME"].