Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

docker

go

I want to use sqlite3 in Golang project. But run it in docker container has some error.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.

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. Or how can I build CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o main main.go Golang project

like image 981
Shikuan Xu Avatar asked Oct 16 '22 11:10

Shikuan Xu


1 Answers

@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
like image 104
Shikuan Xu Avatar answered Oct 20 '22 19:10

Shikuan Xu