CMD vs ENTRYPOINT
Definition
Instructions that define the default command executed when a container starts. `CMD` provides default arguments that a user can easily override. `ENTRYPOINT` configures a container to run as an unyielding executable.
Explain Like I'm New
When the container wakes up, what is its primary job? `CMD` is a suggestion: 'When you wake up, run `python main.py`. (But if the user overrides it by typing `bash`, do that instead).' `ENTRYPOINT` is a strict law: 'When you wake up, you MUST run `python`. If the user passes arguments, append them, but you ARE a python script.'
Real World Example
An NGINX web server uses `CMD ["nginx", "-g", "daemon off;"]`. When the container starts, the web server boots. But if you want to debug, you can run `docker run -it nginx bash`, completely overriding the CMD and starting a bash shell instead.
Common Use Cases
- •Defining container behavior
- •Creating CLI tools
Interview Questions
basic
- If a Dockerfile has five `CMD` instructions scattered throughout it, what happens?
intermediate
- How do `CMD` and `ENTRYPOINT` work together in the same Dockerfile?