Dockerfile has two commands related to how a Docker container runs an application: ENTRYPOINT and CMD. Both can include an executable, but they are used in different contexts and ways.
ENTRYPOINT: This is the command that gets executed when a container is started. If nothing is specified, Docker uses a default entrypoint. You can use this command when you want a container to run as an executable.
CMD: It is an instruction used to provide defaults for an executing container. These defaults can include an executable or they can omit the executable, in which case you must specify an ENTRYPOINT instruction.
The main differences between the two lie in how they interact with Docker run commands and how they interact with each other.
Example:
You might use CMD to run an application within the container when it starts, which can later be overridden with command line arguments:
CMD [“python3”, “app.py”]
Conversely, an ENTRYPOINT command and parameters will not be ignored when Docker starts. You could use it to run a specific software:
ENTRYPOINT [“python3”]
If both are used, ENTRYPOINT parameters become the main command, and CMD parameters become default options. For example:
ENTRYPOINT [“python3”]
CMD [“app.py”]
Here, CMD provides default arguments to ENTRYPOINT and they can be overridden by command line arguments.