我正在创建一个需要创建某个bash脚本的Docker容器。我在Dockerfile中使用以下RUN语句:
RUN printf "#!/bin/bash\n \
# This is a bash comment inside the script\
ls -l" > /home/myuser/script.sh
效果很好,但生成的脚本只是:
#!/bin/bash
ls -l
因此,最终文件中缺少bash注释。我怀疑原因是Docker假定该行是Dockerfile注释,但由于它是用双引号括起来的,我认为很明显不是这样。
当然,我可以通过将完整脚本包含在一行中,或者将其放在外部文件中来解决问题,但我认为应该可以在多行引号字符串中包含bash注释,而没有这种情况。问题。任何解决方法?我已经尝试了所有类型的逃避,但没有成功。
答案 0 :(得分:3)
你是对的,Docker将其解释为Dockerfile注释而不是字符串中的注释有点奇怪。作为一种解决方法,我得到以下工作
FROM ubuntu:latest
RUN printf "#!/bin/bash \
\n# This is a bash comment inside the script \
\nls -l\n" > /script.sh
RUN cat /script.sh
此输出中的结果
Step 3 : RUN cat /script.sh
---> Running in afc19e228656
#!/bin/bash
# This is a bash comment inside the script
ls -l
如果将\n
移到注释行的开头,它仍会生成正确的输出,但不再将该行视为Dockerfile注释行。
假设我找到了right command parsing code,并且我正确地读了它,Docker会在尝试解析该行之前删除注释,看它是否有任何命令。