Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
909 views
in Technique[技术] by (71.8m points)

dockerfile - 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?

question from:https://stackoverflow.com/questions/46797348/docker-cmd-exec-form-for-multiple-command-execution

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...