Comments in a Dockerfile

This is a note to my future self after spending way too much time finding why my Dockerfile did not work: Dockerfile comments start at the beginning of a line.

An unclear error message

It all starts with a simple Dockerfile:

Dockerfile
FROM alpine:3.7
COPY test /tmp  # Copy `test` to `/tmp`

test is a file in the current directory, next to the Dockerfile.
Build the image with docker build .:

Step 1/2 : FROM alpine:3.7
 ---> 6d1ef012b567
Step 2/2 : COPY test /tmp  # Copy `test` to `/tmp`
COPY failed: stat /home/docker/tmp/docker-builder000357151/tmp: no such file or directory

With such an error message, I was lost as to what real the error was…

Comments only start at the beginning of a line

As stated in Docker docs:

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.

The fix is just to move the comment…

FROM alpine:3.7
# Copy `test` to `/tmp`
COPY test /tmp

I understand where the error comes from, but the error message could be improved a litte bit in that case.