Understanding the Differences between Docker CMD and Docker Entrypoint: When to Use Each in Your Containers
Docker is a popular containerization platform that allows developers to create and deploy applications in a consistent and reproducible way. When creating Docker images, it’s important to specify the commands that should be executed when the container is started, and there are two instructions in a Dockerfile that are used for this purpose: CMD and ENTRYPOINT.
While both CMD and ENTRYPOINT are used to specify the commands that should be executed when the container is started, there are important differences between these instructions that can affect how the container behaves. Understanding these differences and when to use each instruction is crucial for creating effective Docker images and containers.
In this article, we’ll dive into the differences between CMD and ENTRYPOINT in Docker, and when to use each of them. We’ll also provide examples and best practices to help you create well-designed and easy-to-use Docker images and containers.
The CMD instruction
in a Dockerfile specifies the default command to be executed when running a container from the image. It should be noted that CMD is not an executable command itself, but rather it specifies the command that should be executed when the container is started.
There are two ways to specify the CMD instruction in a Dockerfile:
- Exec form: This specifies the command to be executed as an array of arguments, with the executable name as the first argument.
CMD ["executable", "arg1", "arg2"]
Example:
CMD ["node", "app.js"]
2. Shell form: This specifies the command to be executed as a single string, with the command and arguments separated by spaces.
CMD command arg1 arg2
Example:
CMD node app.js
Only the last one will be used if a Dockerfile has multiple CMD instructions.
ENTRYPOINT in Docker
The ENTRYPOINT instruction in a Dockerfile specifies the command that should be executed when the container is started, just like CMD. However, unlike CMD, ENTRYPOINT is an executable command itself and it cannot be overridden or ignored even if user try to pass command line arguments.
There are two ways to specify the ENTRYPOINT instruction in a Dockerfile:
- Exec form: This specifies the command to be executed as an array of arguments, with the executable name as the first argument.
ENTRYPOINT ["executable", "arg1", "arg2"]
Example:
ENTRYPOINT ["node", "app.js"]
2. Shell form specifies the command to be executed as a single string, with the command and arguments separated by spaces.
ENTRYPOINT command arg1 arg2
Example:
ENTRYPOINT node app.js
Only the last one will be used if a Dockerfile has multiple ENTRYPOINT instructions.
Difference between CMD and ENTRYPOINT
The main difference between CMD and ENTRYPOINT is that CMD specifies the default command to be executed when running a container from the image. In contrast, ENTRYPOINT specifies the command that should be executed when the container is started.
When a Docker container runs, any arguments passed to the docker run command will be appended to the command specified by ENTRYPOINT, and any arguments specified by CMD will be ignored.
For example, consider the following Dockerfile:
FROM node:alpine
WORKDIR /app
COPY app.js .
CMD ["arg1", "arg2"]
ENTRYPOINT ["node", "app.js"]
If you run the container with the following command:
docker run my-node-app arg3 arg4
The command that will be executed inside the container will be:
node app.js arg3 arg4
Pay attention to arg1 and arg2 arguments specified by CMD are ignored, and arg3 and arg4 are passed as arguments to the command specified by ENTRYPOINT.
When to use CMD vs ENTRYPOINT
The choice between CMD and ENTRYPOINT depends on the requirements of your Docker image and how you want your container to behave when it is started.
Use CMD to specify the default command to be executed when running a container from the image.
This is useful when the user does not provide any command line arguments to the container, and you want to provide a default behavior for the container. For example, if you have a Docker image for a web server, you could use CMD to specify the command to start the web server.
Use ENTRYPOINT to specify the main command to execute when the container starts. This is useful when you want the user to be able to specify additional arguments to the main command when starting the container. For example, suppose you have a Docker image for a database server.
In that case, you could use ENTRYPOINT to specify the command to start the database server, and allow the user to specify the database configuration as additional arguments.
Usually it’s a good practice to use ENTRYPOINT for the main command and CMD for optional arguments. This allows users to customize the container’s behavior by passing additional arguments, while ensuring that the main command is always executed.
In conclusion, CMD and ENTRYPOINT are both important instructions in a Dockerfile, and understanding their differences and use cases is crucial for creating effective Docker images and containers. Choosing the right instruction for your container’s requirements ensures that your containers are well-designed and easy to use.