[英]CGO_Enabled=1 required building Go binary using SQLite in Alpine Docker container
I am trying to compile an Alpine Go container which uses GORM and it's SQLite driver for an in-memory database.我正在尝试编译一个使用GORM的 Alpine Go 容器,它是用于内存数据库的SQLite 驱动程序。 This depends on CGO being enabled.
这取决于是否启用了 CGO。 My binary builds and executes fine using
go build.
我的二进制文件使用
go build.
, but when running my docker image ( docker build.
followed by docker run $imagename
) I get the error message: ,但是在运行我的 docker 图像(
docker build.
然后是docker run $imagename
)时,我收到错误消息:
standard_init_linux.go:219: exec user process caused: no such file or directory
I am building on Windows 10 64 bit.我正在构建 Windows 10 64 位。 I have gcc installed.
我安装了 gcc。 Both
C:\TDM-GCC-64\bin
and C:\cygwin64\bin
are in my $env:path. C:\TDM-GCC-64\bin
和C:\cygwin64\bin
都在我的 $env:path 中。 I have changed the line endings to Linux style (LF) for all files in the package.对于 package 中的所有文件,我已将行结尾更改为 Linux 样式 (LF)。
My docker file is as follows:我的 docker 文件如下:
FROM golang:1.16.4-alpine AS builder
RUN apk update \
&& apk add --no-cache git \
&& apk add --no-cache ca-certificates \
&& apk add --update gcc musl-dev \
&& update-ca-certificates
# Create a user so that the image doens't run as root
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "100001" \
"appuser"
# Set the working directory inside the container.
WORKDIR $GOPATH/src/app
# Copy all files from the current directory to the working directory
COPY . .
# Fetch dependencies.
RUN go get -u -d -v
# Go build the binary, specifying the final OS and architecture we're looking for
RUN GOOS=linux CGO_ENABLED=1 GOARCH=amd64 go build -ldflags="-w -s" -o /go/bin/app -tags timetzdata
FROM scratch
# Import the user and group files from the builder.
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
# Copy our static executable.
COPY --from=builder /go/bin/app/go/bin/app
# Use the user that we've just created, one that isn't root
USER appuser:appuser
ENTRYPOINT ["/go/bin/app"]
Can you please help me understand why my docker image won't run?您能帮我理解为什么我的 docker 映像无法运行吗?
In case it's of any value, the line of Go code to open the DB is like so.如果它是任何值,打开DB的Go代码行是这样的。 As this works using Go build locally or using
go run.
因为这可以使用 Go 在本地构建或使用
go run.
I don't think this is relevant however.但是,我认为这无关紧要。
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
I think it's a simple file format issue.我认为这是一个简单的文件格式问题。 You can fix this according to answers below.
您可以根据以下答案解决此问题。
How to fix “exec user process caused „no such file or directory“” in Docker 如何修复 Docker 中的“exec 用户进程导致“没有这样的文件或目录””
exec user process caused “no such file or directory” - Docker exec 用户进程导致“没有这样的文件或目录” - Docker
I had a same problem and I found solution in @Kashif Khan link.我遇到了同样的问题,我在@Kashif Khan 链接中找到了解决方案。 There is not much voted ansver by @Nailuj29.
@Nailuj29 没有多少投票答案。
Libc not exist in scratch. Libc 不存在于从头开始。 I changed it to alpine and it solved this error.
我将其更改为 alpine 并解决了此错误。
Solution was to install both gcc and alpine-sdk packages using apk in the Dockerfile.解决方案是使用 Dockerfile 中的 apk 安装 gcc 和 alpine-sdk 软件包。 Only gcc was being installed in the question above.
在上述问题中仅安装了 gcc。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.