613

I am writing a Dockerfile. Is there a way to make comments in this file?

Does Docker have a comment option that takes the rest of a line and ignores it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kpie
  • 9,588
  • 5
  • 28
  • 50
  • I'm finding that if a layer is cached, commenting it out has no effect: Docker continues to build commented out layers if already cached. – Unrelated May 20 '21 at 21:17

7 Answers7

748

You can use # at the beginning of a line to start a comment (whitespaces before # are allowed):

# do some stuff
RUN apt-get update \
    # install some packages
    && apt-get install -y cron

#'s in the middle of a string are passed to the command itself, e.g.:

RUN echo 'we are running some # of cool things'
Evgeniy Berezovsky
  • 18,571
  • 13
  • 82
  • 156
Ranjeet
  • 7,722
  • 1
  • 13
  • 17
  • 19
    So the answer to the second question, *"Does Docker have a comment option that takes the rest of a line and ignores it?"*, is no? *"[Docker treats lines that begin with # as a comment](https://docs.docker.com/engine/reference/builder/#format)"*. – Peter Mortensen Apr 02 '19 at 21:04
  • 2
    Can you update your answer and make it more comprehensive/complete? For instance, answering the second question. – Peter Mortensen Apr 02 '19 at 21:10
  • 10
    [BMitch's answer](https://stackoverflow.com/a/42123446/249226) fills in critical information that is missing in this answer. – Jonathan Apr 18 '19 at 16:35
  • 2
    linking to https://stackoverflow.com/questions/53564598/how-can-i-add-comments-in-long-docker-run-commands – MacMartin Jul 31 '20 at 10:14
  • 1
    As the documentation mentions: "For backward compatibility, leading whitespace before comments (#) and instructions (such as RUN) are ignored, but discouraged." You should add the comment at the beginning of a line... https://docs.docker.com/engine/reference/builder/#format – Petr Dvořák May 09 '22 at 16:33
155

As others have mentioned, comments are referenced with a # and are documented here. However, unlike some languages, the # must be at the beginning of the line. If they occur part way through the line, they are interpreted as an argument and may result in unexpected behavior.

# This is a comment

COPY test_dir target_dir # This is not a comment, it is an argument to COPY

RUN echo hello world # This is an argument to RUN but the shell may ignore it

It should also be noted that parser directives have recently been added to the Dockerfile which have the same syntax as a comment. They need to appear at the top of the file, before any other comments or commands. Originally, this directive was added for changing the escape character to support Windows:

# escape=`

FROM microsoft/nanoserver
COPY testfile.txt c:\
RUN dir c:\

The first line, while it appears to be a comment, is a parser directive to change the escape character to a backtick so that the COPY and RUN commands can use the backslash in the path. A parser directive is also used with BuildKit to change the frontend parser with a syntax line. See the experimental syntax for more details on how this is being used in practice.

With a multi-line command, the commented lines are ignored, but you need to comment out every line individually:

$ cat Dockerfile
FROM busybox:latest
RUN echo first command \
# && echo second command disabled \
 && echo third command

$ docker build .
Sending build context to Docker daemon  23.04kB
Step 1/2 : FROM busybox:latest
 ---> 59788edf1f3e
Step 2/2 : RUN echo first command  && echo third command
 ---> Running in b1177e7b563d
first command
third command
Removing intermediate container b1177e7b563d
 ---> 5442cfe321ac
Successfully built 5442cfe321ac
BMitch
  • 231,797
  • 42
  • 475
  • 450
  • 7
    +1 for mentioning *"must be at the beginning of the line"*. What about line continuation? If a comment line ends with \, will the next line also be a comment? In other words, if a multi-line command were to be commented out, would all lines need to start with `#` or only the first line? An experiment suggests it is the former. This answer could be updated to cover that as well (making it even more awesome). – Peter Mortensen Apr 02 '19 at 21:52
  • 2
    @PeterMortensen The comment is needed on each line, docker completely ignores everything up to the linefeed. What's interesting to me is that the multi-line command can span across comments. – BMitch Apr 03 '19 at 00:10
  • For those considering editing the post for syntax highlighting, note that most of the code blocks above are Dockerfiles, not bash scripts, where the syntax highlighting is only distracting from the focus of the post. – BMitch Sep 17 '21 at 12:47
20

Use the # syntax for comments

From: https://docs.docker.com/engine/reference/builder/#format

# My comment here
RUN echo 'we are running some cool things'
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
edhurtig
  • 2,331
  • 1
  • 25
  • 26
14

Dockerfile comments start with #, just like Python. kstaken has good examples:

# Install a more-up-to date version of MongoDB than what is included in the default Ubuntu repositories.

FROM ubuntu
MAINTAINER Kimbro Staken

RUN apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
RUN echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" | tee -a /etc/apt/sources.list.d/10gen.list
RUN apt-get update
RUN apt-get -y install apt-utils
RUN apt-get -y install mongodb-10gen

#RUN echo "" >> /etc/mongodb.conf

CMD ["/usr/bin/mongod", "--config", "/etc/mongodb.conf"] 
Cadoiz
  • 1,446
  • 21
  • 31
DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
6

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.

example code:

# this line is a comment

RUN echo 'we are running some # of cool things'

Output:

we are running some # of cool things
Cadoiz
  • 1,446
  • 21
  • 31
Amin Shojaei
  • 5,451
  • 2
  • 38
  • 46
3

Format

Here is the format of the Dockerfile:

We can use # for commenting purpose, as for example #COMMENT

#FROM microsoft/aspnetcore
FROM microsoft/dotnet
COPY /publish /app
WORKDIR /app
ENTRYPOINT ["dotnet", "WebApp.dll"]

From the above file when we build the docker, it skips the first line and goes to the next line because we have commented it using #

Cadoiz
  • 1,446
  • 21
  • 31
Prateek Naik
  • 2,522
  • 4
  • 18
  • 38
2
# this is comment
this isn't comment

is the way to do it. You can place it anywhere in the line and anything that comes later will be ignored

Amin Shojaei
  • 5,451
  • 2
  • 38
  • 46
Ankur Kothari
  • 822
  • 9
  • 11