英文:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论