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

时间:2018-11-30 20:38:12

标签: 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 \

允许在单个步骤旁边添加注释的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: &&

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

2 个答案:

答案 0 :(得分:4)

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

# comment 1
RUN apt-get update \
    # comment 2
    && apt-get install blabal blabla blabla \
       # comment 3
    && echo this is not a drill

doker删除带有换行符的注释行。

参见docker-nginx的示例。

答案 1 :(得分:1)

如果要在命令的同一行中注释,可以使用以下语法:

RUN apt-get update -y          `# comment1` \ 
&& apt-get install -y          `# comment2` \
    software-properties-common `# comment3` \
    curl                       `# comment4`

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