如何在长docker RUN命令中添加注释?

Mit*_*ops 5 bash docker

我知道在docker文件中使用运行RUN命令以减少步骤/空间是惯例。但是,随着这些内容变长,我还想添加更多注释以使命令清晰明了。

FROM ubuntu:18.04
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update \   # I WANT A COMMENT on what this step is doing
 && apt-get install -y software-properties-common # comments also don't work here, before the slash \
Run Code Online (Sandbox Code Playgroud)

什么是docker / bash语法或docker约定,允许在单个步骤旁边添加注释?如果将注释放在上面指示的位置,则会出现错误

$ sudo docker build .
Sending build context to Docker daemon  4.608kB
Error response from daemon: Dockerfile parse error line 5: unknown instruction: &&
Run Code Online (Sandbox Code Playgroud)

从bash的角度来看这很有意义,但是我几乎没有其他选择来传达该行的意图。

Kam*_*Cuk 7

您只需要在一行中添加注释:

# comment 1
RUN apt-get update \
    # comment 2
    && apt-get install blabal blabla blabla \
    # comment 3
    && echo this is not a drill
Run Code Online (Sandbox Code Playgroud)

docker用换行符删除注释行。

有关示例,请参见docker-nginx


wis*_*cky 7

如果您希望注释与命令位于同一行,可以使用以下语法:

RUN apt-get update -y          `# comment1` \ 
&& apt-get install -y          `# comment2` \
    software-properties-common `# comment3` \
    curl                       `# comment4`
Run Code Online (Sandbox Code Playgroud)

或者

RUN apt-get update -y          $(: comment1) \ 
&& apt-get install -y          $(: comment2) \
    software-properties-common $(: comment3) \
    curl                       $(: comment4)
Run Code Online (Sandbox Code Playgroud)

  • 我也想出了这个(更糟糕:-)),但另一个答案更清晰。 (2认同)