如何在Dockerfile中发表评论?

本文详细介绍了在Dockerfile中使用注释的方法,包括如何使用#符号进行单行注释,注释的正确位置,以及注释与解析器指令的区别。文章还提到了在多行命令中使用注释的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自: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/#format 来自: 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时从上面的文件中,它跳过第一行并转到下一行,因为我们使用#评论它

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值