Docker is a platform that enables us to run and manage applications inside containers. Before running an application in a container, we must create a Docker image using a Dockerfile. A Dockerfile is a text file containing instructions that Docker executes to create the image. Among these instructions, CMD and ENTRYPOINT play a crucial role in defining how a container runs.
In this blog post, we will explore CMD and ENTRYPOINT, their differences, and when to use each.
Prerequisites
To follow along with the examples in this post, ensure you have a code editor and Docker Desktop installed on your system.
What Is CMD in Dockerfile?
CMD provides default instructions for running a container. If no command is supplied during container execution, Docker uses the CMD directive from the Dockerfile.
Example of CMD in a Dockerfile
Create a directory named docker-demo
, and inside it, create a Dockerfile
with the following content:
FROM alpine
CMD ["echo", "Welcome to AItechNav!"]
In this Dockerfile:
-
FROM alpine
sets the base image. -
CMD ["echo", "Welcome to AItechNav!"]
defines the default command to run inside the container.
Building and Running the Docker Image
To create a Docker image, execute the following command inside the docker-demo
directory:
docker build . -t custom-image:v1
Once the image is built, verify it using:
docker image list
Now, run a container using the image:
docker container run custom-image:v1
Output:
Welcome to AItechNav!
Overriding CMD at Runtime
CMD allows users to override the default command:
docker container run custom-image:v1 echo "Empowering AI Enthusiasts!"
Output:
Empowering AI Enthusiasts!
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!"]
Building and Running the Docker Image
Build the new image:
docker build . -t custom-image:v2
Verify the image and run a container:
docker container run custom-image:v2
Output:
Welcome to AItechNav!
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!"
Output:
Welcome to AItechNav! AI and Cloud Learning Hub!
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!"]
Building and Running the Image
Build the image:
docker build . -t custom-image:v3
Run the container:
docker container run custom-image:v3
Output:
Welcome to AItechNav!
Overriding CMD but Not ENTRYPOINT
Run the container with a custom argument:
docker container run custom-image:v3 "the future of AI!"
Output:
Welcome to the future of AI!
In this setup:
- ENTRYPOINT (
echo Welcome to
) remains unchanged. - CMD (
AItechNav!
) is overridden bythe 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!
Top comments (0)