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

查看:119
本文介绍了用于多命令执行的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.

shell-form:

shell-form:

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

exec-form:

exec-form:

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天全站免登陆