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

前端 未结 16 1753
自闭症患者
自闭症患者 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:12

    Yes, that is a good question. I don't understand it fully yet, but:

    I understand that ENTRYPOINT is the binary that is being executed. You can overide entrypoint by --entrypoint="".

    docker run -t -i --entrypoint="/bin/bash" ubuntu
    

    CMD is the default argument to container. Without entrypoint, default argument is command that is executed. With entrypoint, cmd is passed to entrypoint as argument. You can emulate a command with entrypoint.

    # no entrypoint
    docker run ubuntu /bin/cat /etc/passwd
    
    # with entry point, emulating cat command
    docker run --entrypoint="/bin/cat" ubuntu /etc/passwd
    

    So, main advantage is that with entrypoint you can pass arguments (cmd) to your container. To accomplish this, you need to use both:

    # Dockerfile
    FROM ubuntu
    ENTRYPOINT ["/bin/cat"]
    

    and

    docker build -t=cat .
    

    then you can use:

    docker run cat /etc/passwd
    #              ^^^^^^^^^^^
    #                   CMD
    #          ^^^      
    #          image (tag)- using the default ENTRYPOINT
    

提交回复
热议问题