DEV Community

Jayesh Nalawade
Jayesh Nalawade

Posted on • Originally published at jayeshdevops.hashnode.dev

1 1 1 1 1

ENTRYPOINT vs CMD in Dockerfile

CMD (Command)

  • Purpose: Provides default arguments for the container.
  • Usage: Use CMD when you want to provide default behavior but allow it to be overridden by arguments supplied when running the container (docker run).

Example 1: Default command with override

FROM ubuntu:latest
CMD ["echo", "Hello, World!"]

  • If you run the container as docker run <image>, it will print Hello, World!.
  • If you provide a different command, like docker run <image> echo "Goodbye", it will override the CMD.

ENTRYPOINT

  • Purpose: Sets the main executable for the container that is not easily overridden.
  • Usage: Use ENTRYPOINT when you want to enforce a specific command or script as the container's entry point. Additional arguments provided with docker run are passed as arguments to the ENTRYPOINT.

Example 2: Enforce entry point

FROM ubuntu:latest
ENTRYPOINT ["echo"]

  • Running docker run <image> Hello, World! will always use echo, so the result will be Hello, World!.
  • You cannot replace echo unless you use the --entrypoint flag.

Combining ENTRYPOINT and CMD

  • Combine ENTRYPOINT and CMD for flexible and reusable images.
  • ENTRYPOINT specifies the main command, and CMD provides default arguments that can be overridden.

Example 3: Flexible combination

FROM ubuntu:latest
ENTRYPOINT ["echo"]
CMD ["Hello, World!"]

  • Running docker run <image> will print Hello, World!.
  • Running docker run <image> Goodbye will print Goodbye, overriding CMD but not ENTRYPOINT.

Key Differences

Feature CMD ENTRYPOINT
Overridable Yes (via docker run arguments). No, unless --entrypoint is used.
Intended Purpose Default behavior or arguments. Main executable/script.
Flexibility More flexible (easier to override). More rigid (forces specific usage).

When to Use Which?

  • Use CMD when you want to provide a default, flexible behavior that users can easily override.
  • Use ENTRYPOINT when you want to enforce a specific script or command while still allowing extra arguments.

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay