Differences between CMD and ENTRYPOINT- Docker Interview Questions
CMD and ENTRYPOINT are two Dockerfile instructions that together define the command that runs when your container starts.
There are three types of instructions (commands) that you use to build and run Dockerfiles:
RUN : Used to build images and install applications and packages, RUN builds a new layer over an existing image by committing the results.
CMD. Sets default parameters that can be overridden from the Docker Command Line Interface (CLI) when a container is running.
ENTRYPOINT. Default parameters that cannot be overridden when Docker Containers run with CLI parameters.
********
Any Docker image must have an ENTRYPOINT or CMD declaration for a container to start. Though the ENTRYPOINT and CMD instructions may seem similar at first glance, there are fundamental differences in how they build container images.
CMD commands are ignored by Daemon when there are parameters stated within the docker run command.
ENTRYPOINT instructions are not ignored, but instead, are appended as command-line parameters by treating those as arguments of the command.
***********
Dockerfile
FROM alpine
ENTRYPOINT ["echo","hello world"]
CMD ["This is a demo on docker interview quesions"]
docker run with out CLI arguments
docker run with CLI arguments , you can see the logs that message in CMD got replaced by CLI argument
Subscribe to my newsletter
Read articles from Jaswanth kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Jaswanth kumar
Jaswanth kumar
I have been working as a DevOps engineer @TESCRA for an Airlines Client. Mainly on Platform Engineering and Application logging and monitoring end