首页 > 解决方案 > 如何在长 docker RUN 命令中添加注释?

问题描述

我知道习惯上在 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 的角度来看,这是有道理的,但让我没有多少选择来传达线路的意图。

标签: bashdocker

解决方案


您需要一行仅包含注释:

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

docker 使用换行符删除注释行。

请参阅docker-nginx示例。


推荐阅读