Dockerfile CMD shell versus exec form

后端 未结 1 800
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 13:43

What\'s the difference between the shell form and exec form of docker RUN and CMD statements.

eg:

RUN [ \"npm\", \"start\" ]
         


        
相关标签:
1条回答
  • 2020-12-05 14:04

    There are two differences between the shell form and the exec form. According to the documentation, the exec form is the preferred form. These are the two differences:

    The exec form is parsed as a JSON array, which means that you must use double-quotes (“) around words not single-quotes (‘).

    Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: CMD [ "sh", "-c", "echo $HOME" ]. When using the exec form and executing a shell directly, as in the case for the shell form, it is the shell that is doing the environment variable expansion, not docker.

    Some additional subtleties here are:

    The exec form makes it possible to avoid shell string munging, and to RUN commands using a base image that does not contain the specified shell executable.

    In the shell form you can use a \ (backslash) to continue a single RUN instruction onto the next line.

    There is also a third form for CMD:

    CMD ["param1","param2"] (as default parameters to ENTRYPOINT)

    Additionally, the exec form is required for CMD if you are using it as parameters/arguments to ENTRYPOINT that are intended to be overwritten.

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