⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Docker & Containers Interview Questions Scenario 35 of 159 in Docker & Containers
Senior DevOps / SRE [L2] Docker Production Scenario [L2]
🎯 Target Role / Context: Docker Scenario #35 (L2) · Docker

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.

#Docker #L2 #Containers #Linux
🎙️ Candidate Opening & Architectural Context
"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."
Advertisement
⚡ Recommended Practice Lab

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.

1️⃣

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 arg runs under /bin/sh -c and ignores CMD and run-time arguments, which also stops signals reaching your app.
  • Overriding CMD never clears ENTRYPOINT: only --entrypoint changes it.
2️⃣

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: --entrypoint takes a single executable; its arguments follow the image name.
  • Clear ENTRYPOINT: --entrypoint "" makes the trailing arguments the whole command.
Advertisement
3️⃣

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-trunc shows the COMMAND column; docker inspect on the container shows .Path and .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.
4️⃣

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 $PATH means 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 need sh -c '...'; exec form does not invoke a shell.
  • Debug shell: docker run --rm -it --entrypoint sh my-app:1.0 works even when ENTRYPOINT starts the server; it fails on distroless images with no shell.
  • Compose: command: overrides CMD and entrypoint: overrides ENTRYPOINT; setting entrypoint also discards the image's default CMD.
Pro Tip: Kubernetes mapping: <code>command</code> overrides ENTRYPOINT and <code>args</code> overrides CMD. If you set <code>command</code> but not <code>args</code>, the image's CMD is ignored.
5️⃣

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, migrate and worker.
  • 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.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: With an ENTRYPOINT in exec form, extra arguments after the image name are <em>appended</em> to it; to run a different program (for example a shell), you must add <code>--entrypoint</code>."
⚡ 60-Second Elevator Pitch Talking Points
  • 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.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. 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.
  2. 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.
  3. Expecting pipes or && in a docker run override to work directly There is no shell by default; wrap the command in sh -c '...'.
  4. Writing an entrypoint script that does not end with exec "$@" The script swallows the override, or leaves a shell as PID 1.
Advertisement
Want more Docker & Containers scenarios?
Explore our complete collection of scenario-based Docker & Containers interview runbooks.
Browse All Docker & Containers Questions →