In a Dockerfile, ENTRYPOINT and CMD are both used to specify the command that should be run when a container is started. However, they have slightly different functions and usage.
CMD specifies the default command that should be run when a container is started. It can be used in two ways:
As a string: When used as a string, CMD specifies the command to be executed, along with any arguments. For example:
CMD ["python", "app.py"]
As an array: When used as an array, CMD specifies the command to be executed and any arguments, as separate elements. For example:
CMD python app.py
If a command is specified in both CMD and the docker run command, the docker run command takes precedence.
ENTRYPOINT, on the other hand, specifies the command that should always be executed when a container is started, even if a command is specified in the docker run command. It can also be used in two ways:
As a string: When used as a string, ENTRYPOINT specifies the command to be executed, along with any arguments. For example:
ENTRYPOINT ["python", "app.py"]
As an array: When used as an array, ENTRYPOINT specifies the command to be executed and any arguments, as separate elements. For example:
ENTRYPOINT python app.py
If a command is specified in the docker run command, it will be passed as arguments to the ENTRYPOINT command.
To summarize, CMD specifies the default command to be executed when a container is started, and can be overridden by a command specified in the docker run command. ENTRYPOINT, on the other hand, specifies the command that should always be executed when a container is started, and can be combined with CMD to provide default arguments to the command.