How does CMD differ from ENTRYPOINT?

QuestionsQuestions8 SkillsProDec, 24 2025
0106

This is a common point of confusion when starting with Docker! Both CMD and ENTRYPOINT define what happens when a container starts, but they behave differently when you pass arguments to the docker run command.

Think of it this way: ENTRYPOINT is the "Command," and CMD is the "Default Argument."

1. Overriding Behavior

  • CMD: Is easily overwritten. If you provide a command at the end of docker run, Docker ignores the CMD entirely.
  • ENTRYPOINT: Is not easily overwritten. If you provide arguments at the end of docker run, they are appended to the ENTRYPOINT.

2. Comparison Example

Imagine you have a Dockerfile for a tool that says "Hello":

Using CMD:

FROM alpine
CMD ["echo", "Hello"]
  • docker run my-image → Output: Hello
  • docker run my-image hostname → Output: iZrj98s... (It ran hostname and completely ignored echo Hello)

Using ENTRYPOINT:

FROM alpine
ENTRYPOINT ["echo", "Hello"]
  • docker run my-image → Output: Hello
  • docker run my-image User → Output: Hello User (It kept the echo Hello and added User to the end)

3. Combining Them Together (The Pro Approach)

In many professional Dockerfiles, they are used together. ENTRYPOINT sets the constant command, and CMD provides a default parameter that the user can change.

ENTRYPOINT ["/start.sh"]
CMD ["9100"] 

In this case, /start.sh will always run, and it will receive 9100 as its first argument unless the user specifies a different value during docker run.

Summary Table

Feature CMD ENTRYPOINT
Primary Purpose Default command & arguments. The main executable of the container.
Overriding Completely replaced by arguments after the image name. Arguments are appended to the entrypoint.
Flexibility High (easy to run a shell instead). Strict (forces the container to run a specific script/app).

In your current lab step, we use ENTRYPOINT because we want to ensure that start.sh always runs to configure Nginx correctly before the server starts!

0 Comments

no data
Be the first to share your comment!