由于bash进入posix模式,因此无法在docker构建期间使用进程替换 [英] Cannot use process substitution during docker build because bash goes into posix mode

查看:189
本文介绍了由于bash进入posix模式,因此无法在docker构建期间使用进程替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Dockerfile中,我想使用进程替换:

  RUN echo<(echo'$ DATA:' $ DATA)

但是 docker build 运行每个带有 / bin / sh 的RUN命令。显然,以 sh 运行会导致bash切换到POSIX模式,该模式不允许进程替换:


  / bin / sh:-c:第0行:意外标记'('

我尝试关闭POSIX模式:

 运行集+ o posix&& echo<(echo'$ DATA:' $ DATA)

但是似乎语法错误甚至在运行第一个命令之前就发生了。如果我将& 替换为;也是一样。。



请注意,该命令(甚至是我在此处用作简化示例的命令)都包含单引号和双引号,所以我不能只需在 bash -c 前面加上前缀。



使用的shell实际上是bash,但它被称为<$ c码头工人$ c> / bin / sh


 步骤7 :RUN ls -l / bin / sh 
--->在93a9809e12a7
中运行lrwxrwxrwx 1根根9 Dec 28 03:38 / bin / sh-> / bin / bash



解决方案

如果您确定正在构建映像中有 bash ,则可以使用 SHELL 命令,我在另一个问题中进行了介绍。



您可以使用 SHELL [ / bin / bash, -c] 。考虑:

  $ docker build --no-cache-< <(echo'
> FROM fedora
> RUN cat<(echo hello world)
>')
将构建上下文发送到Docker守护程序2.048kB
步骤1/2:FROM fedora
---> ef49352c9c21
步骤2/2:RUN cat<(回声你好,世界)
--->在573730ced3a3中运行
/ bin / sh:-c:第0行:意外令牌附近的语法错误((
/ bin / sh:-c:第0行:cat<(echo hello world) '
命令'/ bin / sh -c cat<(echo hello world)'返回了非零代码:1
$ docker build --no-cache-<<(echo '
> FROM fedora
> SHELL [ / bin / bash, -c]
> RUN cat<(echo hello world)
> ')
将构建上下文发送到Docker守护程序2.048kB
步骤1/3:FROM fedora
---> ef49352c9c21
步骤2/3:SHELL [ / bin / bash, -c]
--->在e78260e6de42
中运行卸下中间容器e78260e6de42
---> ff6ec782a9f6
步骤3/3:运行cat< ;(echo hello world)
--->在afbb42bba5b4中运行
hello world
移除中间容器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

这篇关于由于bash进入posix模式,因此无法在docker构建期间使用进程替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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