二进制文件是使用“ CGO_ENABLED = 0”进行编译的,go-sqlite3需要cgo才能工作

我想在 Golang 项目中使用 sqlite3,但是在 docker 容器中运行它有一些错误。Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub

这是我的构建脚本:

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

我不能在 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

如何在 docker 容器中使用 sqlite3,或者如何构建 CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o main main.go。

dongyuan7110
dongyuan7110 为什么不在Dockerfile中构建它呢?
11 个月之前 回复
douyong7199
douyong7199 参见x-cellent.com/blog/cgo-bindings
11 个月之前 回复

1个回答

@LinPy Thank you for u help.

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.

Images 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
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐