首页 > 解决方案 > 编译 dockerfile 时如何修复“非法选项”错误?

问题描述

我在dockerfile中有一个应用程序,需要调用一个.sh文件来下载包,但是调用这个文件时总是出错。

我的 dockerfile 是:

FROM alpine:3.12 as builder

ARG VERSION=7.16.0
ARG DISTRO=tomcat
ARG SNAPSHOT=true

ARG JMX_PROMETHEUS_VERSION=0.12.0

RUN apk add --no-cache \
        bash \
        ca-certificates \
        maven \
        tar \
        wget \
        xmlstarlet

COPY settings.xml download.sh camunda-run.sh camunda-tomcat.sh camunda-wildfly.sh  /tmp/

RUN /tmp/download.sh

FROM alpine:3.12

RUN apk add --no-cache \
        bash \
        ca-certificates \
        curl \
        openjdk11-jre-headless \
        tzdata \
        tini \
        xmlstarlet \
    && curl -o /usr/local/bin/wait-for-it.sh \
      "https://raw.githubusercontent.com/vishnubob/wait-for-it/a454892f3c2ebbc22bd15e446415b8fcb7c1cfa4/wait-for-it.sh" \
    && chmod +x /usr/local/bin/wait-for-it.sh

RUN addgroup -g 1000 -S camunda && \
    adduser -u 1000 -S camunda -G camunda -h /camunda -s /bin/bash -D camunda
WORKDIR /camunda
USER camunda

ENTRYPOINT ["/sbin/tini", "--"]
CMD ["./camunda.sh"]

COPY --chown=camunda:camunda --from=builder /camunda .

我的 download.sh 文件看起来完全像这样:https ://github.com/camunda/docker-camunda-bpm-platform/blob/next/download.sh

运行命令时:docker build . -t servicecamundadocker/latest我收到错误:

 => ERROR [builder 4/4] RUN /tmp/download.sh                                                                                       0.2s ------
 > [builder 4/4] RUN /tmp/download.sh:
#11 0.216 /bin/sh: illegal option -
------

在此处输入图像描述

有谁知道如何解决这个错误?谢谢

标签: dockerdockerfiledocker-build

解决方案


经过大量研究,我找到了解决方案。

问题是 Windows 使用\r\n作为行尾,而 unix 只使用\n。因此,在我的download.sh文件中,有一个^M字符导致错误/bin/sh: 非法选项

解决方案是从 downlod.sh 文件中复制代码并将其转换为LF

  • 在Windows机器上使用Notepad++等文本编辑器进行转换
    • 转到菜单编辑-> EOL 转换-> Unix (LF)

完成后,docker build 可以正常工作。

谢谢大家的支持。


推荐阅读