Gitlab Runner错误exec: “sh”: 在$PATH中找不到可执行文件

huangapple go评论70阅读模式
英文:

Gitlab Runner error exec: "sh": executable file not found in $PATH

问题

当在Gitlab CI上运行容器时,我遇到了这个错误:

ERROR: Job failed (system failure): Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "sh": executable file not found in $PATH: unknown (exec.go:57:0s)

我的 .Dockerfile 文件如下:

FROM golang:1.16-alpine AS builder
ENV \
    OUTDIR='/out' \
    GO111MODULE='on'    

WORKDIR /app
COPY go.mod /app/
COPY go.sum /app/
RUN go mod download
COPY . /app/
RUN CGO_ENABLED=0 GOBIN=${OUTDIR}/usr/bin/ go install .

FROM scratch
COPY --from=builder /out/ /
ENTRYPOINT ["/usr/bin/app-cli"]

我的 .gitlab-ci.yml 文件如下:

stages:
  - validation

validation:
  image:
    name: gitlab.mycompany.net:4567/myteam/app-cli:latest
    entrypoint: [""]
  stage: validation
  rules:
    - if: '$CI_MERGE_REQUEST_IID'
  script:
    - ls

这个错误是与我的 Dockerfile 还是 Gitlab CI 相关的?

我可以在本地运行以下命令来解决这个问题:
docker run --rm -ti gitlab.mycompany.net:4567/myteam/app-cli:latest
但在 Gitlab Runner 上无法运行。

英文:

I have this error when running a container on Gitlab CI

ERROR: Job failed (system failure): Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "sh": executable file not found in $PATH: unknown (exec.go:57:0s)

My .Dockerfile

FROM golang:1.16-alpine AS builder
ENV \
	OUTDIR='/out' \
	GO111MODULE='on'    

WORKDIR /app
COPY go.mod /app/
COPY go.sum /app/
RUN go mod download
COPY . /app/
RUN CGO_ENABLED=0 GOBIN=${OUTDIR}/usr/bin/ go install .

FROM scratch
COPY --from=builder /out/ /
ENTRYPOINT ["/usr/bin/app-cli"]

My .gitlab-ci.yml

stages:
  - validation

validation:
  image:
    name: gitlab.mycompany.net:4567/myteam/app-cli:latest
    entrypoint: [""]
  stage: validation
  rules:
    - if: '$CI_MERGE_REQUEST_IID'
  script:
    - ls

Is this error related to my dockerfile or the gitlab ci ?

I can run this locally by
docker run --rm -ti gitlab.mycompany.net:4567/myteam/app-cli:latest
but not at gitlab runner

答案1

得分: 1

给出一个空的入口点和根本不给出入口点是不同的。

docker run命令中,你没有给出任何入口点。因此,Docker镜像将使用默认的入口点ENTRYPOINT ["/usr/bin/app-cli"]运行。

gitlab-ci中,你用一个空的入口点entrypoint: [""]覆盖了默认的入口点,导致出现找不到可执行文件的错误。

尝试这样做:

validation:
  image:
    name: gitlab.mycompany.net:4567/myteam/app-cli:latest
    entrypoint: ["/usr/bin/app-cli"]
英文:

Giving an empty entrypoint and not giving the entrypoint at all are not the same thing.

In the docker run command, you are not giving any entrypoint. Hence, the docker image runs with the default entrypoint ENTRYPOINT ["/usr/bin/app-cli"].

In the gitlab-ci, you are overwriting the default entrypoint with an empty one entrypoint: [""] where executable file not found.

Try this:

validation:
  image:
    name: gitlab.mycompany.net:4567/myteam/app-cli:latest
    entrypoint: ["/usr/bin/app-cli"]

答案2

得分: 0

除了已经指出的覆盖入口点之外,问题在于您正在调用需要 shell 的脚本 ls
尝试在脚本部分只调用您的应用程序 /usr/bin/app-cli

英文:

Besides overwriting the entrypoint as already pointed out, the issue is that you are calling ls as the script which requires a shell.
Try just calling your application in the script part /usr/bin/app-cli.

答案3

得分: 0

找到答案了

我只需要将
FROM scratch
改为
FROM alpine:latest
现在我可以使用 sh

英文:

found the answer

I just need to change the
FROM scratch to
FROM alpine:latest
so now I can use the sh

huangapple
  • 本文由 发表于 2021年8月9日 14:23:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/68707634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定