【问题标题】:Docker /bin/sh not found with binary go fileDocker /bin/sh 找不到二进制 go 文件
【发布时间】:2020-04-01 16:04:28
【问题描述】:

服务了许多页面和论坛,但没有找到解决方案。我有一个带有可执行 .sh 文件的简单 docker 容器。当我从 Windows 构建和运行它时 - 一切都很好。现在尝试使用 Docker 版本 19.03.5 从 Ubuntu 18.04 构建它。

对于重建使用我以bash install.sh 执行的 .sh 脚本

go build -o main.sh ./main

docker stop stats
docker container rm stats
docker image rm stats
docker build -t stats .
docker run --name stats -p 8080:8080 stats

我的 Dockerfile 是:

FROM alpine:3.10.1

ARG appPath="app"

RUN mkdir /app/
COPY main.sh /app/main.sh
RUN chmod +x /app/main.sh
COPY resources  /app/resources

RUN apk add --no-cache bash
WORKDIR /app

EXPOSE 8080
CMD /bin/bash -c 'ls' && /app/main.sh dev

输出的“安装”调用如下:

bash install.sh
stats
stats
Untagged: stats:latest
Sending build context to Docker daemon  38.98MB
Step 1/10 : FROM alpine:3.10.1
 ---> b7b28af77ffe
Step 2/10 : ARG appPath="app"
 ---> Using cache
 ---> 08baa7336701
Step 3/10 : RUN mkdir /app/
 ---> Using cache
 ---> fb9870e78322
Step 4/10 : COPY main.sh /app/main.sh
 ---> Using cache
 ---> 79bf713855e3
Step 5/10 : RUN chmod +x /app/main.sh
 ---> Using cache
 ---> 88bf70f9c6ec
Step 6/10 : COPY resources  /app/resources
 ---> Using cache
 ---> 2ebf95627a9e
Step 7/10 : RUN apk add --no-cache bash
 ---> Using cache
 ---> 39cd823e7f2f
Step 8/10 : WORKDIR /app
 ---> Using cache
 ---> 37e6fcea2d65
Step 9/10 : EXPOSE 8080
 ---> Using cache
 ---> 4250094c65f6
Step 10/10 : CMD /bin/bash -c 'ls' && /app/main.sh dev
 ---> Using cache
 ---> ed41a1efb15b
Successfully built ed41a1efb15b
Successfully tagged stats:latest
main.sh
resources
/bin/sh: /app/main.sh: not found

我不明白出了什么问题。 main.sh 在那里。如果我尝试执行CMD /bin/bash -c 'ls' && bash main.sh dev,则接收main.sh: main.sh: cannot execute binary file

那里出了什么问题,我该如何解决?

UPD:

重命名的文件是二进制文件,最后不应该有 .sh。

然后又试了一次:

install.sh

#!/bin/bash
go build -o myapp ./main

docker stop stats
docker container rm stats
docker image rm stats
docker build -t stats .
docker run --name stats -p 8080:8080 stats

Dockerfile

FROM alpine:3.10.1

RUN mkdir /app/
COPY myapp /app/myapp
RUN chmod +x /app/myapp
COPY resources  /app/resources

RUN apk add --no-cache bash
WORKDIR /app

EXPOSE 8080
CMD /bin/bash -c 'ls' && ./myapp dev

输出如下:

....    
Step 9/9 : CMD /bin/bash -c 'ls' && ./myapp dev
 ---> Running in 52fc24d26747
Removing intermediate container 52fc24d26747
 ---> 8d4f415e6dcd
Successfully built 8d4f415e6dcd
Successfully tagged stats:latest
myapp
resources
/bin/sh: ./myapp: not found

试过/app/myapp./myapp,还是不行。

【问题讨论】:

  • app/main.sh 的第一行(shebang / bang#!…)是什么样子的?
  • 它是二进制文件,而不是文本 bash 脚本。

标签: bash docker go


【解决方案1】:

我猜测错误消息具有误导性,真正的问题是 Docker 映像中缺少某些库或其他 Go 运行时组件。尝试静态构建您的应用程序(这意味着它需要的所有东西都将捆绑到二进制文件中)。

CGO_ENABLED=0 go build -o myapp ./main

(是的,如果它不是一个 shell 脚本,就不要称它为 main.sh。Unix 不在乎,但你把你的人类读者搞糊涂了。实际上,不要这样称呼它如果是。您无需知道grep 是二进制文件、shell 脚本还是 Python 程序;同样的约定对您自己的工具也有意义。)

【讨论】:

    猜你喜欢
    • 2021-07-24
    • 2019-06-18
    • 2018-04-26
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 2022-01-14
    • 2017-10-20
    相关资源
    最近更新 更多