Dockerfile 运行 命令忽略字符串中以“$”开头的字符
Dockerfile RUN command omits characters starts with "$" from a string
我正在尝试将以下字符串打印到文件中:
"hellow world $xHbbbbbbbb"
我有两种选择:
printf "hellow world $xHbbbbbbbb\n" > /myfile1
echo "hellow world $xHbbbbbbbb\n" > /myfile2
它在终端上工作正常。
当我像这样使用 Dockerfile 构建它时:
cat > Deleteme <<EOF
FROM alpine:latest
RUN printf "hellow world $xHbbbbbbbb\n" > /myfile1
RUN echo "hellow world $xHbbbbbbbb\n" > /myfile2
EOF
docker build -t deleteme -f Deleteme .
docker run --rm -it deleteme sh -c "cat /myfile1 && cat /myfile2"
输出为:
hellow world
hellow world \n
为什么 RUN
命令省略了 $xHbbbbbbbb
?
我想是因为 $
将其标识为变量,但它在终端上对我有用,所以我不明白为什么它在 Dockerfile 上也不起作用。
如何将以下字符串写入文件:
"hellow world $xHbbbbbbbb"
在 Dockerfile 中,$xHbbbbbbbb
确实评估为环境变量
(有关用法和示例,请参阅 Docker Documentation | Environment replacement)。
要获得预期的结果,您需要转义 \
和 $
。
此外,在 echo
中,\n
不会被解释为换行符,除非指定了 -e
选项,但看起来您可以省略它(参见 echo man page更多)。
放在一起:
cat > Deleteme <<EOF
FROM alpine:latest
RUN printf "hellow world \$xHbbbbbbbb\n" > /myfile1
RUN echo "hellow world \$xHbbbbbbbb" > /myfile2
EOF
以下 Deleteme
文件的结果:
FROM alpine:latest
RUN printf "hellow world $xHbbbbbbbb\n" > /myfile1
RUN echo "hellow world $xHbbbbbbbb" > /myfile2
和docker
输出:
hellow world $xHbbbbbbbb
hellow world $xHbbbbbbbb
我正在尝试将以下字符串打印到文件中:
"hellow world $xHbbbbbbbb"
我有两种选择:
printf "hellow world $xHbbbbbbbb\n" > /myfile1
echo "hellow world $xHbbbbbbbb\n" > /myfile2
它在终端上工作正常。
当我像这样使用 Dockerfile 构建它时:
cat > Deleteme <<EOF
FROM alpine:latest
RUN printf "hellow world $xHbbbbbbbb\n" > /myfile1
RUN echo "hellow world $xHbbbbbbbb\n" > /myfile2
EOF
docker build -t deleteme -f Deleteme .
docker run --rm -it deleteme sh -c "cat /myfile1 && cat /myfile2"
输出为:
hellow world
hellow world \n
为什么 RUN
命令省略了 $xHbbbbbbbb
?
我想是因为 $
将其标识为变量,但它在终端上对我有用,所以我不明白为什么它在 Dockerfile 上也不起作用。
如何将以下字符串写入文件:
"hellow world $xHbbbbbbbb"
在 Dockerfile 中,$xHbbbbbbbb
确实评估为环境变量
(有关用法和示例,请参阅 Docker Documentation | Environment replacement)。
要获得预期的结果,您需要转义 \
和 $
。
此外,在 echo
中,\n
不会被解释为换行符,除非指定了 -e
选项,但看起来您可以省略它(参见 echo man page更多)。
放在一起:
cat > Deleteme <<EOF
FROM alpine:latest
RUN printf "hellow world \$xHbbbbbbbb\n" > /myfile1
RUN echo "hellow world \$xHbbbbbbbb" > /myfile2
EOF
以下 Deleteme
文件的结果:
FROM alpine:latest
RUN printf "hellow world $xHbbbbbbbb\n" > /myfile1
RUN echo "hellow world $xHbbbbbbbb" > /myfile2
和docker
输出:
hellow world $xHbbbbbbbb
hellow world $xHbbbbbbbb