Home > database >  dockerfile CMD executable and command
dockerfile CMD executable and command

Time:10-12

in part of docker document,


CMD ["executable","param1","param2"] (exec form, this is the preferred form)

CMD command param1 param2 (shell form)


what is executable, command meaning?

I have confused about that.

What I think is ll, cd, ls, chmod, cp.

They are all executable and command.

Because I try this in dockerfile

CMD ["ll", "-a"]

It's doesn't work.

But when we type ll in teminal, it's work, I think it's just execute ll.

I think I had some wrong concept about that.

CodePudding user response:

CMD

Usage:

  1. CMD ["<executable>","<param1>","<param2>"] (exec form, this is the preferred form)

From the docs

  • 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.

  • If you want to run your <command> without a shell then you must express the command as a JSON array and give the full path to the executable. This array form is the preferred format of CMD. Any additional parameters must be individually expressed as strings in the array:

  1. CMD ["<param1>","<param2>"] (as default parameters to ENTRYPOINT)

  2. CMD <command> <param1> <param2> (shell form)

If you use the shell form of the CMD, then this will execute in /bin/sh -c:

Coming to your problem

Exec Way

if you want to execute ll command using executable way then you would have two options available

  • Add "sh" as first param like CMD ["sh", "-c", "ll", "-a"].
  • Or provide absolute path of ll(It is alias to ls -lh) executable like CMD [ "/bin/ls", "-lh" "-a"]

Shell way

if you want to execute ll command using shell way then you can use like this CMD ll

References :

https://docs.docker.com/engine/reference/builder/#cmd

  • Related