ENTRYPOINT vs CMD
ENTRYPOINT defines the executable; CMD provides the default args.
Introduction
ENTRYPOINT defines the executable; CMD provides the default args. Together they decide what runs when the container starts and what users can override.
Purpose of this lesson
This lesson teaches ENTRYPOINT vs CMD as an engineering decision: what problem it solves, when to use it, how to implement it safely, and what signals tell you it is failing.
Understanding the topic
Use this whenever you create or review an application image. Build quality affects deploy speed, security exposure, reproducibility, CI cost, and how quickly engineers can diagnose failures.
Core concepts to understand:
CMD ["node", "server.js"]alone — overridable by run args.ENTRYPOINT ["node"]+CMD ["server.js"]— args overridable, executable fixed.- Always use exec form (JSON array) — handles signals properly.
- Shell form (
CMD node server.js) wraps in/bin/sh -c— breaks signals.
Visual explanation
Architecture or command flow to keep in mind:
# Pattern: entrypoint = binary, CMD = default argsENTRYPOINT ["myapp"]CMD ["--help"]# docker run image → myapp --help# docker run image serve --port=80 → myapp serve --port=80
Step-by-step explanation
- Start from a trusted, pinned base image and document why it fits the runtime.
- Order Dockerfile instructions from least-changing dependency metadata to most-changing source files so the cache stays useful.
- Build locally with BuildKit, inspect layers with
docker history, and run the image with the same command CI will use. - Test shutdown behavior, health checks, non-root execution, and runtime configuration before pushing the image.
- Tag with an immutable version or git SHA, scan the image, and promote the same artifact through environments.
Informative example
Use the example below as a working baseline, then verify the runtime behavior instead of assuming the command or file is correct.
# Pattern: entrypoint = binary, CMD = default argsENTRYPOINT ["myapp"]CMD ["--help"]# docker run image → myapp --help# docker run image serve --port=80 → myapp serve --port=80
A production-minded check usually includes docker ps, docker logs, docker inspect, and one validation from outside the container such as curl, a database connection, or a registry pull.
# Verification loop for ENTRYPOINT vs CMDdocker ps -adocker logs --tail=100 <container-name>docker inspect <container-or-image-name>docker system df
Real-world use
A wrapper entrypoint.sh that runs DB migrations then exec "$@" is the canonical pattern for production images.
Enterprise use cases
In a mature engineering organization, ENTRYPOINT vs CMD is documented as a repeatable pattern with approved base images, ownership labels, CI checks, security expectations, rollback notes, and troubleshooting commands. The difference between a tutorial and production practice is that every container decision must be observable, reviewable, and reversible.
Best practices
- Use exec form (JSON array) — not shell form.
- Make ENTRYPOINT the binary, CMD the default args.
exec "$@"at the end of entrypoint.sh hands PID 1 to your app.
Common mistakes
- Shell form swallows SIGTERM — container takes 10s to die.
- Forgetting
execin shell wrappers — your app isn't PID 1.
Debugging tips
- Use
docker build --progress=plainwhen BuildKit output hides the failing build step. - Run the image with a temporary shell or overridden command to inspect files, users, environment, and entrypoint behavior.
- Check
docker historyfor accidental secrets, unexpectedly large layers, and cache-busting instructions.
Optimization strategies
- Prefer explicit names, labels, tags, and networks so cleanup and debugging stay predictable.
- Pin versions for repeatability, then update intentionally through a scheduled base-image refresh.
- Use
docker system dfand targeted prune commands to control local and CI disk growth.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1QuestionDifference between ENTRYPOINT and CMD?+
Answer
2QuestionWhy use exec form?+
Answer
3QuestionWhy does PID 1 matter?+
Answer
Hands-on exercise
Create a small lab for ENTRYPOINT vs CMD: run the example, inspect the created Docker object, intentionally introduce one mistake, and record the command that reveals the failure. The goal is not just to make the happy path work; it is to build operational reflexes.
# Hands-on lab scaffoldmkdir -p docker-entrypoint-vs-cmd-labcd docker-entrypoint-vs-cmd-lab# Add the Dockerfile, compose.yml, or command from this lesson.# Then run one happy-path test and one broken-path test.docker versiondocker infodocker system df
Summary
ENTRYPOINT vs CMD matters because Docker is not only a packaging tool; it is a runtime, build, networking, storage, and delivery workflow. Treat each lesson as a production habit: make it repeatable, inspectable, secure, and easy to debug.