Q: How do you override the default CMD when running a container?
To override the default CMD when running a container, put the new command after the image name: docker run my-app:1.0 python manage.py migrate. Whatever follows the image replaces CMD.
Want to master this scenario in a live sandbox? KodeKloud's Docker Certified Associate (DCA) Hands-On Lab Course covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: To <strong>override the default CMD</strong> when running a container, put the new command after the image name: <code>docker run my-app:1.0 python manage.py migrate</code>. Whatever follows the image replaces <code>CMD</code>. The twist interviewers look for is <code>ENTRYPOINT</code>: if the image defines one in exec form, your arguments are appended to it instead of replacing it, and you need <code>--entrypoint</code> to change the program itself. Getting this right matters for running migrations, one-off admin tasks and opening a debug shell in production images without rebuilding them.
Understand the Default CMD and ENTRYPOINT Together
The two instructions combine; the combination decides what runs.
- CMD only: arguments after the image name replace CMD completely.
- ENTRYPOINT (exec form) plus CMD: CMD supplies default arguments; run-time arguments replace CMD and are appended to ENTRYPOINT.
- ENTRYPOINT shell form:
ENTRYPOINT command argruns under/bin/sh -cand ignores CMD and run-time arguments, which also stops signals reaching your app. - Overriding CMD never clears ENTRYPOINT: only
--entrypointchanges it.
Override CMD in Practice
One image, three different invocations.
# Dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY . .
ENTRYPOINT ["python", "-m", "app"]
CMD ["serve", "--port", "8080"]
docker run --rm my-app:1.0 # python -m app serve --port 8080
docker run --rm my-app:1.0 migrate # python -m app migrate
docker run --rm --entrypoint sh my-app:1.0 -c 'env | sort' # replaces ENTRYPOINT
docker run --rm -it --entrypoint "" my-app:1.0 python --version
- Default run: uses ENTRYPOINT plus CMD from the image.
- Replace CMD: arguments after the image name become the new CMD.
- Replace ENTRYPOINT:
--entrypointtakes a single executable; its arguments follow the image name. - Clear ENTRYPOINT:
--entrypoint ""makes the trailing arguments the whole command.
Verify What Will Actually Run
Check the effective command before and after starting the container.
docker inspect --format '{{json .Config.Entrypoint}} {{json .Config.Cmd}}' my-app:1.0
# ["python","-m","app"] ["serve","--port","8080"]
docker run -d --name job1 my-app:1.0 migrate
docker inspect -f '{{.Path}} {{.Args}}' job1
docker wait job1
- Image defaults:
docker inspect --format '{{json .Config.Entrypoint}} {{json .Config.Cmd}}' my-app:1.0. - Running container:
docker ps --no-truncshows the COMMAND column;docker inspecton the container shows.Pathand.Args. - Process tree:
docker top <container>confirms PID 1 is your program and not a shell wrapper. - Exit status:
docker inspect -f '{{.State.ExitCode}}'for one-off jobs, which CI should assert on.
Edge Cases and Failure Modes
Most surprises come from quoting and from the ENTRYPOINT interaction.
- Executable not found:
exec: "migrate": executable file not found in $PATHmeans the image has no ENTRYPOINT and you passed an argument expecting one; either name the full command or use--entrypoint. - Shell features: pipes,
&&and variable expansion needsh -c '...'; exec form does not invoke a shell. - Debug shell:
docker run --rm -it --entrypoint sh my-app:1.0works even when ENTRYPOINT starts the server; it fails on distroless images with no shell. - Compose:
command:overrides CMD andentrypoint:overrides ENTRYPOINT; settingentrypointalso discards the image's default CMD.
Production Practice
Design images so overrides are safe and predictable.
- Exec form always: use JSON arrays so the app is PID 1 and receives SIGTERM for graceful shutdown.
- Subcommand pattern: ENTRYPOINT as the binary and CMD as the default subcommand lets the same image run
serve,migrateandworker. - Jobs: run migrations as a Kubernetes Job or a CI step with an overridden command rather than baking them into startup.
- Guardrails: avoid entrypoint scripts that swallow arguments; end them with
exec "$@"so overrides still work.
- Anything after the image name in docker run replaces CMD; ENTRYPOINT stays unless I pass --entrypoint.
- I confirm the effective command with docker inspect on Config.Entrypoint and Config.Cmd, and docker ps --no-trunc on the running container.
- I design images with exec-form ENTRYPOINT plus a default CMD so migrations and debug shells run from the same image, and I map this to command and args in Kubernetes.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Passing sh as an argument to an image with an ENTRYPOINT The argument is appended to the entrypoint instead of replacing it; use --entrypoint sh.
- Using shell-form ENTRYPOINT or CMD The command runs under /bin/sh -c, so the app is not PID 1 and may miss SIGTERM; CMD and arguments can also be ignored.
- Expecting pipes or && in a docker run override to work directly There is no shell by default; wrap the command in sh -c '...'.
- Writing an entrypoint script that does not end with exec "$@" The script swallows the override, or leaves a shell as PID 1.