5

I've seen How do I make a comment in a Dockerfile?, and it doesn't answer this question.

The Docker documentation are ambiguous regarding location of the hash in a Dockerfile:

Docker treats lines that begin with # as a comment

It's not clear whether whitespace before the hash is allowed. My testing seems to indicate that it is allowed, but I'm looking for a definitive answer.

The same page is unambiguous about location of the hash in a .dockerignore file:

If a line in .dockerignore file starts with # in column 1, then this line is considered as a comment and is ignored before interpreted by the CLI.

The lack of ambiguity there would seem to imply the same does not apply to Dockerfile comments.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andy Madge
  • 624
  • 5
  • 17
  • 3
    Why wouldn't you just try it? – Oleg Sklyar May 15 '18 at 11:11
  • 4
    I did try it, that's why I said "My testing seems to indicate that it is allowed, but I'm looking for a definitive answer." It seems to work, but I don't want to use it if it's not supposed to be that way. – Andy Madge May 15 '18 at 21:03
  • I downvotes and I commented: the question is not really worth asking as even if space is permitted ahed of hash (and seems it is) I have not seen a single Dockerfile that would really benefit from formatting comments like this. Normally in code line comments are aligned vertically with scope blocks and in a Dockerfile there is only one scope level. It at all this question should be put directly to the docker team suggesting to improve documentation. – Oleg Sklyar May 15 '18 at 21:27
  • Your comment was wrong though wasn't it - I said quite clearly in the question that I already tried it. The purpose of StackOverflow is to answer questions, not to say "you're wrong to ask that question". I take your point about asking the Docker team though - I'll do that. – Andy Madge May 15 '18 at 21:34
  • You're wrong about the indentation - Pretty much every Dockerfile I've ever seen does use indentation for continuation lines in RUN commands. This is the specific case I'm interested in, hence the question. – Andy Madge May 15 '18 at 21:36
  • Scope and indentation are not the same thing, but really, it does not matter. Given that there is indeed a statement that you did test it in your question (at least now) I would be happy to drop the down vote but SO locks it. Sorry for that, probably my bad. Still, the problem is not worth the time spent here on discussion IMO. And then comment is just a comment and down vote can be overwritten by upvotes if others think the question is worth it. It looks like the others do not think that way. – Oleg Sklyar May 15 '18 at 21:39
  • 1
    Of course they're not, that was my point. Scope is not relevant, but indentation is - it's the whole point of the question. Just because _you_ don't know the answer or think it's worthwhile, doesn't mean it's not a valid question. Anyway, thanks for your time. – Andy Madge May 15 '18 at 21:51

2 Answers2

4

Looking at the Docker CLI files:

On the file parser line 45 we find

line := strings.TrimLeftFunc(string(scannedBytes), unicode.IsSpace)

It trims empty spaces from the left. So if the non-first space character would be a # that would count as a comment for any code that follows the left trim.

The isSpace function checks for the following characters

'\t', '\n', '\v', '\f', '\r', ' ', U+0085 (NEL), U+00A0 (NBSP).

These would all be removed by the code on line 45 until they encounter a character that does not fit these specifications.

# Nothing trimmed
           # 1 tab 7 spaces trimmed
    0 # 4 spaces trimmed

Then on line 48 we find where it tests if it's a comment

  if len(line) > 0 && !strings.HasPrefix(line, "#") {

So any space characters that are stripped by strings.TrimLeftFunc will not "invalidate" a comment.

So in conclusion on your question Does the hash '#' in a Dockerfile comment need to be in column 1? the answer is no, it can be preceded by space characters and still remain a comment.

# Nothing trimmed   < -- comment
# 1 tab 7 spaces trimmed < -- comment
0 # 4 spaces trimmed  < -- not a comment
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
1

The Docker documentation says more:

Docker treats lines that begin with # as a comment, unless the line is a valid parser directive. A # marker anywhere else in a line is treated as an argument.

I would take that literally, meaning, yes, it must be in column 1.

Since in your case you don't have it at the beginning of the line, and no command precedes it, then it cannot be a argument to anything, and will stay a comment.

Just a couple days ago, I found this question on SO: Jenkins-Run Docker: COPY failed: stat /var/lib/docker/tmp/docker-builder...: no such file or directory

Perplexabot
  • 1,852
  • 3
  • 19
  • 22