简体   繁体   中英

Creating bash script from Dockerfile strips comments

I'm creating a Docker container which needs to create a certain bash script. I use the following RUN sentence inside my Dockerfile:

RUN printf "#!/bin/bash\n \
# This is a bash comment inside the script\
ls -l" > /home/myuser/script.sh

It works well, but the resulting script is just:

#!/bin/bash
ls -l

So, bash comment is missing from the final file. I suspect that the reason is that Docker assumes that the line is a Dockerfile comment, but since it's enclosed inside double quotes, I think it's clear that's not the case.

Of course I could solve the issue by just including the full script in a single line, or placing it in an external file, but I think it should be possible to include bash comments inside a multiline, quoted string without this kind of problems. Any workaround? I've tried all kind of escapings, without success.

You're right, it's a little weird that Docker interprets that as a Dockerfile comment instead of a comment inside a string. As a workaround, I got the following to work

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

Results in this output

Step 3 : RUN cat /script.sh
 ---> Running in afc19e228656
#!/bin/bash 
# This is a bash comment inside the script 
ls -l

If you move \\n to the beginning of the comment line it still generates the correct output but no longer treats that line as a Dockerfile comment line.


Assuming I found the right command parsing code , and I'm reading it correctly, Docker strips comments out before attempting to parse the line to see if it has any commands on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM