CMD vs ENTRYPOINT
Prerequisites
1. Docker CMD vs ENTRYPOINT
This section explains the roles of CMD and ENTRYPOINT, how they work together, and what happens when you override them using --entrypoint.
ENTRYPOINT = fixed executable (program) CMD = default arguments
1
| ENTRYPOINT(Not Replaceable) + CMD(Fully Replaceable) = final command
|
1
2
| ENTRYPOINT = program
CMD = default parameter
|
1-1. CMD Only
CMD is fully replaceable
1
2
| FROM ubuntu:22.04
CMD ["echo", "hello"]
|
✔️ Run
same as:
1
| docker run my-image echo hello
|
✔️ Override CMD
same as:
👉 Actual execution:
1-2. ENTRYPOINT Only
ENTRYPOINT is not replaced, arguments are appended
1
2
| FROM ubuntu:22.04
ENTRYPOINT ["echo", "hello"]
|
✔️ Run
1
| docker run my-image world
|
same as:
1
| docker run my-image echo hello world
|
👉 Actual execution:
✔️ Override ENTRYPOINT → X
same as:
1
| docker run my-image echo hello ls
|
👉 Actual execution:
1-3. ENTRYPOINT + CMD
CMD is fully replaceable
ENTRYPOINT is not replaced, arguments are appended
1
2
3
| FROM ubuntu:22.04
ENTRYPOINT ["echo"]
CMD ["hello"]
|
✔️ Run (default)
same as:
1
| docker run my-image echo hello
|
👉 Actual execution:
✔️ Run (override argument)
1
| docker run my-image world
|
same as:
1
| docker run my-image echo world
|
👉 Actual execution:
2. Practical Example (Python)
1
2
3
| FROM python:3.10
ENTRYPOINT ["python"]
CMD ["app.py"]
|
✔️ Default
✔️ Change argument
1
| docker run my-image test.py
|
✔️ Invalid argument case
👉 Actual execution:
👉 Result:
1
| python: can't open file 'ls': No such file or directory
|
3. Overriding ENTRYPOINT with --entrypoint
CMD provides default arguments, ENTRYPOINT defines the executable, and --entrypoint allows you to override it at runtime.
1
| docker run -it --entrypoint bash my-image
|
✔️ What happens?
👉 ENTRYPOINT is replaced:
- ENTRYPOINT → replaced
- CMD → ignored
| Case | Result |
|---|
| docker run image | ENTRYPOINT + CMD |
| docker run image arg | ENTRYPOINT + arg |
| docker run –entrypoint bash image | bash only |
3-1. Why Use --entrypoint?
✔️ Debugging
1
| docker run -it --entrypoint bash my-image
|
- Access container manually
- Inspect files, logs, environment
✔️ Bypass default execution
If container always runs an app:
1
| ENTRYPOINT ["python", "app.py"]
|
👉 You cannot stop it normally
👉 But:
1
| docker run -it --entrypoint bash my-image
|
👉 You can enter container without running the app