【发布时间】:2016-08-11 03:55:49
【问题描述】:
我正在编写一个 Dockerfile。有没有办法在这个文件中制作 cmets?
Docker 是否有一个注释选项,它会占用一行的其余部分并忽略它?
【问题讨论】:
-
我发现如果一个层被缓存了,注释掉它没有效果:如果已经缓存了,Docker 会继续构建注释掉的层。
标签: docker dockerfile
我正在编写一个 Dockerfile。有没有办法在这个文件中制作 cmets?
Docker 是否有一个注释选项,它会占用一行的其余部分并忽略它?
【问题讨论】:
标签: docker dockerfile
您可以在行首使用# 来开始comment(# 之前的空格是允许的):
# do some stuff
RUN apt-get update \
# install some packages
&& apt-get install -y cron
字符串中间的#'s 被传递给命令本身,例如:
RUN echo 'we are running some # of cool things'
【讨论】:
Dockerfile cmets 以 # 开头,就像 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"]
【讨论】:
MAINTAINER 已弃用,现在建议使用标签:LABEL maintainer="foo@abc.com"
对 cmets 使用 # 语法
发件人:https://docs.docker.com/engine/reference/builder/#format
# My comment here
RUN echo 'we are running some cool things'
【讨论】:
ADD . $foo # ADD . /bar
正如其他人所提到的,cmets 用# 引用,并且是documented here。但是,与某些语言不同,# 必须位于行首。如果它们出现在该行的中途,它们将被解释为一个参数,并可能导致意外的行为。
# 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
还应注意parser directives 最近已添加到 Dockerfile 中,其语法与注释相同。它们需要出现在文件顶部,在任何其他 cmets 或命令之前。最初,添加此指令是为了更改转义字符以支持 Windows:
# escape=`
FROM microsoft/nanoserver
COPY testfile.txt c:\
RUN dir c:\
第一行虽然看起来是注释,但它是一个解析器指令,用于将转义字符更改为反引号,以便COPY 和RUN 命令可以在路径中使用反斜杠。解析器指令也与BuildKit 一起使用,以使用syntax 行更改前端解析器。请参阅experimental syntax,了解有关如何在实践中使用它的更多详细信息。
使用多行命令,注释的行将被忽略,但您需要单独注释掉每一行:
$ 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
【讨论】:
# 开头还是只需要第一行?一项实验表明它是前者。可以更新此答案以涵盖该问题(使其更加出色)。
这是Dockerfile:的格式
我们可以使用#进行评论,例如#COMMENT
#FROM microsoft/aspnetcore
FROM microsoft/dotnet
COPY /publish /app
WORKDIR /app
ENTRYPOINT ["dotnet", "WebApp.dll"]
从上面的文件中,当我们构建 docker 时,它会跳过第一行并转到下一行,因为我们使用 # 对其进行了注释
【讨论】:
Docker 将 以
#开头的行视为注释,除非 line 是一个有效的解析器指令。#标记在一行中的任何其他位置 被视为参数。示例代码:
# this line is a comment RUN echo 'we are running some # of cool things'输出:
we are running some # of cool things
【讨论】:
# this is comment
this isn't comment
是这样做的方法。您可以将它放在行中的任何位置,后面的任何内容都将被忽略
【讨论】: