Docker interview question · Most asked
CMD or ENTRYPOINT: what is the difference, and what are exec and shell forms?
Summary
ENTRYPOINT sets the executable the container runs; CMD provides default arguments (or a default command if there is no entrypoint), which docker run image args overrides. The exec form (["node", "server.js"]) runs the process directly; the shell form (node server.js) wraps it in /bin/sh -c.
Longer Answer
Prefer the exec form: with the shell form, the shell becomes PID 1 and typically does not forward signals, so docker stop (SIGTERM) does not reach your application, which is killed after the timeout instead of shutting down gracefully. A common pattern is an ENTRYPOINT script that prepares the environment and ends with exec "$@" so the real process replaces the shell, plus CMD for default arguments.
Key Points
- ENTRYPOINT = executable; CMD = default args.
- Exec form runs the process directly.
- Shell form breaks signal handling.
- Entrypoint scripts end with exec "$@".
More most-asked Docker questions
- How do you secure Docker containers and images?
- How does Docker build caching work, and how do you order a Dockerfile to use it well?
- What is a container, and how does it differ from a virtual machine?
- What is the difference between an image and a container, and how do image layers work?
- What are Dockerfile best practices for production images?
- ARG or ENV: what is the difference, and how do you pass secrets into a build safely?
- What are multi-stage builds, and why use them?
- How do you make Docker images smaller?