docker CMD vs ENTRYPOINT

The ENTRYPOINT or CMD usually determine the single and main startup process inside the running container, The ENTRYPOINT and the CMD are the final Docker DSL invocation statements in a Dockerfile.A Dockerfile can not be complete without either one of them.

Both the ENTRYPOINT and CMD will execute a /bin/sh -c , but have significant differences.

ENTRYPOINT usually is straight forward with the and is often the statement that enables the  docker image as a runnable container. The ENTRYPOINT statement doesnot let the run time arguments get overwritten during the container runtime, instead the commands are passed on as arguments to the ENTRYPOINT, for Example when the container is started as docker run my-image -d, then the resultant -d argument is passed on as an argument to the ENTRYPOINT.
It means that only last ENTRYPOINT statement is valid in the Dockerfile

The ENTRYPOINT takes CMD as its arguments.

ENTRYPOINT command arg1 arg2

OR

ENTRYPOINT command
CMD ["arg1", "arg2"]
ENTRYPOINT ["java","-jar","app.jar"]
CMD ["-Dserver.port=8080"]

CMD on the other hand is subversive to the ENTRYPOINT.
CMD start statement can be overwritten with container arguments during the container runtime. The CMD does not execute anything at the build time.

CMD ["command","arg1", "arg2"]
CMD ["java","-Djava.security.egd=file:/dev/urandom","-jar","-Dserver.port=8080","app.jar"]

The major practical use with CMD is that the you will be able to overwrite the docker runtime command line arguments but you cannot override with you have ENTRYPOINT.

The CMD will be very beneficial in cases which require sending arguments to containers in runtime which in development and QA stages and is great for debugging environments.

The [] are not mandatory for ENTRYPOINT and CMD instruction statements inside Dockerfile but can be given for better readability when you have lot of arguments

Leave a Comment