【问题标题】:DL4006 warning: Set the SHELL option -o pipefail before RUN with a pipe in itDL4006 警告:在 RUN 之前设置 SHELL 选项 -o pipefail,其中包含管道
【发布时间】:2021-10-23 00:00:29
【问题描述】:

我有一个 Dockerfile

FROM strimzi/kafka:0.20.1-kafka-2.6.0

USER root:root
RUN mkdir -p /opt/kafka/plugins/debezium
# Download, unpack, and place the debezium-connector-postgres folder into the /opt/kafka/plugins/debezium directory
RUN curl -s https://repo1.maven.org/maven2/io/debezium/debezium-connector-postgres/1.7.0.Final/debezium-connector-postgres-1.7.0.Final-plugin.tar.gz | tar xvz --transform 's/debezium-connector-postgres/debezium/' --directory /opt/kafka/plugins/
USER 1001

当我在上面使用hadolint

hadolint Dockerfile

我收到警告

Dockerfile:6 DL4006 警告:在 RUN 之前设置 SHELL 选项 -o pipefail,其中包含管道。如果您在 alpine 映像中使用 /bin/sh,或者如果您的 shell 符号链接到 busybox,那么请考虑将您的 SHELL 显式设置为 /bin/ash,或者禁用此检查

我知道我在以RUN 开头的行中有一个管道|

但是,根据这个警告,我仍然真的不知道如何解决。

【问题讨论】:

    标签: dockerfile lint hadolint


    【解决方案1】:

    哦,刚刚在https://github.com/hadolint/hadolint/wiki/DL4006 的 wiki 页面中找到了解决方案

    这是我的固定版本:

    FROM strimzi/kafka:0.20.1-kafka-2.6.0
    
    USER root:root
    RUN mkdir -p /opt/kafka/plugins/debezium
    # Download, unpack, and place the debezium-connector-postgres folder into the /opt/kafka/plugins/debezium directory
    SHELL ["/bin/bash", "-o", "pipefail", "-c"]
    RUN curl -s https://repo1.maven.org/maven2/io/debezium/debezium-connector-postgres/1.7.0.Final/debezium-connector-postgres-1.7.0.Final-plugin.tar.gz | tar xvz --transform 's/debezium-connector-postgres/debezium/' --directory /opt/kafka/plugins/
    USER 1001
    

    添加SHELL ["/bin/bash", "-o", "pipefail", "-c"]的原因是https://github.com/docker/docker.github.io/blob/master/develop/develop-images/dockerfile_best-practices.md#using-pipes

    以下是副本:


    一些RUN 命令依赖于使用管道字符 (|) 将一个命令的输出通过管道传输到另一个命令的能力,如下例所示:

    RUN wget -O - https://some.site | wc -l > /number
    

    Docker 使用 /bin/sh -c 解释器执行这些命令,它只 评估管道中最后一个操作的退出代码以确定成功。 在上面的示例中,此构建步骤成功并长时间生成了一个新图像 因为wc -l 命令成功,即使wget 命令失败。

    如果您希望命令因管道中任何阶段的错误而失败, 预先添加 set -o pipefail && 以确保意外错误防止 从不经意的成功中构建。例如:

    RUN set -o pipefail && wget -O - https://some.site | wc -l > /number
    

    并非所有 shell 都支持 -o pipefail 选项。

    dash shell 等情况下 基于 Debian 的映像,考虑使用 RUNexec 形式明确 选择一个支持pipefail 选项的shell。例如:

    RUN ["/bin/bash", "-c", "set -o pipefail && wget -O - https://some.site | wc -l > /number"]
    

    【讨论】:

      猜你喜欢
      • 2019-06-01
      • 1970-01-01
      • 2020-11-09
      • 2014-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      • 2015-06-20
      相关资源
      最近更新 更多