"Understanding ENTRYPOINT vs CMD in Docker: Differences and Use Cases"

In Docker, both ENTRYPOINT and CMD are instructions used in the Dockerfile to define the default behavior of a container.
Written by teamember02
Updated 7 months ago

However, they serve different purposes and can be used together in complementary ways. Here’s a detailed explanation of each and their differences:

ENTRYPOINT
The ENTRYPOINT instruction specifies a command that will always run when the container starts. It is used to set the main command for the container execution. This command will not be overridden by any arguments passed to docker run, except when explicitly overridden.

Syntax:

Dockerfile
ENTRYPOINT ["executable", "param1", "param2"]

Example:

Dockerfile
FROM ubuntu ENTRYPOINT ["top", "-b"]

In this example, the container will always run the top -b command when started.

Use Case:

  • Defining a fixed command: When you want to ensure a specific command is always executed, regardless of additional arguments.
  • Running a primary service: Often used to run the main application or service in
    the container. 

CMD

The CMD instruction provides default arguments for the ENTRYPOINT command. If an ENTRYPOINT is not specified, CMD defines the command to be executed when running the container. It can be overridden by arguments provided in docker run.

Syntax:

Dockerfile
CMD ["executable", "param1", "param2"]

or

Dockerfile
CMD ["param1", "param2"]

or

Dockerfile
CMD command param1 param2

Example:

Dockerfile
FROM ubuntu CMD ["echo", "Hello, World!"]

In this example, if no other command is specified, the container will run echo Hello, World!.

Use Case:

  • Providing default arguments: To set default behavior that can be overridden.
  • Fallback command: To specify a default command if no other command is given.

Combining ENTRYPOINT and CMD

When both ENTRYPOINT and CMD are used together, CMD provides default parameters to the ENTRYPOINT.

Example:

Dockerfile
FROM ubuntu ENTRYPOINT ["python3", "script.py"] CMD ["--help"]

In this example:

  • The container will run python3 script.py --help by default.
  • If you run the container with additional arguments, e.g., docker run my_image --version, it will run python3 script.py --version.

Overriding Behavior

  • Overriding CMD: You can override CMD by providing a new command at the end of docker run.
    docker run my_image new_command
    
  • Overriding ENTRYPOINT: Use the --entrypoint flag.
    docker run --entrypoint new_entrypoint my_image
    

Summary

  • ENTRYPOINT: Sets the main command to run when the container starts and cannot be easily overridden.
  • CMD: Sets default arguments for the ENTRYPOINT or acts as the command if no ENTRYPOINT is specified. It can be overridden by providing arguments at runtime.

By understanding and using ENTRYPOINT and CMD effectively, you can create flexible and reliable Docker containers that behave as expected in different scenarios.

Did this answer your question?