如何在Dockerfile中發表評論?

2020-08-08 14:08:15

本文翻譯自:How do I make a comment in a Dockerfile?

I am writing a Dockerfile. 我正在寫一個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? Docker是否有一個註釋選項可以佔用剩下的一行並忽略它?


#1樓

參考:https://stackoom.com/question/2u24b/如何在Dockerfile中發表評論


#2樓

您可以使用#來評論一條線

# Everything on this line is a comment

#3樓

Dockerfile comments start with '#', just like Python. Dockerfile註釋以'#'開頭,就像Python一樣。 Here is a good example ( kstaken/dockerfile-examples ): 這是一個很好的例子( kstaken / dockerfile-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"] 

#4樓

Use the # syntax for comments 使用#語法進行註釋

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

# My comment here
RUN echo 'we are running some cool things'

#5樓

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. 還應該注意, 解析器指令最近已新增到Dockerfile中,其語法與註釋相同。 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: 最初,新增了此指令以更改跳脫字元以支援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. 第一行雖然看起來是註釋,但它是一個解析器指令,用於將跳脫字元更改爲反引號,以便COPYRUN命令可以使用路徑中的反斜槓。 A parser directive is also used with BuildKit to change the frontend parser with a syntax line. 解析器指令也與BuildKit一起使用,以使用syntax行更改前端解析器。 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

#6樓

Format 格式

Here is the format of the Dockerfile: 這是Dockerfile:的格式Dockerfile:

We can use # for commenting purpose #Comment 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 # 在我們構建docker時從上面的檔案中,它跳過第一行並轉到下一行,因爲我們使用#評論它