Docker CMD exec-form for multiple command execution

DockerDockerfile

Docker Problem Overview


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?

Docker Solutions


Solution 1 - Docker

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"]

Solution 2 - Docker

I would argue though that these commands should be execute with at a RUN step rather than CMD.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZakView Question on Stackoverflow
Solution 1 - DockerZakView Answer on Stackoverflow
Solution 2 - DockerIvasanView Answer on Stackoverflow