【问题标题】:/bin/bash: invalid optionash: -/bin/bash: 无效选项ash: -
【发布时间】:2022-02-08 17:27:10
【问题描述】:

我是 Docker 新手,如果这是一个菜鸟问题,我很抱歉 :(

一位朋友与我分享了他的项目,我正在尝试创建容器,但出现错误。

这是我的输入:

docker build -t project-py -f project-py/prod/Dockerfile .

这是错误:

 => ERROR [19/27] RUN build.sh                                                                                     0.2s
------
 > [19/27] RUN build.sh:
: invalid optionash: -
------
failed to solve with frontend dockerfile.v0: failed to build LLB: executor failed running [/bin/sh -c build.sh]: runc did not terminate sucessfully

到目前为止我的理解是他不理解我的 build.sh 中的这一行:#!/bin/bash -e

我做了一些测试:

  • 将 build.sh 文件留空 = 有效
  • 去掉“-e”=给错误解释器错误

我的朋友没有错误...

这是我的Dockerfile

# based on https://github.com/dockerfiles/django-uwsgi-nginx
FROM amazonlinux

ENV HOME /home/appuser
ENV AWS_DEFAULT_REGION us-east-1

# Install required packages and remove the cache when done
RUN yum update -y && yum install -y \
    amazon-linux-extras awscli git \
    make glibc-devel gcc patch mysql-devel \
    enchant pyOpenSSL python3 python3-devel python3-pip python3-setuptools python-imaging \
    which vim git \
    && yum clean all

RUN cd /tmp && \
    curl -O https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm && \
    rpm -U ./amazon-cloudwatch-agent.rpm && \
    rm amazon-cloudwatch-agent.rpm

RUN amazon-linux-extras enable nginx1 && yum install -y nginx && yum clean all

#breaking this up to benefit from docker caching...
RUN pip3 install --upgrade pip supervisor uwsgitop \
  && useradd -m appuser \
    && chown -R appuser:appuser /home/appuser \
    && mkdir /var/log/project

# this is our log config
COPY project-py/prod/amazon-cloudwatch-agent/amazon-cloudwatch-agent.json /opt/aws/amazon-cloudwatch-agent/etc/config.json
# this is a hacked version of the ctl script that doesn't use systemd/initd to start it
COPY project-py/prod/amazon-cloudwatch-agent/amazon-cloudwatch-agent-ctl /opt/aws/amazon-cloudwatch-agent/bin/

USER appuser

# COPY requirements.txt and RUN pip install BEFORE adding the rest of your code, this will cause Docker's caching mechanism
# to prevent re-installing all your dependencies when you made a change a line or two in your app.
COPY --chown=appuser:appuser project-py/requirements/production.txt $HOME/project-py/requirements.txt

RUN python3 -m venv $HOME/venv && /bin/bash -c "source $HOME/venv/bin/activate; pip3 install -r $HOME/project-py/requirements.txt"

# add our stuff. break it up so that we can benefit from docker caching
COPY --chown=appuser:appuser project-docs $HOME/project-docs
COPY --chown=appuser:appuser project-static $HOME/project-static
COPY --chown=appuser:appuser project-www $HOME/project-www
COPY --chown=appuser:appuser project-py/assets/fonts $HOME/project-py/assets/fonts
COPY --chown=appuser:appuser project-py/*.py $HOME/project-py/
COPY --chown=appuser:appuser project-py/project $HOME/project-py/project
COPY --chown=appuser:appuser project-py/templates $HOME/project-py/templates
COPY --chown=appuser:appuser project-py/tools $HOME/project-py/tools
COPY project-py/prod/build.sh /usr/local/bin/
RUN build.sh

这里是build.sh

#!/bin/bash -e

source $HOME/venv/bin/activate
echo "compiling translations..."
pybabel compile --use-fuzzy -d $HOME/project-py/hourglass/translations

我正在使用这个版本的 docker 在 Surface Pro 7 上开发 W10 build 2004:

Client: Docker Engine - Community
 Cloud integration: 1.0.2
 Version:           19.03.13
 API version:       1.40
 Go version:        go1.13.15
 Git commit:        4484c46d9d
 Built:             Wed Sep 16 17:00:27 2020
 OS/Arch:           windows/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.13
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       4484c46d9d
  Built:            Wed Sep 16 17:07:04 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.3.7
  GitCommit:        8fba4e9a7d01810a393d5d25a3621dc101981175
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

非常感谢您的帮助

【问题讨论】:

  • 您是否使用 Linux 换行符保存文件? /bin/bash 是否存在于您的基本映像中?
  • 你的 Dockerfile 的基础镜像 (FROM ...) 是什么?你能edit你的帖子添加你的Dockerfile的minimal reproducible example和重现错误的部分sh脚本吗?
  • 我的文件使用 Linux 换行符(CR LF)保存我编辑我的帖子以添加 Dockerfile 和 build.sh 谢谢!
  • 检查您的脚本文件中是否有隐藏符号,例如 ^@ 或 ^M。您可以为此使用vicat -v filename,如this answer中所建议的那样。 stackoverflow.com/a/17136853/4486909
  • Linux 格式只有 LF,没有 CR; CR+LF 格式是 DOS/Windows 样式,will cause problems in the script (并且您的乱码错误消息表明这是一个回车,它正在抱怨)。将您的脚本(以及任何其他可能存在相同问题的文件)转换为正确的 Linux 文本格式(仅 LF),然后重试。

标签: bash docker


【解决方案1】:

文件似乎使用 Windows 换行符 (cr+lf) 保存。使用 Linux 换行符 (lf) 保存脚本,因为 Linux 会将回车符解释为命令行的一部分。

【讨论】:

    【解决方案2】:

    在 Dockfile 中,在 RUN ./setup.sh 之前添加这一行

    RUN cat setup.sh | sed '1 s/\r$//' > setup.sh
    
    

    基本上,它会从第一行删除 \r。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-19
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-15
      相关资源
      最近更新 更多