The difference between CMD and ENTRYPOINT in Docker
Docker containers have become the standard when it comes to managing software and dependencies in different environments. When working with real-world applications, you need to create a docker file before building your application container image.
A docker file is simply a read-only text document that contains a set of instructions that will be called when assembling an image. These commands include RUN
, , CMD
and ENTRYPOINT
.
In this article, we will discuss the usage of these commands. Most of the developers who might be in the initial stage of learning docker tend to use these commands interchangeably, which might cause some problems for you.
CMD
Commands in Docker
This command specifies RUN
the instructions to be executed when the docker command is executed. However, this requires executing the docker command without specifying any parameters RUN
.
When an argument is specified, this command is overwritten. On the other hand, if no command line arguments are specified, CMD
the command is executed.
CMD
The command is not necessary for your docker container to function properly, as the command can be used for the same purpose at runtime. However, the command can be handy ECHO
when running an executable file every time the container starts .CMD
To demonstrate how to use CMD
the command to run an executable at runtime, we will create a simple docker container containing a simple Flask program that prints out a message. Note that this can be replicated in any language, not necessarily Python.
We'll start by creating our main application, which should be as simple as shown below.
from flask import Flask
app = Flask(__name__)
def hello():
print("Hello, this is a simple Flask application")
hello()
In the same folder, we will touch Dockerfile
create our Dockerfile using the command .
The Dockerfile only specifies the base image, the working directory, and the packages that should be installed.
On the last line, you should notice CMD
the command. In this case, we use CMD
the command to execute the file when the container starts app.py
.
# base image
FROM python
# Set your working directory
WORKDIR /var/www/
# Copy the necessary files
COPY ./app.py /var/www/app.py
COPY ./requirements.txt /var/www/requirements.txt
# Install the necessary packages
RUN pip install -r /var/www/requirements.txt
# Run the app
CMD python3 app.py
requirements.txt
It should look like this.
click==8.0.4
Flask==2.0.3
gunicorn==20.1.0
itsdangerous==2.1.0
Jinja2==3.0.3
MarkupSafe==2.1.0
Werkzeug==2.0.3
Now that we have everything ready, we can now proceed with building the docker image. Before doing that, we need to make sure that we are in the same folder where the program is stored.
In our case, we will cd
put the into my-app
the folder before building the image as shown below.
~/my-app$ docker build -t isaactonyloi_image .
Output:
=> [internal] load build context 0.9s
=> => transferring context: 320B 0.1s
=> [2/5] WORKDIR /var/www/ 5.1s
=> [3/5] COPY ./app.py /var/www/app.py 3.2s
=> [4/5] COPY ./requirements.txt /var/www/requirements.txt 3.2s
=> [5/5] RUN pip install -r /var/www/requirements.txt 53.9s
=> exporting to image 6.9s
=> => exporting layers 5.8s
=> => writing image sha256:5847e4777754d9d576accd929076bfbee633ca71f049ebe1af6e9bae161f3e96 0.1s
=> => naming to docker.io/library/isaactonyloi_image 0.2s
isaac@DESKTOP-HV44HT6:~/my-app$
We have successfully built our image based on the earlier docker file. We can verify this below.
~/my-app$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
isaactonyloi_image latest 5847e4777754 7 minutes ago 929MB
run
We can finally create our docker container using the docker command based on this image . Also, note that we will CMD
do this without passing any arguments to the command.
~/my-app$ docker run isaactonyloi_image
Hello, this is a simple Flask application
Besides this, CMD
commands allow us to create parameters that can be easily overridden at runtime.
We have made changes to the command in the example below CMD
. The other files remain the same and we have rebuilt a new image.
# base image
FROM python
# Set your working directory
WORKDIR /var/www/
# Copy the necessary filesls
COPY ./app.py /var/www/app.py
COPY ./requirements.txt /var/www/requirements.txt
# Install the necessary packages
RUN pip install -r /var/www/requirements.txt
# Run the app
CMD ["echo", "Hello, Developer"]
This is the new image we rebuilt based on the changes we made to the Dockerfile.
~/my-app$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
new_image latest 73f323be0d2f 25 minutes ago 929MB
When we create a new docker container again without passing any arguments, we should CMD
get the message below command.
isaac@DESKTOP-HV44HT6:~/my-app$ docker run new_image
Hello, Developer
However, when we pass arguments at runtime, CMD
the command will automatically be overwritten and the new arguments should take precedence. So, it provides us the flexibility to add new arguments at runtime as shown below.
~/my-app$ docker run new_image hostname
da0a832888cb
The above output shows that CMD
the command was not executed and overwritten with the new hostname parameter.
ENTRYPOINT
Commands in Docker
The docker command is similar ENTRYPOINT
to the command, but not identical.CMD
When using CMD
the command we can easily override it by passing an argument at runtime, but ENTRYPOINT
this is not the case with the command.
Therefore, ENTRYPOINT
it can be used to not overwrite the entry point instruction at runtime.
As shown below, we can explore how this command works by simply CMD
replacing the command in the Dockerfile with the command. We will build a new image based on the changes to the docker file.ENTRYPOINT
isaac@DESKTOP-HV44HT6:~/my-app$ docker build -t tonyloi_newimage .
[+] Building 42.7s (10/10) FINISHED
Output:
=> [internal] load build definition from Dockerfile 2.0s
=> => transferring dockerfile: 365B 0.7s
=> [internal] load .dockerignore 1.6s
=> => transferring context: 2B 0.4s
=> [internal] load metadata for docker.io/library/python:latest 35.4s
=> [1/5] FROM docker.io/library/python@sha256:c90e15c86e2ebe71244a2a51bc7f094554422c159ce309a6faadb6debd5a6df0 0.3s
=> [internal] load build context 1.2s
=> => transferring context: 63B 0.1s
=> CACHED [2/5] WORKDIR /var/www/ 0.0s
=> CACHED [3/5] COPY ./app.py /var/www/app.py 0.0s
=> CACHED [4/5] COPY ./requirements.txt /var/www/requirements.txt 0.0s
=> CACHED [5/5] RUN pip install -r /var/www/requirements.txt 0.0s
=> exporting to image 2.1s
=> => exporting layers 0.0s
=> => writing image sha256:15fb8e4e3ff58ed529b11342bba75b029fd4323beb24aac70ca36b178d04cb34 0.2s
=> => naming to docker.io/library/tonyloi_newimage 0.1s
isaac@DESKTOP-HV44HT6:~/my-app$
This output proves that we have successfully built the new image. Now we can create a new docker container based on this image.
You can choose to create a container using either the image name or the image ID, both of which can be docker images
accessed using the command. This will also show the image we created earlier.
~/my-app$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tonyloi_newimage latest 15fb8e4e3ff5 48 minutes ago 929MB
new_image latest 73f323be0d2f 48 minutes ago 929MB
isaactonyloi_image latest 5847e4777754 48 minutes ago 929MB
If we build the docker container without adding any parameters, we should get the following output.
isaac@DESKTOP-HV44HT6:~/my-app$ docker run tonyloi_newimage
Hello, Developer
If we try to pass arguments when creating another container based on this image, you will notice that, CMD
unlike the case with the command, these new arguments do not overwrite ENTRYPOINT
the command.
We can verify this below.
isaac@DESKTOP-HV44HT6:~/my-app$ docker run tonyloi_newimage Welcome to ADC
Hello, Developer Welcome to ADC
in conclusion
We can say that these two commands are very similar; however, they differ in that CMD
the command can be overridden at runtime, whereas ENTRYPOINT
the command cannot.
Furthermore, some situations may require the use of both commands simultaneously.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Copy files from host to Docker container
Publish Date:2025/03/25 Views:126 Category:Docker
-
This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest
Get the IP address of the Docker container
Publish Date:2025/03/25 Views:102 Category:Docker
-
This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con
Uninstalling Docker on macOS
Publish Date:2025/03/25 Views:95 Category:Docker
-
Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p
Enter the Docker container's shell
Publish Date:2025/03/25 Views:98 Category:Docker
-
This article will demonstrate how to enter the Docker container shell using multiple methods. Use docker exec to enter the Docker container's shell We need to have a container up and running to use this command. We can check the status of t
Listing containers in Docker
Publish Date:2025/03/25 Views:122 Category:Docker
-
This article will discuss various commands for listing containers created in our system. This means we should create some containers beforehand for these commands to return values. List all running containers in Docker We will start by list
Mount the host directory into the Docker container
Publish Date:2025/03/25 Views:189 Category:Docker
-
Docker provides, among other features, tools to work with the host operating system and the container file system. One of these features is the ability to persist data in containers and share data between containers by mounting directories
Docker build command with multiple parameters
Publish Date:2025/03/25 Views:138 Category:Docker
-
docker build The command allows us to create a Docker image from a Dockerfile. It also allows us to build an image from a context that references a set of files located in a location specified by a PATH or URL. A Dockerfile, on the other ha
Tagging images with Docker and Docker Compose
Publish Date:2025/03/25 Views:70 Category:Docker
-
When creating a Docker image, it is best practice to give it a descriptive and meaningful name. This process makes identifying and managing images more manageable, especially when dealing with many images. This article discusses tagging ima
Creating a database user with Docker Postgres
Publish Date:2025/03/24 Views:166 Category:Docker
-
When developing applications, we usually use database management systems such as PostgreSQL, MySQL, MongoDB, etc. to record application data. Docker helps us run an instance of these application database management systems. This helps save