Docker Course
Docker
/
Advanced

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?

Flash Cards

Question

Five CMD instructions?

Click to reveal answer
Answer

Docker completely ignores the first four. Only the absolute last `CMD` instruction in the file will take effect.

Question

Work together?

Click to reveal answer
Answer

If you use both, `ENTRYPOINT` becomes the un-changeable executable, and `CMD` becomes the default arguments fed into it. Example: `ENTRYPOINT ["ping"]` `CMD ["google.com"]` By default, it pings google. If the user types `docker run myimage yahoo.com`, it overrides the CMD and pings yahoo instead.