Docker
Dockerfile
CMD vs ENTRYPOINT
containerization
DevOps

What is the difference between CMD and ENTRYPOINT in a Dockerfile?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

In the realm of Docker containers, understanding how to define what happens when a container starts is crucial. Two primary instructions found in a Dockerfile for this purpose are CMD and ENTRYPOINT. Although these directives are often misunderstood or used interchangeably, they serve different purposes. This article delineates the differences between CMD and ENTRYPOINT, covers their technical aspects, and explores their use through examples.

The CMD Instruction

The CMD instruction in a Dockerfile is utilized to specify the default command that should run when a container is started. It acts as a fallback command if no command is provided during the docker run execution. The CMD directive provides a way to include default parameters when starting a container but can be overridden by providing alternative arguments at runtime.

Examples of CMD Usage

In its basic form, the CMD instruction can be represented in three ways:

  1. Exec Form: Most recommended, as it prevents the underlying shell process and related issues.
dockerfile
   CMD ["executable", "param1", "param2"]
  1. Shell Form: Executes the command using /bin/sh -c.
dockerfile
   CMD command param1 param2
  1. Inherited Form: If CMD is defined as a parameter.
dockerfile
   FROM ubuntu
   CMD ["ls"]
   # It can be overridden by providing different arguments

In a scenario where you use CMD like so:

dockerfile
CMD ["nginx", "-g", "daemon off;"]

This will instruct the container to run nginx with -g daemon off; as arguments unless specified otherwise in docker run.

The ENTRYPOINT Instruction

Contrariwise, ENTRYPOINT is used to define a command that will always be executed, opt-in to act as the immutable part of the command. The benefit of using ENTRYPOINT is ensuring that required binaries are executed when a container starts and maintaining more control over command execution when shipping containerized applications.

Examples of ENTRYPOINT Usage

Similar to CMD, ENTRYPOINT can also be used in:

  1. Exec Form: Preferred due to its robust nature, preventing shell interpretation.
dockerfile
   ENTRYPOINT ["executable", "param1", "param2"]
  1. Shell Form: Converts the command to a shell command with /bin/sh -c.
dockerfile
   ENTRYPOINT command param1 param2

A Dockerfile example using ENTRYPOINT might look like:

dockerfile
ENTRYPOINT ["nginx"]
CMD ["-g", "daemon off;"]

Here, nginx will always run upon starting a container, with default parameters provided by CMD.

Combining ENTRYPOINT and CMD

You can combine ENTRYPOINT and CMD to create flexible entry points. ENTRYPOINT prepares the stage for command execution, while CMD acts as the default parameter provider. Consider this example:

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

When running docker run <image>, it echoes Hello, World!, but upon specifying arguments during runtime, like docker run <image> Hey Docker!, Hey Docker! overrides the CMD part, resulting in echo Hey Docker!.

Key Differences

AspectCMDENTRYPOINT
PurposeDefault command/arguments for a container Can be overriddenForces a command to run, controlling responsibility less flexible
FlexibilityEasily overridden by runtime command argumentsLess flexible, always executes even with runtime arguments
Use CaseDefault behavior alterations e.g., script argumentsFixed setup scripts, crucial being executed every time
PrecautionIf used together, can lead to command execution failureMust be paired with CMD or explicit arguments in docker run

When to Use Each

  • Use CMD when you need default behavior that can be changed at runtime. Ideal for default arguments, scripts, or application commands that may vary.
  • Use ENTRYPOINT when you want a mandatory command always to run, regardless of user input. Vital for fixed setups or applications that must always start in a specific way.

Understanding the roles of CMD and ENTRYPOINT helps build more predictable and structured Docker containers. By combining these directives properly, you can leverage Docker's dynamic and versatile nature for effective containerization.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.