Difficulty: Intermediate
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?
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.
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.
| Dockerfile | docker run img runs | docker run img hi runs |
|---|---|---|
CMD ["echo", "hello"] only | echo hello | hi (CMD replaced entirely) |
ENTRYPOINT ["echo"] only | echo | echo hi |
ENTRYPOINT ["echo"] + CMD ["hello"] | echo hello | echo 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.
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.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.CMD and the last ENTRYPOINT in a Dockerfile take effect.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.CMD ["echo", "$HOME"] prints $HOME literally. Use an explicit sh -c or an entrypoint script if you need expansion.entrypoint and command; Kubernetes uses command (overrides ENTRYPOINT) and args (overrides CMD). Mixing these up is a common production bug.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.
# 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.
#!/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.
CMD, ENTRYPOINT, Exec Form, --entrypoint flag, Entrypoint Scripts