在scratch中找不到多阶段构建执行的go文件。

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

Multistage build exec go file not found in scratch

问题

我是你的中文翻译助手,以下是翻译好的内容:

我对Docker还很陌生,有一些初级问题。我按照这个教程改进Docker文件。单个构建可以正常工作,但是当我使用scratch镜像时,多阶段构建失败。

Docker文件如下:

  1. # syntax=docker/dockerfile:1
  2. ##
  3. ## 步骤1 - 构建
  4. ##
  5. # 指定用于应用程序的基础镜像,可以是alpine或ubuntu
  6. FROM golang:1.18-alpine AS build
  7. # 非root用户
  8. # RUN addgroup -S qcheckgroup && adduser -S qcheck -G qcheckgroup
  9. # USER qcheck
  10. # 在镜像内创建工作目录
  11. ENV GO111MODULE=on
  12. WORKDIR /app
  13. # 将Go模块和依赖项复制到镜像中
  14. COPY . .
  15. # 下载Go模块和依赖项
  16. # RUN go mod download
  17. # 编译应用程序
  18. RUN go build .
  19. ##
  20. ## 步骤2 - 部署
  21. ##
  22. FROM scratch
  23. WORKDIR /
  24. FROM scratch
  25. WORKDIR /
  26. COPY --from=build app/query_check_span /
  27. EXPOSE 8080
  28. ENTRYPOINT [ "/query_check_span" ]

构建过程正常工作 docker build -t qcheck:multi -f .\Dockerfile.multi .,但是当我尝试使用 docker run qcheck:multi 运行容器时,出现以下错误:exec /query_check_span: no such file or directory

看起来问题是scratch镜像无法执行编译后的Go程序。当我将镜像更改为例如alpine时,我确认可以正常工作。

你有什么想法我做错了什么吗?我如何使用scratch镜像运行容器?

英文:

I'm new to Docker and I have some noob questions. I've followed this tutorial to improve Docker files. The single build works fine, but the multistage fails when I use scratch image.

The Docker file is:

  1. # syntax=docker/dockerfile:1
  2. ##
  3. ## STEP 1 - BUILD
  4. ##
  5. # specify the base image to be used for the application, alpine or ubuntu
  6. FROM golang:1.18-alpine AS build
  7. # None root user
  8. # RUN addgroup -S qcheckgroup && adduser -S qcheck -G qcheckgroup
  9. # USER qcheck
  10. # create a working directory inside the image
  11. ENV GO111MODULE=on
  12. WORKDIR /app
  13. # copy Go modules and dependencies to image
  14. COPY . .
  15. # download Go modules and dependencies
  16. # RUN go mod download
  17. # compile application
  18. RUN go build .
  19. ##
  20. ## STEP 2 - DEPLOY
  21. ##
  22. FROM scratch
  23. WORKDIR /
  24. FROM scratch
  25. WORKDIR /
  26. COPY --from=build app/query_check_span /
  27. EXPOSE 8080
  28. ENTRYPOINT [ "/query_check_span" ]

The build process works fine docker build -t qcheck:multi -f .\Dockerfile.multi . but when I try to run the container with docker run qcheck:multi I get the following error: exec /query_check_span: no such file or directory.

It looks that the problem is that scratch image is unable to execute the compiled go program. I confirm that when I've change the image to apline for example.

Do you have an idea of what I am doing wrong? How could I run the container using scratch image?

答案1

得分: 1

在构建阶段,你应该使用RUN go build -o ./query_check_span而不是RUN go build .。这个命令会输出一个名为query_check_span的二进制文件。

英文:

In your build stage, you should RUN go build -o ./query_check_span instead of RUN go build .. That command will output the binary file which has the name query_check_span.

huangapple
  • 本文由 发表于 2022年6月23日 03:07:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/72720780.html
匿名

发表评论

匿名网友

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

确定