In this blog we will look at the key differences between Docker ENTRYPOINT vs CMD instruction using a practical example.
What is ENTRYPOINT and CMD
In a Dockerfile, ENTRYPOINT
and CMD
are two different instructions that are used to define how a container should run.
ENTRYPOINT
is used to specify the main command that should be executed when a container is started using the image. The default ENTRYPOINT command is /bin/sh -c
CMD
, on the other hand, is used to specify the default command and arguments that should be executed when a container is started.
If both ENTRYPOINT
and CMD
are specified in a Dockerfile, the command specified in CMD
will be appended to the ENTRYPOINT
command. It acts as an argument for ENTRYPOINT. The resulting command will be executed when the container is started.
Lets understand these concepts practically.
Executing Commands Using ENTRYPOINT and CMD
Let’s take an example of the following Dockerfile. It installs http-tools and starts the ab (apache benchmark) utility using CMD and Entrypoint. Both of them perform the same job
Using CMD
FROM centos:7
MAINTAINER Devopscube
RUN yum -y update && \
yum -y install httpd-tools && \
yum clean all
CMD ["ab"]
Using ENTRYPOINT:
FROM centos:7
MAINTAINER Devopscube
RUN yum -y update && \
yum -y install httpd-tools && \
yum clean all
ENTRYPOINT ["ab"]
Now if you run the container from the above Dockerfile images, it will throw the following error.
➜ docker run demo
ab: wrong number of arguments
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
-n requests Number of requests to perform
The reason is, ab command requires an http endpoint as an argument to start the service.
We have two ways to get around this problem. Hardcode the HTTP endpoint argument as shown in the below examples.
Using CMD: The ab
executable and HTTP URL arguments are added in separate square brackets.
FROM centos:7
MAINTAINER Devopscube
RUN yum -y update && \
yum -y install httpd-tools && \
yum clean all
CMD ["ab"] ["http://google.com/"]
Using ENTRYPOINT: The executable and argument are separated by commas in the same square bracket.
FROM centos:7 MAINTAINER Devopscube RUN yum -y update && \ yum -y install httpd-tools && \ yum clean all ENTRYPOINT ["ab" , "http://google.com/" ]
Difference Between ENTRYPOINT and CMD
Lets look at the difference between CMD
and ENTRYPOINT
by passing ab commands during docker run.
Using CMD:
FROM centos:7
MAINTAINER Devopscube
RUN yum -y update && \
yum -y install httpd-tools && \
yum clean all
CMD ["ab"]
While running the container, just add the full ab command at the end of the docker run command. It will override the whole CMD specified in the Dockerfile.
docker run ab-demo ab http://google.com/
Using ENTRYPOINT:
If you want to pass the URL argument to ENTRYPOINT, you need to pass the URL alone. The reason is we have the ab
command as part of the ENTRYPOINT definition.
And the URL you pass in the run command will be appended to the ENTRYPOINT script. In this case, CMD instruction is not required in the Dockerfile.
FROM centos:7
MAINTAINER Devopscube
RUN yum -y update && \
yum -y install httpd-tools && \
yum clean all
ENTRYPOINT ["ab"]
Docker Command:
docker run ab-demo http://google.com/
You can override the whole ENTRYPOINT like you do with CMD using the --entrypoint
flag as shown below
docker run --entrypoint "ab" ab-demo http://google.com/
Using ENTRYPOINT and CMD in Same Dockerfile
You can also use both CMD
and ENTRYPOINT
instructions to achieve this. Here is how the Dockerfile looks.
FROM centos:7
MAINTAINER Devopscube
RUN yum -y update && \
yum -y install httpd-tools && \
yum clean all
ENTRYPOINT ["ab"]
CMD ["http://dummy-url.com/"]
When ENTRYPOINT
and CMD
used in the same Dockerfile, everything in the CMD instruction will be appended to the ENTRYPOINT as an argument.
If you run a container using the above Dockerfile, at container start, ab script will get executed with the dummy-url.com
as an argument.
ENTRYPOINT and CMD in Kubernetes Pod
Lets understand how Docker Entrypoint and CMD translates to Kubernetes Pod.
When it comes to Kubernetes Pod, we have the option named “command
” and “args
” in the Pod Specification to override Entrypoint and CMD set in the Docker image.
In Kubernetes Pod if you want to override Docker CMD or Entrypoint, you need to specify the command and args show below.
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: my-image
command: ["my-script.sh"]
args: ["arg1", "arg2"]
In this example, the entrypoint
is set to ["/bin/bash", "-c"]
, which means that when the container starts, it will run the bash
shell with the -c
flag to execute the command specified in the command
field (my-script.sh
). The args
field specifies any additional arguments to be passed to my-script.sh
.
Here is what you should know.
- In case a Command or Args is not provided for a container, the defaults specified by the image will be utilized.
- However, if you provide a Command without any Args for the container, only the specified Command will be executed while disregarding the default arguments of the image.
- On the other hand, if only Args are supplied, the image’s default command will be used in conjunction with the provided arguments.
- Lastly, if both Command and Args are supplied, the defaults of the image will be overridden and the supplied values will be utilized.