ENTRYPOINT and CMD in Dockerfile
In dockerfile
, both ENTRYPOINT
and CMD
directives often confuse users since both ENTRYPOINT
and CMD
directives define commands to be executed when the container starts. ENTRYPOINT sets the process to run, while CMD supplies default arguments to that process.
By the end of this story, You will have a good understanding of the following
CMD Directive
The CMD directive provides default command and arguments for the container that users can override when they run the container. It allows to specify multiple CMD instructions in the dockerfile, only the last one gets executed. CMD specifies default arguments for the ENTRYPOINT command. If ENTRYPOINT is not set, CMD will provide the command to run.
There are two ways to define CMD in the dockerfile.
Exec form:
CMD ["commmand", "args1", "args2", "argsN"]
Shell form:
CMD command args1 args2 argsN
For Example
Let us create a Dockerfile with the following lines
FROM ubuntu
CMD ["echo", "Hello, World!"]
Run below command to create test_cmd:latest image
docker build -t test_cmd:latest .
// notice the "." at the end, it means to build the default Dockerfile in the current directory.
// use -f option to execute a custom file name or different path
Launch a container from a newly created image
docker run -it test_cmd:latest
// Output
// Hello, World!
The above command returns the Hello, World!
output, Now let us launch a new container by overriding the default command.
docker run -it test_cmd:latest echo Hello, Naren!
// Output
// Hello, Naren!
ENTRYPOINT Directive
ENTRYPOINT defines the main command that will always be executed when the container starts. This command cannot be overridden by arguments provided to docker run
, but additional arguments can be appended.
There are two ways to define ENTRYPOINT in the dockerfile..
Exec form:
ENTRYPOINT ["commmand", "args1", "args2", "argsN"]
Shell form:
ENTRYPOINT command args1 args2 argsN
For Example
Lets create a Dockerfile with the following lines
FROM ubuntu
ENTRYPOINT ["echo", "Hello, World!"]
Run below command to create test_entrypoint:latest image
docker build -t test_entrypoint:latest .
// notice the "." at the end, it means to build the default Dockerfile in the current directory.
// use -f option to execute a custom file name or different path
Launch a container from a newly created image
docker run -it test_entrypoint:latest
// Output
// Hello, World!
The above command returns the Hello, World!
output, Now let us launch a new container with a command.
docker run -it test_entrypoint:latest echo Hello, Naren!
// Output
// Hello, World! echo Hello, Naren!
As we can see in the above output the runtime command gets appended to ENTRYPOINT command.
ENTRYPOINT V/S CMD
ENTRYPOINT and CMD instruction look the same but they are different from each other in respect of execution of the command. Let understand the difference in both the instructions.
CMD
- Provide default commands to the container when starts.
- Default command can be overridden by
docker run
arguments
ENTRYPOINT
- Sets the process to run when container starts
- Cannot overridden, additional arguments from
docker run
can be appended.
When to use CMD or ENTRYPOINT
CMD
Use CMD when you want to provide a default command or default arguments that can be overridden by the user.
Use CMD when the container runs a basic script or command that might need to change.
ENTRYPOINT
Use ENTRYPOINT when you want to enforce a specific command that should always be executed. This ensures that the main command is not accidentally overridden.
Use ENTRYPOINT for containers that act as wrappers around a primary executable, often combined with CMD to provide default arguments.
Best Practices
Use ENTRYPOINT to define the main command for the container.
Use CMD to provide default arguments for ENTRYPOINT.
Prefer the exec form (["executable", "param1", "param2"]) for both ENTRYPOINT and CMD to avoid running in a shell, which is more predictable and prevents issues with signal handling.