为了账号安全,请及时绑定邮箱和手机立即绑定

CGO_Enabled=1 需要在 Alpine Docker 容器中使用 SQLite 构建 Go

CGO_Enabled=1 需要在 Alpine Docker 容器中使用 SQLite 构建 Go

Go
HUH函数 2022-09-05 10:04:29
我正在尝试编译一个Alpine Go容器,该容器使用GORM,它是内存中数据库的SQLite驱动程序。这取决于是否启用了 CGO。我的二进制文件使用 构建和执行正常,但是当运行我的 docker 映像(后跟 )时,我收到错误消息:go build .docker build .docker run $imagenamestandard_init_linux.go:219: exec user process caused: no such file or directory我正在Windows 10 64位上构建。我已经安装了gcc。两者都在我的$env:路径中。我已将包中所有文件的行尾更改为Linux样式(LF)。C:\TDM-GCC-64\binC:\cygwin64\bin我的 docker 文件如下:FROM golang:1.16.4-alpine AS builderRUN 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 rootRUN 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 directoryCOPY . .# Fetch dependencies.RUN go get -u -d -v# Go build the binary, specifying the final OS and architecture we're looking forRUN GOOS=linux CGO_ENABLED=1 GOARCH=amd64 go build -ldflags="-w -s" -o /go/bin/app -tags timetzdataFROM scratch# Import the user and group files from the builder.COPY --from=builder /etc/passwd /etc/passwdCOPY --from=builder /etc/group /etc/groupCOPY --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 rootUSER appuser:appuserENTRYPOINT ["/go/bin/app"]你能帮我理解为什么我的 docker 镜像无法运行吗?如果它有任何价值,则打开数据库的Go代码行就是这样。由于这适用于使用本地构建或使用Go构建,因此我认为这无关紧要。go run .db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
查看完整描述

2 回答

?
PIPIONE

TA贡献1829条经验 获得超9个赞

Libc 不存在于零头。我把它改成了alpine,它解决了这个错误。


查看完整回答
反对 回复 2022-09-05
?
饮歌长啸

TA贡献1951条经验 获得超3个赞

解决方案是在Dockerfile中使用apk安装gcc和alpine-sdk软件包。在上面的问题中只安装了 gcc。


查看完整回答
反对 回复 2022-09-05
  • 2 回答
  • 0 关注
  • 385 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号