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

bfnvny8b  于 2023-10-14  发布在  Go
关注(0)|答案(2)|浏览(388)

我想在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 Golang项目

xxhby3vn

xxhby3vn1#

@LinPy谢谢你的帮助。
https://www.x-cellent.com/blog/cgo-bindings/
我解决了问题。但是构建需要很长时间,大约10分钟,我仍然在寻找更好的解决方案。
镜像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
6kkfgxo0

6kkfgxo02#

使用正确的映像构建

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 go build -ldflags="-w -s" -o server

相关问题