在 docker build 期间无法使用进程替换,因为 bash 进入 posix 模式 [英] Cannot use process substitution during docker build because bash goes into posix mode
问题描述
在 Dockerfile 中,我想使用进程替换:
运行 echo <(echo '$DATA:'"$DATA")
但是 docker build
使用 /bin/sh
运行每个 RUN 命令.显然作为 sh
运行会导致 bash 切换到不允许进程替换的 POSIX 模式:
/bin/sh: -c: line 0: 意外标记 `(' 附近的语法错误
我尝试关闭 POSIX 模式:
RUN set +o posix &&echo <(echo '$DATA:'"$DATA")
但似乎语法错误甚至在第一个命令运行之前就发生了.如果我用 ;
替换 &&
也是一样.
请注意,该命令(即使是我在此处用作简化示例的命令)同时包含单引号和双引号,因此我不能简单地添加 bash -c
.
使用的shell实际上是一个bash,但是它被docker调用为/bin/sh
:
第 7 步:运行 ls -l/bin/sh--->在 93a9809e12a7 中运行lrwxrwxrwx 1 根 9 Dec 28 03:38/bin/sh ->/bin/bash
如果您确定正在构建的映像中有 bash
,那么您可以使用 更改 shell 调用SHELL
命令,我在another question 中对此进行了描述.
您可以使用 SHELL [ "/bin/bash", "-c" ]
.考虑:
$ docker build --no-cache - <<(回声'>来自软呢帽>RUN cat <(echo hello world)>')将构建上下文发送到 Docker 守护进程 2.048kB步骤 1/2:来自 Fedora--->ef49352c9c21Step 2/2 : RUN cat <(echo hello world)--->在 573730ced3a3 中运行/bin/sh: -c: 第 0 行:意外标记 `(' 附近的语法错误/bin/sh: -c: line 0: `cat <(echo hello world)'命令 '/bin/sh -c cat <(echo hello world)' 返回一个非零代码:1$ docker build --no-cache - <<(回声'>来自软呢帽>外壳 ["/bin/bash", "-c"]>RUN cat <(echo hello world)>')将构建上下文发送到 Docker 守护进程 2.048kB第 1/3 步:来自 Fedora--->ef49352c9c21第 2/3 步:外壳 ["/bin/bash", "-c"]--->在 e78260e6de42 中运行移除中间容器 e78260e6de42--->ff6ec782a9f6Step 3/3 : RUN cat <(echo hello world)--->在 afbb42bba5b4 中运行你好世界移除中间容器 afbb42bba5b4--->25f756dcff9b成功搭建25f756dcff9b
In a Dockerfile, I want to use process substitution:
RUN echo <(echo '$DATA:'"$DATA")
But docker build
runs every RUN command with /bin/sh
. Apparently being run as sh
causes bash to switch to POSIX mode, which does not allow process substitution:
/bin/sh: -c: line 0: syntax error near unexpected token `('
I tried switching off POSIX mode:
RUN set +o posix && echo <(echo '$DATA:'"$DATA")
But it seems the syntax error happens even before the first command is run. Same if I replace &&
with ;
.
Note that the command (even the one that I used as a simplified example here) contains both single and double quotes, so I can't simply prepend bash -c
.
The used shell is actually a bash, but it is invoked as /bin/sh
by docker:
Step 7 : RUN ls -l /bin/sh ---> Running in 93a9809e12a7 lrwxrwxrwx 1 root root 9 Dec 28 03:38 /bin/sh -> /bin/bash
If you are sure you have bash
in your image being built, then you can change the shell invokation by using the SHELL
command, which I described in another question.
You can use SHELL [ "/bin/bash", "-c" ]
. Consider:
$ docker build --no-cache - < <(echo '
> FROM fedora
> RUN cat <(echo hello world)
> ')
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM fedora
---> ef49352c9c21
Step 2/2 : RUN cat <(echo hello world)
---> Running in 573730ced3a3
/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `cat <(echo hello world)'
The command '/bin/sh -c cat <(echo hello world)' returned a non-zero code: 1
$ docker build --no-cache - < <(echo '
> FROM fedora
> SHELL ["/bin/bash", "-c"]
> RUN cat <(echo hello world)
> ')
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM fedora
---> ef49352c9c21
Step 2/3 : SHELL ["/bin/bash", "-c"]
---> Running in e78260e6de42
Removing intermediate container e78260e6de42
---> ff6ec782a9f6
Step 3/3 : RUN cat <(echo hello world)
---> Running in afbb42bba5b4
hello world
Removing intermediate container afbb42bba5b4
---> 25f756dcff9b
Successfully built 25f756dcff9b
这篇关于在 docker build 期间无法使用进程替换,因为 bash 进入 posix 模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!