用于执行多个命令的 Docker CMD exec-form [英] Docker CMD exec-form for multiple command execution

查看:37
本文介绍了用于执行多个命令的 Docker CMD exec-form的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个通过 shell-form 中的 CMD 指令运行多个命令的愚蠢示例.我更喜欢使用 exec-form,但我不知道如何连接指令.

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.

外壳形式:

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

执行形式:

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

有人可以在 exec-form 中提供等效的语法吗?

Can someone provide the equivalent syntax in exec-form?

推荐答案

简短的回答是,您不能将 exec 表单中的命令链接在一起.

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

&& 是shell 的一个函数,用于将命令链接在一起.事实上,当您在 Dockerfile 中使用此语法时,您实际上是在利用 shell 功能.

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

如果您想使用 exec 形式 拥有多个命令,那么您必须使用 exec 形式 来调用 shell,如下所示...

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

这篇关于用于执行多个命令的 Docker CMD exec-form的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆