Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a comment in a Dockerfile?

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?

like image 505
kpie Avatar asked Apr 19 '16 06:04

kpie


People also ask

How do I comment multiple lines in Dockerfile?

CTRL + / works well in with VScode single or multi.

How do I tag in Docker?

Docker tags are just an alias for an image ID. The tag's name must be an ASCII character string and may include lowercase and uppercase letters, digits, underscores, periods, and dashes. In addition, the tag names must not begin with a period or a dash, and they can only contain 128 characters.


2 Answers

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' 
like image 185
Ranjeet Avatar answered Oct 02 '22 05:10

Ranjeet


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 
like image 23
BMitch Avatar answered Oct 02 '22 05:10

BMitch