Stacey
Stacey

Reputation: 23

When do you have to use `/bin/sh -c "..."` in the CMD directive in a Dockerfile?

I have been studying Dockerfile implementations of several projects and I noticed that some of them start the application with:

CMD executable

while other do:

CMD /bin/sh -c "executable"

I have been trying both versions for each project, and don't really notice a difference.

Is there a difference? If so, when is which version preferred?

Upvotes: 2

Views: 3550

Answers (1)

pcarter
pcarter

Reputation: 1618

From the docker documentation (https://docs.docker.com/engine/reference/builder/):

  • CMD ["executable","param1","param2"] (exec form, this is the preferred form)
  • CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
  • CMD command param1 param2 (shell form)

...

Note: 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.

So

CMD executable

which uses the shell form is already implicitly invoking a shell. So there's no reason to explicit invoke one.

Upvotes: 2

Related Questions