Docker ENTRYPOINT Vs CMD Explained With Examples

ENTRYPOINT vs CMD Explained
This blog post provides a comprehensive explanation of the key differences between Docker’s ENTRYPOINT and CMD instructions, along with practical examples. Gain a clear understanding of how these instructions function and how they can be used to define container behavior. Whether you are new to Docker or seeking clarification on the nuances of ENTRYPOINT and CMD, this article offers valuable insights. Discover how to execute commands, override default behaviors, and utilize both instructions together in a Dockerfile. Additionally, learn about the translation of ENTRYPOINT and CMD into Kubernetes Pod specifications. Enhance your Docker knowledge and optimize your containerized applications with this in-depth exploration of “ENTRYPOINT vs CMD.”

Understanding ENTRYPOINT and CMD

In a Dockerfile, both ENTRYPOINT and CMD are instructions 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. By default, the ENTRYPOINT command is set to `/bin/sh -c`.

On the other hand, CMD 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.

Let’s explore these concepts practically.

Executing Commands Using ENTRYPOINT and CMD

Let’s consider the following Dockerfile example that installs httpd-tools and starts the ab (Apache Benchmark) utility using CMD and ENTRYPOINT. Both methods accomplish the same task.

Using CMD:

FROM centos:7
MAINTAINER Hassan
RUN yum -y update && \
    yum -y install httpd-tools && \
    yum clean all
CMD ["ab"]

Using ENTRYPOINT:

FROM centos:7
MAINTAINER Hassan
RUN yum -y update && \
    yum -y install httpd-tools && \
    yum clean all
ENTRYPOINT ["ab"]

If you run the container using the above Dockerfile images, it will throw the following error:

ab: wrong number of arguments
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
    -n requests     Number of requests to perform

The reason for this error is that the ab command requires an HTTP endpoint as an argument to start the service.

There are two ways to solve this problem: hardcoding the HTTP endpoint argument.

Using CMD:

FROM centos:7
MAINTAINER Hassan
RUN yum -y update && \
    yum -y install httpd-tools && \
    yum clean all
CMD ["ab", "http://google.com/"]

Using ENTRYPOINT:

FROM centos:7
MAINTAINER Hassan
RUN yum -y update && \
    yum -y install httpd-tools && \
    yum clean all
ENTRYPOINT ["ab", "http://google.com/"]

Difference Between ENTRYPOINT and CMD

Let’s examine the difference between CMD and ENTRYPOINT by passing ab commands during Docker run.

Using CMD:

FROM centos:7
MAINTAINER Hassan
RUN yum -y update && \
    yum -y install httpd-tools && \
    yum clean all
CMD ["ab"]

While running the container, you can add the full ab command at the end of the Docker run command. This will override the entire 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 only need to provide the URL alone. The reason is that we have the ab command as part of the ENTRYPOINT definition.

The URL you pass in the run command will be appended to the ENTRYPOINT script. In this case, the CMD instruction is not required in the Dockerfile.

FROM centos:7
MAINTAINER Hassan
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 entire ENTRYPOINT just 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 the Same Dockerfile

You can also use both CMD and ENTRYPOINT instructions in the same Dockerfile to achieve different combinations. Here is an example:

FROM centos:7
MAINTAINER Hassan
RUN yum -y update && \
    yum -y install httpd-tools && \
    yum clean all
ENTRYPOINT ["ab"]
CMD ["http://dummy-url.com/"]

When ENTRYPOINT and CMD are used together in the same Dockerfile, everything specified in the CMD instruction will be appended to the ENTRYPOINT as an argument.

If you run a container using the above Dockerfile, the ab script will be executed with http://dummy-url.com/ as an argument.

ENTRYPOINT and CMD in Kubernetes Pod

Now let’s understand how Docker’s ENTRYPOINT and CMD translate to Kubernetes Pod.

In Kubernetes Pod, we have the options named “command” and “args” in the Pod Specification to override ENTRYPOINT and CMD set in the Docker image.

If you want to override Docker CMD or ENTRYPOINT in a Kubernetes Pod, you need to specify the command and args as shown 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’s what you should know:

  1. If a Command or Args is not provided for a container in a Kubernetes Pod, the defaults specified by the image will be utilized.
  2. However, if you provide a Command without any Args for the container, only the specified Command will be executed, disregarding the default arguments of the image.
  3. On the other hand, if only Args are supplied, the image’s default command will be used in conjunction with the provided arguments.
  4. Lastly, if both Command and Args are supplied, the defaults of the image will be overridden, and the supplied values will be utilized.

ENTRYPOINT vs CMD Explained FAQs

Q1: What happens if both ENTRYPOINT and CMD are specified in a Dockerfile?
A: When both ENTRYPOINT and CMD instructions are present, 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.

Q2: How can I pass arguments to ENTRYPOINT and CMD?
A: Arguments can be passed by providing them as elements in the array format within the Dockerfile. For example:

ENTRYPOINT ["executable", "arg1", "arg2", "arg3"]
CMD ["arg1", "arg2", "arg3"]

Q3: Can I override the ENTRYPOINT or CMD values during runtime?
A: Yes, you can override the ENTRYPOINT or CMD values when running the container using the docker run command. Simply specify the desired command and arguments after the container image name.

Q4: What happens if I provide a command without any arguments in CMD?
A: If you provide a command without any arguments in CMD, only the specified command will be executed when running the container, disregarding the default arguments defined in the image.

Q5: How can I specify both ENTRYPOINT and CMD in a Dockerfile?
A: You can use both ENTRYPOINT and CMD instructions in the same Dockerfile. When used together, the CMD instruction will be appended to the ENTRYPOINT as an argument.

Q6: How do ENTRYPOINT and CMD translate to Kubernetes Pod?
A: In a Kubernetes Pod, the equivalent options to override ENTRYPOINT and CMD are “command” and “args” in the Pod Specification. You can specify the desired command and arguments using these options.

Q7: What happens if Command or Args are not provided for a container in a Kubernetes Pod?
A: If Command or Args are not provided for a container in a Kubernetes Pod, the defaults specified by the image will be utilized for running the container.

Q8: Can I override Docker CMD or ENTRYPOINT in a Kubernetes Pod?
A: Yes, you can override Docker CMD or ENTRYPOINT in a Kubernetes Pod by specifying the desired command and arguments using the “command” and “args” options in the Pod Specification.

Please note that these are general FAQs, and the specific usage and behavior may vary depending on your Docker version and configuration. It’s always recommended to refer to the official Docker and Kubernetes documentation for detailed information and guidelines.

That’s all about the differences between Docker’s ENTRYPOINT and CMD instructions. Remember to choose the appropriate instruction based on your specific use case.

0 Shares:
You May Also Like