【发布时间】:2019-02-25 16:41:02
【问题描述】:
我正在尝试将以下字符串打印到文件中:
"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"
【问题讨论】:
-
使用双反斜杠
\\$xHbbbbbbbb -
@ponury-kostek 它不起作用:
Step 1/2 : FROM alpine:latest ---> caf27325b298 Step 2/2 : RUN echo "hellow world \\n" > /myfile2 ---> Running in 04e688ab920a ---> 381811b5961e Removing intermediate container 04e688ab920a Successfully built 381811b5961e -
你不应该使用
cat > Deleteme <<'EOF'吗?
标签: docker dockerfile alpine