简体   繁体   English

二进制是用 'CGO_ENABLED=0' 编译的,go-sqlite3 需要 cgo 才能工作。 这是一个存根

[英]Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub

I want to use sqlite3 in Golang project.我想在 Golang 项目中使用 sqlite3。 But run it in docker container has some error.但是在 docker 容器中运行它有一些错误。 Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub

this is my build script这是我的构建脚本

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main main.go

I can't use CGO_ENABLED=1 in mac computer.我不能在 mac 计算机中使用 CGO_ENABLED=1 。

 FROM golang:1.13-alpine ENV WORK_DIR=/go ENV TIME_ZONE=Asia/Singapore RUN ln -snf /usr/share/zoneinfo/$TIME_ZONE /etc/localtime && echo $TIME_ZONE > /etc/timezone WORKDIR $WORK_DIR RUN mkdir -p logs COPY main. COPY config.conf. COPY basic.db./data COPY db db ENTRYPOINT./main -c config.conf

How can I use sqlite3 in the docker container.如何在 docker 容器中使用 sqlite3。 Or how can I build CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o main main.go Golang project或者我该如何构建CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o main main.go Golang 项目

@LinPy Thank you for u help. @LinPy 谢谢你的帮助。

https://www.x-cellent.com/blog/cgo-bindings/ https://www.x-cellent.com/blog/cgo-bindings/

I solved the problem.我解决了这个问题。 But build takes a long time, about 10 minutes, and I'm still looking for a better solution.但是构建需要很长时间,大约 10 分钟,我仍在寻找更好的解决方案。

Images Dockerfile: https://github.com/sillyhatxu/alpine-build图片 Dockerfile: https://github.com/sillyhatxu/alpine-build

 FROM xushikuan/alpine-build:2.0 AS builder ENV WORK_DIR=$GOPATH/src/github.com/sillyhatxu/mini-mq WORKDIR $WORK_DIR COPY.. RUN go build -a -ldflags "-linkmode external -extldflags '-static' -s -w" -o main main.go FROM xushikuan/alpine-build:1.0 ENV BUILDER_WORK_DIR=/go/src/github.com/sillyhatxu/mini-mq ENV WORK_DIR=/app ENV TIME_ZONE=Asia/Singapore WORKDIR $WORK_DIR RUN ln -snf /usr/share/zoneinfo/$TIME_ZONE /etc/localtime && echo $TIME_ZONE > /etc/timezone RUN mkdir -p logs RUN mkdir -p db RUN mkdir -p data COPY --from=builder $BUILDER_WORK_DIR/main $WORK_DIR COPY --from=builder $BUILDER_WORK_DIR/config.conf $WORK_DIR COPY --from=builder $BUILDER_WORK_DIR/db $WORK_DIR/db COPY --from=builder $BUILDER_WORK_DIR/basic.db $WORK_DIR/data ENTRYPOINT./main -c config.conf

Build with the correct image使用正确的图像构建

FROM golang:1.18-alpine AS BUILDER RUN apk add --no-cache gcc g++ git openssh-client RUN GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOPROXY=https://goproxy.cn,direct \ go build -ldflags="-w -s" -o server

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM