JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Docker >

The difference between CMD and ENTRYPOINT in Docker

Author:JIYIK Last Updated:2025/03/25 Views:

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, , CMDand 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.

CMDCommands in Docker

This command specifies RUNthe 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, CMDthe command is executed.

CMDThe 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 ECHOwhen running an executable file every time the container starts .CMD

To demonstrate how to use CMDthe 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 Dockerfilecreate 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 CMDthe command. In this case, we use CMDthe 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.txtIt 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 cdput the into my-appthe 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

runWe can finally create our docker container using the docker command based on this image . Also, note that we will CMDdo this without passing any arguments to the command.

~/my-app$ docker run isaactonyloi_image
Hello, this is a simple Flask application

Besides this, CMDcommands 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 CMDget the message below command.

isaac@DESKTOP-HV44HT6:~/my-app$ docker run new_image
Hello, Developer

However, when we pass arguments at runtime, CMDthe 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 CMDthe command was not executed and overwritten with the new hostname parameter.

ENTRYPOINTCommands in Docker

The docker command is similar ENTRYPOINTto the command, but not identical.CMD

When using CMDthe command we can easily override it by passing an argument at runtime, but ENTRYPOINTthis is not the case with the command.

Therefore, ENTRYPOINTit can be used to not overwrite the entry point instruction at runtime.

As shown below, we can explore how this command works by simply CMDreplacing 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 imagesaccessed 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, CMDunlike the case with the command, these new arguments do not overwrite ENTRYPOINTthe 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 CMDthe command can be overridden at runtime, whereas ENTRYPOINTthe 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.

Article URL:

Related Articles

Docker deploys nginx php application

Publish Date:2025/03/26 Views:131 Category:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

Publish Date:2025/03/26 Views:97 Category:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

Publish Date:2025/03/26 Views:125 Category:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

Publish Date:2025/03/26 Views:202 Category:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Enforce clean build of images in Docker

Publish Date:2025/03/26 Views:88 Category:Docker

This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho

Running a Docker instance from a Dockerfile

Publish Date:2025/03/26 Views:140 Category:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial