What is the difference between CMD and ENTRYPOINT in a Dockerfile?
Jun 25, 2025 am 12:05 AMCMD and ENTRYPOINT are used in Docker to set the default command and fixed entry point when container starts. CMD provides default commands that can be overwritten, suitable for scenarios where flexible running mode is required; ENTRYPOINT executes specific commands regularly, and runtime parameters will be appended thereafter, suitable for ensuring that certain initialization steps or main process are always executed. The two can also be used in combination to take into account flexibility and control. The choice is based on the degree of flexibility or consistency of the desired container behavior.
When you're building Docker images, two instructions that often confuse newcomers are CMD
and ENTRYPOINT
. They both define what command gets run when a container starts, but they behave differently—especially when users pass arguments at runtime.
Understanding CMD: Default Command with Flexibility
The main purpose of CMD
is to provide defaults for a running container. These defaults can include an executable and its arguments, but they're meant to be easily overridden by the user.
For example:
CMD ["nginx", "-g", "daemon off;"]
This tells Docker to start nginx in the foreground if no other command is provided when the container runs. But if someone runs your image like this:
docker run my-image echo "Hello"
Then "nginx", "-g", "daemon off;"
will be replaced entirely with echo "Hello"
.
So think of CMD
as a suggestion —it's good for setting up default behavior, but it's not set in stone.
Key points:
- It defines the default command and/or arguments.
- Easily overridden by passing a new command at runtime.
- If you have multiple
CMD
lines in a Dockerfile, only the last one takes effect.
ENTRYPOINT: Setting a Fixed Starting Point
On the other hand, ENTRYPOINT
sets the actual entry point of the container. It's more about fixing the command that runs, making sure some part of it doesn't change unless explicitly configured to do so.
Here's a typical use case:
ENTRYPOINT ["docker-entrypoint.sh"]
In this case, even if someone adds a command at runtime, it will be passed as an argument to the entrypoint script. For example:
docker run my-image redis-server --port 8888
That means the container actually runs:
docker-entrypoint.sh redis-server --port 8888
This setup is useful when you want to make sure certain initialization steps always happen before launching the main process.
Key points:
- Defines the primary command that runs when the container starts.
- Arguments from the command line get appended to it (unless you override it with
--entrypoint
). -
CMD
values ??can act as default arguments toENTRYPOINT
.
When to Use CMD vs ENTRYPOINT
Choosing between them depends on how flexible or strict you want your container behavior to be.
Use CMD
when:
- You want to allow easy customization of what runs.
- The image might be used in different ways depending on context.
- You're building something general-purpose, like a base image.
Use ENTRYPOINT
when:
- You want to enforce a specific executable always runs.
- Your container behaves like a wrapper around a specific command or service.
- You need to do pre-processing (like configuration setup) before starting the main app.
Also, combining them is common and powerful:
ENTRYPOINT ["nginx", "-g", "daemon off;"] CMD ["-p", "8080:80"]
In this case, any extra flags you pass at runtime will be added after the ENTRYPOINT
, letting you customize behavior without breaking the core command.
So while CMD
gives flexibility and defaults, ENTRYPOINT
ensures consistency and control over execution. Knowing how to use each—or combine them—can help you build better Docker images that work the way you expect.
Basically that's it.
The above is the detailed content of What is the difference between CMD and ENTRYPOINT in a Dockerfile?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop <container_id>); start the container (docker start <container_id>); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

The methods to view Docker logs include: using the docker logs command, for example: docker logs CONTAINER_NAME Use the docker exec command to run /bin/sh and view the log file, for example: docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log Use the docker-compose logs command of Docker Compose, for example: docker-compose -f docker-com
