Docker CMD vs ENTRYPOINT: Understanding the Differences

Anuj Tyagi - Mar 3 - - Dev Community

Docker is a platform used to manage applications within containers. To run an application in a container, a Docker image needs to be created first which can be done using a Dockerfile. A Dockerfile is a text document that has one or more commands that are executed by Docker to build the image. Two important instructions among these are CMD and ENTRYPOINT for defining how a container operates.

In this blog post, we will explore CMD and ENTRYPOINT, their differences, and when to use each.

Prerequisites

You will need a code editor and Docker Desktop application on your device to follow through with the practical examples demonstrated in this guide.

What Is CMD in Dockerfile?

CMD any instructions within the Dockerfile that comes into effect by default. It is executed when no command is executed while using the container.

Example of CMD in a Dockerfile

Make a directory called docker-demo, and in there, a new file called Dockerfile with this content:

FROM alpine
CMD ["echo", "Welcome to AItechNav!"]
Enter fullscreen mode Exit fullscreen mode

In this Dockerfile:

  • FROM alpine sets the base image.
  • CMD ["echo", "Welcome to AItechNav!"] defines the default command to run inside the container.

Build Image and Run the container

To create a Docker image, execute the following command inside the docker-demo directory:

docker build . -t custom-image:v1
Enter fullscreen mode Exit fullscreen mode

Once the image is built, verify it using:

docker image list
Enter fullscreen mode Exit fullscreen mode

Now, run a container using the image:

docker container run custom-image:v1
Enter fullscreen mode Exit fullscreen mode

Output:

Welcome to AItechNav!
Enter fullscreen mode Exit fullscreen mode

Overriding CMD at Runtime

CMD allows users to override the default command:

docker container run custom-image:v1 echo "Empowering AI Enthusiasts!"
Enter fullscreen mode Exit fullscreen mode

Output:

Empowering AI Enthusiasts!
Enter fullscreen mode Exit fullscreen mode

This shows that CMD acts as a default but can be replaced when running a container.

What Is ENTRYPOINT in Dockerfile?

ENTRYPOINT, like CMD, specifies the command to execute when running a container. However, unlike CMD, the ENTRYPOINT directive cannot be overridden at runtime; instead, additional arguments are appended to it.

Example of ENTRYPOINT in a Dockerfile

Modify the Dockerfile to use ENTRYPOINT instead of CMD:

FROM alpine
ENTRYPOINT ["echo", "Welcome to AItechNav!"]
Enter fullscreen mode Exit fullscreen mode

Building and Running the Docker Image

Build the new image:

docker build . -t custom-image:v2
Enter fullscreen mode Exit fullscreen mode

Verify the image and run a container:

docker container run custom-image:v2
Enter fullscreen mode Exit fullscreen mode

Output:

Welcome to AItechNav!
Enter fullscreen mode Exit fullscreen mode

Attempting to Override ENTRYPOINT at Runtime

Unlike CMD, if we attempt to override ENTRYPOINT:

docker container run custom-image:v2 echo "AI and Cloud Learning Hub!"
Enter fullscreen mode Exit fullscreen mode

Output:

Welcome to AItechNav! AI and Cloud Learning Hub!
Enter fullscreen mode Exit fullscreen mode

The argument is appended instead of replacing the default command.

Using CMD & ENTRYPOINT Together

You can combine CMD and ENTRYPOINT to define a fixed command while allowing users to provide different arguments.

Modify the Dockerfile:

FROM alpine
ENTRYPOINT ["echo", "Welcome to"]
CMD ["AItechNav!"]
Enter fullscreen mode Exit fullscreen mode

Building and Running the Image

Build the image:

docker build . -t custom-image:v3
Enter fullscreen mode Exit fullscreen mode

Run the container:

docker container run custom-image:v3
Enter fullscreen mode Exit fullscreen mode

Output:

Welcome to AItechNav!
Enter fullscreen mode Exit fullscreen mode

Overriding CMD but Not ENTRYPOINT

Run the container with a custom argument:

docker container run custom-image:v3 "the future of AI!"
Enter fullscreen mode Exit fullscreen mode

Output:

Welcome to the future of AI!
Enter fullscreen mode Exit fullscreen mode

In this setup:

  • ENTRYPOINT (echo Welcome to) remains unchanged.
  • CMD (AItechNav!) is overridden by the future of AI! at runtime.

Difference Between CMD & ENTRYPOINT

The table below highlights their differences:

Feature CMD ENTRYPOINT
Provides a default command Yes Yes
Allows command override at runtime Yes No (arguments are appended)

When to Use CMD vs. ENTRYPOINT

  • Use CMD when you want to provide a default command but allow users to override it at runtime.
  • Use ENTRYPOINT when you want to enforce a specific command while allowing additional parameters.
  • Use both CMD and ENTRYPOINT together when you want a fixed command but allow users to modify arguments.

Conclusion

CMD and ENTRYPOINT are essential Dockerfile instructions that define how a container runs. CMD provides flexibility by allowing users to override the default command, whereas ENTRYPOINT enforces a fixed command and appends additional arguments. By understanding their differences and best use cases, you can structure your Dockerfiles efficiently for various application needs.

Happy containerizing!

. . . . . . . . . . . . .