Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker CMD exec-form for multiple command execution

Here is a silly example of running multiple commands via the CMD instruction in shell-form. I would prefer to use the exec-form, but I don't know how to concatenate the instructions.

shell-form:

CMD mkdir -p ~/my/new/directory/ \  && cd ~/my/new/directory \  && touch new.file 

exec-form:

CMD ["mkdir","-p","~/my/new/directory/"] # What goes here? 

Can someone provide the equivalent syntax in exec-form?

like image 240
Zak Avatar asked Oct 17 '17 18:10

Zak


People also ask

How do I run multiple commands in Docker exec?

In order to execute multiple commands using the “docker exec” command, execute “docker exec” with the “bash” process and use the “-c” option to read the command as a string. Note: Simple quotes may not work in your host terminal, you will have to use double quotes to execute multiple commands.

Can I have multiple CMD commands in Dockerfile?

Docker will always run a single command, not more. So at the end of your Dockerfile, you can specify one command to run. Not more.


1 Answers

The short answer is, you cannot chain together commands in the exec form.

&& is a function of the shell, which is used to chain commands together. In fact, when you use this syntax in a Dockerfile, you are actually leveraging the shell functionality.

If you want to have multiple commands with the exec form, then you have do use the exec form to invoke the shell as follows...

CMD ["sh","-c","mkdir -p ~/my/new/directory/ && cd ~/my/new/directory && touch new.file"] 
like image 87
Zak Avatar answered Sep 22 '22 08:09

Zak