6

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.

0

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.