douzi3756
2019-09-13 10:19
采纳率: 100%
浏览 2.2k

二进制文件是使用“ 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。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doumei1772 2019-09-15 05:35
    已采纳

    @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
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题