Docker D2 Assignment – CMD V/s Entrypoint

5 Differences

CMDEntrypoint
It can be replaced by command
in docker run

Ex:-
[root@ip-172-31-17-58 sample3]# docker run -it test-sample1 /bin/bash
root@373fb5615122:/#


Although dockerfile had

CMD /usr/sbin/apache2ctl -D FOREGROUND

If attempted to be replaced via docker run command
it will instead be added as a run time argument to actual entry point command
Ex:-
[root@ip-172-31-17-58 sample3]# docker run test-sample3 /bin/bash
Hello /bin/bash


where dockerfile had entrypoint as below

ENTRYPOINT [“/bin/echo”, “Hello”]
CMD will be included as a 2nd command to be executed along
with Entrypoint

For ex – See the Section 1 CMD with 1 Entrypoint
Entrypoint can not be overridden by CMD

For ex – See the Section 1 CMD with 1 Entrypoint
CMD can also be overwritten by another CMD

See section 2 CMD
Entrypoint can only be overwritten by another entrypoint

See section 2 Entrypoints
CMD will only be PID 1 if Entrypoint not mentionedThe last entrypoint is the PID 1
Multiple params can not be added to it once image builtMultiple params for a running container can be added

Same as 1st row example

2 CMD

Latest CMD command overrode the 1st CMD command

e.g. for dockerfile where we had highlighted lines:-

FROM ubuntu
MAINTAINER Rajesh Kumar << rajesh@scmgalaxy.com>>
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get -y install tzdata
RUN apt-get install git -y && apt-get install -yq apache2
CMD echo "Hello world"
CMD ["/bin/echo", "Hello"]

output was

[root@ip-172-31-17-58 two-cmd]# docker run two-cmd
Hello

1 CMD with 1 Entrypoint

Entrypoint command was executed first along with CMD command when docker run invoked.

e.g. for dockerfile where we had highlighted lines:-

FROM ubuntu
MAINTAINER Rajesh Kumar << rajesh@scmgalaxy.com>>
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get -y install tzdata
RUN apt-get install git -y && apt-get install -yq apache2
CMD echo "Hello world"
ENTRYPOINT ["/bin/echo", "Hello"]

We had the container run with highlighted command where we can see both being executed

CONTAINER ID   IMAGE               COMMAND             CREATED          STATUS                      PORTS     NAMES

[root@ip-172-31-17-58 centos]# docker ps -a --no-trunc | grep test-sample5
bb7c1f162da7e2a4a31af7d7f795ae982bb324210cfa0e8c2c2c5b52cc1f5675   test-sample5                                                              "/bin/echo Hello /bin/sh -c 'echo \"Hello world\"'"   About a minute ago   Exited (0) About a minute ago                                                                                  brave_bassi

2 Entrypoints

Last entrypoint command overrode the previous one

e.g. for dockerfile where we had highlighted lines:-

FROM ubuntu
MAINTAINER Rajesh Kumar << rajesh@scmgalaxy.com>>
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get -y install tzdata
RUN apt-get install git -y && apt-get install -yq apache2
ENTRYPOINT ["/bin/echo", "Hello world"]
ENTRYPOINT ["/bin/echo", "Hello"]
[root@ip-172-31-17-58 2-entry]# docker run two-rp
Hello