-
Notifications
You must be signed in to change notification settings - Fork 18.9k
Don't treat comment-only lines in Dockerfiles as empty continuation lines. #34333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…ines. Signed-off-by: Robert Rollins <rrollins@caltech.edu>
|
Unfortunately I think we have to deprecate this first and warn about it, before removing it. |
|
Yes, I think this is the same change we made a while back, and it broke tons of Dockerfiles. We then reverted that change, knowing it's ugly, but probably something we'll have to accept being something we can't change. The (probably confusing) part is that comment lines here are Dockerfile comments, not shell comments |
|
I'm curious how this same change broke tons of Dockerfiles. My Dockerfiles work just fine with or without comments between continuation lines right now. It's just that the build command started complaining today (when I updated to Docker for Mac's latest build) about having supposedly blank lines between continuations, which are in fact non-blank lines which just have a comment. |
Hm, that's something that should be fixed, probably requires detecting if it was an empty file, or a comment line.
It's kinda horrid, but the use case is to allow blank lines in |
|
Why would it be horrid to allow blank lines in RUN statements? It is a really good way of reducing layers in an image and keep readability at the same time. Example: |
|
I think the expected behaviour here is what is supported by a shell. Ignoring blank lines and allowing comment lines in continuations does not match the behaviour of a shell. |
@baptiste-ls multiple reasons; first of all it breaks the expectation that instructions inside a FROM busybox
RUN echo "foo" \
# install the bar package and dependencies
# TODO disabled; the bar package is broken
&& echo "bar" \
# install the baz package
&& echo "baz"
# Now run some configuration
RUN echo "something else"FROM busybox
RUN echo "foo" \
# install the bar package and dependencies
# TODO disabled; the bar package is broken
#&& echo "bar" \
# install the baz package
#&& echo "baz"
# Now run some configuration
RUN echo "something else"While the first executes |
|
@dnephin yes, but changing that was a breaking change; we can possibly reconsider when/if the here doc proposal is there, but even then, it's gonna be hard. I do think we should look at the errors for comment-lines, because those should be valid, and not produce a warning |
Untrue. The following script prints 'hi' without error or warning in bash 3.2.25 and 4.4.0, as well as zsh 4.2.6 and 5.0.8: |
|
That's a fair point. I didn't realize that worked until I re-read the description in this PR. However this doesn't work: echo hi \
# comment
morethingshereSo to support the shell behaviour correctly we'd actually have to fully parse the line as a shell command instead of just shlex the line. I would guess this broken example is more common. If you want to comment between commands you could just as easily run with |
This is something I do frequently in my docker files to explain why I do some operations in multi-line/mutli-command RUN statments. |
|
I also use full line comments (sometimes multi-line comments) to describe sections of a large multi-line RUN command all the time. Please accept this PR and don't deprecate the usage. |
|
Given how our instructions work and are parsed, we cannot reasonably support comments only in the same way the shell does (which is the reason for our divergence from the shell in the first place). In a normal shell script, if you want a comment in a long block of code, you can simply add it between statements and everything works. In our case, the continuations are not for the shell, but for the For example, in shell, one would write something like this: set -e
stuff
stuff
# comment
stuff
stuff
# more
# comment
stuff stuffIn a RUN set -e; \
\
stuff; \
stuff; \
# comment
stuff; \
stuff; \
# more
# comment
stuff stuffWhich is fine, and still really readable (and fairly easy to write). The flip side is that this actually simply RUN set -e; stuff; stuff; stuff; stuff; stuff stuff(including my tab characters, but not any newlines or comments) If we included the comments in that parsing (assuming I added RUN set -e; stuff; stuff; # comment stuff; stuff; # more # comment stuff stuffThis makes it very clear that everything after that first Now, there's some discussion in #34423 about making a much easier syntax that would allow for actual newlines in a The point stands that these comment lines are the only good way we currently have to put comments in the middle of a long As for empty lines, I think the requirement of adding |
|
I opened #35004 as alternative solution for comment-only lines |
|
I fully support #35004, as it solves this problem much more effectively than mine. I'll close this PR. |
|
Thanks @coredumperror ! |
Signed-off-by: Robert Rollins rrollins@caltech.edu
- What I did
Changed the Dockerfile parser to make it treat lines that contain only a comment between two lines of a multi-line RUN command as non-empty continuation lines.
- Why I did it
Disallowing comments between lines not only makes it that much harder to accurately document long, complicated RUN commands, it also doesn't match how Bash handles this. This bash script works just fine:
Echoing 'hi' with no errors. So why disallow this in Dockerfiles?
- How to verify it
I dunno. Looks pretty straightforward.
- Description for the changelog
Lines in Dockerfiles containing only a comment are no longer treated as empty continuation lines.
- A picture of a cute animal (not mandatory but encouraged)

Here's my cat Rita, being a total goof.