Dockerfile RUN 命令被破坏.如何正确逃脱它?



使用ubuntu:latestdocker image...

此命令在命令行中工作正常:

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

但在 DockerfileRUN命令中,它不会:

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

。只是似乎找不到正确的方法。 我哪里出错了?

使用"="而不是"==">

你得到不同结果的原因可能是因为不同的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

最新更新