CMD and ENTRYPOINT
- Docker & Kubernetes course
- Beginner
- Lesson 23/82
- 4 min
Learn to set the container start command with CMD.
CMD and ENTRYPOINT
CMD is the container's default command; it is completely replaced by docker run image other-command. ENTRYPOINT is the fixed program; the arguments given to docker run are appended to it. When used together, CMD becomes the default arguments:
ENTRYPOINT ["ping"]
CMD ["-c", "3", "localhost"]
# docker run pinger -> ping -c 3 localhost
# docker run pinger -c 1 1.1.1.1 -> ping -c 1 1.1.1.1Exec form and shell form
CMD ["python", "app.py"] (a JSON array) is the exec form: the program becomes PID 1 directly and receives the SIGTERM sent by docker stop. CMD python app.py is the shell form: the command runs inside /bin/sh -c, the signal goes to the shell and the application may not shut down cleanly.
CMD ["python", "app.py"] # recommended
CMD python app.py # signals stay in the shellPractice A tool with default arguments
Write a Dockerfile based on alpine:3.20: ENTRYPOINT in exec form is ping; CMD gives the default arguments -c, 3, localhost in exec form.
Starter code
# buraya yazSample solution
FROM alpine:3.20
ENTRYPOINT ["ping"]
CMD ["-c", "3", "localhost"]Expected output
PING localhost (127.0.0.1): 56 data bytes 64 bytes from 127.0.0.1: seq=0 ttl=64 time=0.051 ms 3 packets transmitted, 3 packets received, 0% packet loss
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 With ENTRYPOINT ["ping"] and CMD ["localhost"], what does docker run img 8.8.8.8 run?
- ping 8.8.8.8 Correct answer
- ping localhost 8.8.8.8
- 8.8.8.8
- ping localhost
Explanation: The given arguments replace CMD and are appended to ENTRYPOINT.
2 Write the start command in exec form.
CMD ____
CMD ["python", "app.py"]
Explanation: The exec form is a JSON array; double quotes are required.
3 Match.
| Term | Meaning |
|---|---|
CMD |
Replaceable default |
ENTRYPOINT |
Fixed program |
["a", "b"] |
Exec form |
CMD a b |
Shell form |