TL; DR
当我使用$ docker build -t broker .
在本地构建Docker镜像时,为什么我的Docker镜像可以工作,但是在GitLab CI中构建它时,却得到bash: line 1: /bin/broker: not found
?
背景/排除故障
我正在使用GitLab CI使用Helm for Kubernetes部署系统的新版本。
我只是将Dockerfiles更改为使用alpine:3.7
而不是golang:1.9.2
,因为它只是运行内置的二进制文件而不执行go代码,因此我的图像大小从约800MB减小到约24MB。
我现在遇到的问题是,在GitLab CI中构建镜像时,我从容器中收到以下错误:
bash: line 1: /bin/broker: not found
我尝试过changing
ENTRYPOINT
to CMD
以确保二进制文件作为 shell 程序运行,但是它只是将错误更改为:/bin/sh: /bin/broker: not found
如果我在本地构建镜像并运行它,那么一切都会按预期进行。另外,从GitLab CI日志中,我得到了:
$ go build
$ mv $SERVICE_NAME $CI_PROJECT_DIR
Uploading artifacts...
broker: found 1 matching files
Uploading artifacts to coordinator... ok id=1234
responseStatus=201 Created token=ABC123
这意味着我的二进制文件已成功构建,就像使用旧镜像一样。接下来,我得到这个:
[...]
Step 3/4 : COPY ./broker /bin
---> 50d04cbbc81c
Step 4/4 : CMD /bin/broker
---> Running in c48a73351599
[...]
...表示二进制文件已作为 Artifact 成功下载并复制到图像上。
我的Dockerfile看起来像这样:
# This file is intended for use with the GitLab CI tool where the binary has already been built.
FROM alpine:3.7
# Make sure we have some basic dev tools.
RUN \
apk --update add curl bash nano && \
rm -r /var/cache/apk/*
# The binary is built and downloaded to the current directory by GitLab CI.
COPY ./broker /bin
RUN chmod +x /bin/broker
# Run the program.
CMD /bin/broker
我使用完全限定的路径,因此
$PATH
var无关紧要。谢谢你的帮助。
最佳答案
在尝试使用更好的Google搜索后,我找到了解决方法。
当然,在运行go build
时,我是为不在Alpine上运行的本地Linux构建的。
解决方案是将我的GitLab CI文件中的go build
更改为 CC=$(which musl-gcc) go build --ldflags '-w -linkmode external -extldflags "-static"'
,该文件可为Alpine正确构建。
musl-gcc
is included in the musl-tools
,因此要安装它,请运行apt-get -y musl-tools
。
关于docker - Docker入口点 “not found”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49235033/