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.
A hash is a function that converts one value to another. Hashing data is a common practice in computer science and is used for several different purposes. Examples include cryptography, compression, checksum generation, and data indexing.
A hash function is a mathematical function that converts an input value into a compressed numerical value – a hash or hash value. Basically, it's a processing unit that takes in data of arbitrary length and gives you the output of a fixed length – the hash value.
THE WORD "hash" is a variant of "hatch", which means "to inscribe with parallel lines", as in "hatchure" and "cross-hatch"; it derives from Old French hacher, meaning "to chop", and the dish called "hash" is so named because it contains chopped meat.
Hashing is simply passing some data through a formula that produces a result, called a hash. That hash is usually a string of characters and the hashes generated by a formula are always the same length, regardless of how much data you feed into it. For example, the MD5 formula always produces 32 character-long hashes.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With