What is the difference between CMD and ENTRYPOINT in a Dockerfile?

前端 未结 16 1739
自闭症患者
自闭症患者 2020-11-21 22:31

In Dockerfiles there are two commands that look similar to me: CMD and ENTRYPOINT. But I guess that there is a (subtle?) difference between them -

相关标签:
16条回答
  • 2020-11-21 23:26

    I run across this and at the beginning I found it really confusing to be honest and I think this confusion comes from using the word "CMD" because in fact what goes there acts as argument. So after digging a little bit I understood how it works. Basically:

    ENTRYPOINT --> what you specify here would be the command to be executed when you container starts. If you omit this definition docker will use /bin/sh -c bash to run your container.

    CMD --> these are the arguments appended to the ENTRYPOINT unless the user specifies some custom argument, i.e: docker run ubuntu <custom_cmd> in this case instead of appending what's specified on the image in the CMD section, docker will run ENTRYPOINT <custom_cmd>. In case ENTRYPOINT has not been specified, what goes here will be passed to /bin/sh -c acting in fact as the command to be executed when starting the container.

    As everything it's better to explain what's going on by examples. So let's say I create a simple docker image by using the following specification Dockerfile:

    From ubuntu
    ENTRYPOINT ["sleep"]
    

    Then I build it by running the following:

    docker build . -t testimg
    

    This will create a container that everytime you run it sleeps. So If I run it as following:

    docker run testimg
    

    I'll get the following:

    sleep: missing operand
    Try 'sleep --help' for more information.
    

    This happens because the entry point is the "sleep" command which needs an argument. So to fix this I'll just provide the amount to sleep:

    docker run testimg 5
    

    This will run correctly and as consequence the container will run, sleeps 5 seconds and exits. As we can see in this example docker just appended what goes after the image name to the entry point binary docker run testimg <my_cmd>. What happens if we want to pass a default value (default argument) to the entry point? in this case we just need to specify it in the CMD section, for example:

    From ubuntu
    ENTRYPOINT ["sleep"]
    CMD ["10"]
    

    In this case if the user doesn't pass any argument the container will use the default value (10) and pass it to entry point sleep.

    Now let's use just CMD and omit ENTRYPOINT definition:

    FROM ubuntu
    CMD ["sleep", "5"]
    

    If we rebuild and run this image it will basically sleeps for 5 seconds.

    So in summary, you can use ENTRYPOINT in order to make your container acts as an executable. You can use CMD to provide default arguments to your entry point or to run a custom command when starting your container that can be overridden from outside by user.

    0 讨论(0)
  • 2020-11-21 23:29

    The official documentation of Dockerfile best practices does a great job explaining the differences. Dockerfile best practices

    CMD:

    The CMD instruction should be used to run the software contained by your image, along with any arguments. CMD should almost always be used in the form of CMD ["executable", "param1", "param2"…]. Thus, if the image is for a service, such as Apache and Rails, you would run something like CMD ["apache2","-DFOREGROUND"]. Indeed, this form of the instruction is recommended for any service-based image.

    ENTRYPOINT:

    The best use for ENTRYPOINT is to set the image’s main command, allowing that image to be run as though it was that command (and then use CMD as the default flags).

    0 讨论(0)
  • 2020-11-21 23:31

    CMD command mentioned inside Dockerfile file can be overridden via docker run command while ENTRYPOINT can not be.

    0 讨论(0)
  • 2020-11-21 23:33

    I have read all answers and I want to summarize for better understanding at first glance like following:

    Firstly, the whole command that gets executed in the container includes two parts: the command and the arguments

    • ENTRYPOINT defines the executable invoked when the container is started (for command)

    • CMD specifies the arguments that get passed to the ENTRYPOINT (for arguments)

    In the Kubernetes In Action book points an important note about it. (chapter 7)

    Although you can use the CMD instruction to specify the command you want to execute when the image is run, the correct way is to do it through the ENTRYPOINT instruction and to only specify the CMD if you want to define the default arguments.

    You can also read this article for great explanation in a simple way

    0 讨论(0)
提交回复
热议问题