TL; DR
为什么我在本地构建Docker镜像时可以工作$ docker build -t broker .
,但是在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
我尝试更改ENTRYPOINT
以CMD
确保二进制文件作为外壳程序运行,但是它只是将错误更改为:
/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
[...]
...表示二进制文件已作为工件成功下载并复制到图像上。
我的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
,我当然会为本地Linux构建,该Linux无法在Alpine上运行。
解决方案是将go build
我的GitLab CI文件更改为CC=$(which musl-gcc) go build --ldflags '-w -linkmode external -extldflags "-static"'
,该文件可为Alpine正确构建。
musl-gcc
包含在中musl-tools
,因此要安装它,请运行apt-get -y musl-tools
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句