简体   繁体   English

CGO_Enabled=1 需要在 Alpine Docker 容器中使用 SQLite 构建 Go 二进制文件

[英]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\binC:\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 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.

相关问题 二进制是用 'CGO_ENABLED=0' 编译的,go-sqlite3 需要 cgo 才能工作。 这是一个存根 - Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub 使用 go build CGO_ENABLED 交叉编译 - 警告:未找到 libudev.so.1 - Cross-compile using go build CGO_ENABLED - warning: libudev.so.1 not found 使用支持 go-sqlite3 CGO 的包构建非常​​慢的 docker - Very slow docker build with go-sqlite3 CGO enabled package 在 Alpine Docker 容器中从源代码编译 Go 时出错:“loadinternal:找不到运行时/cgo” - Error compiling Go from source in an Alpine Docker container: "loadinternal: cannot find runtime/cgo" 构建从 python 调用 CGO 的 Docker 容器 - Building Docker container that calls CGO from python 在高山linux容器内构建docker映像 - Building docker image, inside an alpine linux container 在Alpine Linux Docker容器中构建GNATCOLL - Building GNATCOLL in an Alpine Linux Docker Container Go 编译的二进制文件不会在 Ubuntu 主机上的 alpine docker 容器中运行 - Go-compiled binary won't run in an alpine docker container on Ubuntu host 在 Alpine Linux Docker 的路径中找不到已安装的 Go 二进制文件 - Installed Go binary not found in path on Alpine Linux Docker 是否可以通过docker golang:alpine image构建静态的sqlite Go应用程序? - Is it possible to build a static sqlite Go app by docker golang:alpine image?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM