Skip to main content

Command Palette

Search for a command to run...

ENTRYPOINT vs CMD

Published
2 min readView as Markdown

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

FeatureCMDENTRYPOINT
OverridableYes (via docker run arguments).No, unless --entrypoint is used.
Intended PurposeDefault behavior or arguments.Main executable/script.
FlexibilityMore 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.

More from this blog

The Start(Devops)

21 posts