Bash 如何在长docker运行命令中添加注释?

Bash 如何在长docker运行命令中添加注释?,bash,docker,Bash,Docker,我知道在docker文件中使用run-on-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-comm

我知道在docker文件中使用run-on-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的角度来看,这是有道理的,但我没有多少选择来传达这句话的意图。

你需要一句只有注释的话:

# comment 1
RUN apt-get update \
    # comment 2
    && apt-get install blabal blabla blabla \
    # comment 3
    && echo this is not a drill
docker删除带有换行符的注释行


请参见示例。

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

运行apt get update-y`#comment1`\
&&apt get install-y`#comment2`\
软件属性通用`#注释3`\
卷曲`#注释4`

运行apt get update-y$(:comment1)\
&&apt get安装-y$(:comment2)\
软件属性通用$(:comment3)\
curl$(:comment4)
我也想到了这个(更糟的是:-),但另一个答案要干净得多。