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

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

Multistage build exec go file not found in scratch

问题

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

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

Docker文件如下:

# syntax=docker/dockerfile:1

##
## 步骤1 - 构建
##

# 指定用于应用程序的基础镜像,可以是alpine或ubuntu
FROM golang:1.18-alpine AS build

# 非root用户
# RUN addgroup -S qcheckgroup && adduser -S qcheck -G qcheckgroup

# USER qcheck

# 在镜像内创建工作目录
ENV GO111MODULE=on
WORKDIR /app

# 将Go模块和依赖项复制到镜像中
COPY . .

# 下载Go模块和依赖项
# RUN go mod download

# 编译应用程序
RUN go build .


##
## 步骤2 - 部署
##
FROM scratch

WORKDIR /

FROM scratch

WORKDIR /

COPY --from=build app/query_check_span /

EXPOSE 8080

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:

# syntax=docker/dockerfile:1

##
## STEP 1 - BUILD
##

# specify the base image to  be used for the application, alpine or ubuntu
FROM golang:1.18-alpine AS build

# None root user
# RUN addgroup -S qcheckgroup && adduser -S qcheck -G qcheckgroup

# USER qcheck

# create a working directory inside the image
ENV GO111MODULE=on
WORKDIR /app

# copy Go modules and dependencies to image
COPY . .

# download Go modules and dependencies
# RUN go mod download

# compile application
RUN go build .


##
## STEP 2 - DEPLOY
##
FROM scratch

WORKDIR /

FROM scratch

WORKDIR /

COPY --from=build app/query_check_span /

EXPOSE 8080

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:

确定