Dockerfile RUN命令损坏。如何正确逃脱?

时间:2020-01-04 20:30:23

标签: docker dockerfile

使用ubuntu:latest码头工人图片...

此命令在命令行上可以正常工作:

if [ "`echo hi there`" == "hi there" ]; then echo it worked; else echo nope; fi

root@9df9198ced39:/# if [ "`echo hi there`" == "hi there" ]; then echo it worked; else echo nope; fi
it worked

但是在Dockerfile RUN命令中,它没有:

FROM ubuntu
RUN if [ "`echo hi there`" == "hi there" ]; then echo it worked; else echo nope; fi

sh: 1: [: hi there: unexpected operator

我尝试转义"backtick[]。我尝试使用# escape=\ ...我尝试了JSON语法:

RUN ["sh", "-c", "if [ \"`echo hi there`\" == \"hi there\" ]; then echo it worked; else echo nope; fi"]

...似乎似乎找不到正确的方法。我要去哪里错了?

1 个答案:

答案 0 :(得分:3)

使用'='而不是'=='

获得不同结果的原因可能是由于不同的shell解释器。

$ bash
$ if [ "`echo hi there`" = "hi there" ]; then echo it worked; else echo nope; fi
it worked
$ if [ "`echo hi there`" == "hi there" ]; then echo it worked; else echo nope; fi
it worked

$ sh
$ if [ "`echo hi there`" = "hi there" ]; then echo it worked; else echo nope; fi
it worked
$ if [ "`echo hi there`" == "hi there" ]; then echo it worked; else echo nope; fi
sh: 2: [: hi there: unexpected operator
相关问题