CMD vs. ENTRYPOINT

Difficulty: Intermediate

Question

Explain the difference between CMD and ENTRYPOINT in a Dockerfile. How do they interact, and how does each behave when you pass arguments to docker run?

Answer

ENTRYPOINT defines the executable that always runs when the container starts, so the container behaves like a command. CMD supplies the default arguments, or the default command if there is no ENTRYPOINT, and is replaced by whatever you type after the image name in docker run. Used together in exec form, ENTRYPOINT is the fixed program and CMD is the overridable default input.

How Docker combines them

At startup Docker builds the final command line as ENTRYPOINT + CMD. Arguments after the image name in docker run replace CMD only. To replace ENTRYPOINT you must use --entrypoint.

Dockerfiledocker run img runsdocker run img hi runs
CMD ["echo", "hello"] onlyecho hellohi (CMD replaced entirely)
ENTRYPOINT ["echo"] onlyechoecho hi
ENTRYPOINT ["echo"] + CMD ["hello"]echo helloecho hi
ENTRYPOINT echo (shell form) + any CMD/bin/sh -c echo/bin/sh -c echo (CMD and args ignored)

The last row is the classic trap: shell-form ENTRYPOINT ignores both CMD and runtime arguments, and wraps your process in /bin/sh -c, so it does not receive SIGTERM directly.

When to use which

  • Only CMD: general purpose images where users often want to run something else, such as docker run my-app npm test or docker run python:3.13 bash.
  • ENTRYPOINT + CMD: tool-style images. For example an image wrapping a CLI: ENTRYPOINT ["aws"], CMD ["--help"], so docker run awscli s3 ls just works.
  • Entrypoint script + CMD: the pattern used by official images like postgres and nginx. A docker-entrypoint.sh does setup (wait for a dependency, render config from env vars, run migrations), then ends with exec "$@" to hand control to CMD. The exec replaces the shell so your app becomes PID 1 and gets signals.

Rules and gotchas

  • Only the last CMD and the last ENTRYPOINT in a Dockerfile take effect.
  • If a child image sets ENTRYPOINT, any CMD inherited from the base image is reset, so you must set CMD again.
  • docker run --entrypoint sh img overrides the entrypoint; anything after the image name becomes its arguments. Note --entrypoint takes a single executable, not a full command string.
  • Exec form (JSON array) does no variable expansion: CMD ["echo", "$HOME"] prints $HOME literally. Use an explicit sh -c or an entrypoint script if you need expansion.
  • In Compose and Kubernetes the names differ: Compose uses entrypoint and command; Kubernetes uses command (overrides ENTRYPOINT) and args (overrides CMD). Mixing these up is a common production bug.

A practical mental model

Think of ENTRYPOINT as the verb and CMD as the default object. A payments worker image might have ENTRYPOINT ["node", "worker.js"] and CMD ["--queue=settlements"], so the same image can be started as --queue=refunds for another deployment without rebuilding.

How the container start command is assembled. Runtime arguments replace CMD; only --entrypoint replaces ENTRYPOINT.
  1. ENTRYPOINT ["node", "worker.js"]: Fixed executable unless --entrypoint is used.
  2. CMD ["--queue=settlements"]: Default arguments from the Dockerfile.
  3. docker run img --queue=refunds: Arguments after the image name replace CMD.
  4. node worker.js --queue=refunds: Final command started as PID 1.

Code examples

ENTRYPOINT and CMD together

# Dockerfile
# FROM alpine:3.20
# ENTRYPOINT ["ping", "-c", "3"]
# CMD ["localhost"]

docker build -t pinger .

docker run --rm pinger              # ping -c 3 localhost
docker run --rm pinger google.com   # ping -c 3 google.com (CMD replaced)

# Replace the entrypoint itself to debug the image
docker run --rm -it --entrypoint sh pinger

The entrypoint is fixed, CMD is the default target, and runtime arguments swap the target without touching the entrypoint.

Entrypoint script pattern with exec "$@"

#!/bin/sh
# docker-entrypoint.sh
set -e

# One-time setup before the real process starts
if [ -n "$RUN_MIGRATIONS" ]; then
  node scripts/migrate.js
fi

# Replace this shell with CMD so the app is PID 1 and receives SIGTERM
exec "$@"

# Dockerfile:
# COPY docker-entrypoint.sh /usr/local/bin/
# ENTRYPOINT ["docker-entrypoint.sh"]
# CMD ["node", "server.js"]

The script runs setup, then exec hands over to CMD, keeping graceful shutdown working while still letting users override CMD.

Key points

  • Final command = ENTRYPOINT + CMD; docker run arguments replace CMD only.
  • Use --entrypoint to override ENTRYPOINT at runtime.
  • Shell-form ENTRYPOINT ignores CMD and runtime args and breaks signal handling.
  • Only the last CMD and last ENTRYPOINT count; setting ENTRYPOINT resets inherited CMD.
  • Kubernetes command maps to ENTRYPOINT and args maps to CMD.

Common follow-up questions

  • What happens if a Dockerfile has multiple CMD instructions?
  • How do you override an ENTRYPOINT at runtime, in Compose, and in Kubernetes?
  • Why do entrypoint scripts end with exec "$@"?
  • What is the difference between exec form and shell form?

Interview tips

  • Draw or describe the ENTRYPOINT + CMD concatenation; it shows you know the actual mechanics rather than a slogan.
  • Bring up the shell-form trap and signal handling, which separates people who have debugged slow shutdowns from those who have not.
  • Mention the Kubernetes command/args mapping if the role involves K8s; interviewers often ask it next.

Concepts covered

CMD, ENTRYPOINT, Exec Form, --entrypoint flag, Entrypoint Scripts

More Docker & Containers questions