非常慢的docker构建与启用了CGO的go-sqlite3包。

huangapple go评论95阅读模式
英文:

Very slow docker build with go-sqlite3 CGO enabled package

问题

自从我在我的Go项目中安装了go-sqlite3作为依赖项后,我的Docker构建时间开始在1分钟左右波动。

我尝试通过使用go mod download来缓存依赖项来优化构建过程,但是并没有减少总体构建时间。

然后我发现:

> go-sqlite3是一个启用了CGO的包,你需要设置环境变量CGO_ENABLED=1,并且在你的路径中有一个gcc编译器。

所以我额外运行了go install github.com/mattn/go-sqlite3这一步,将构建时间减少到了大约17秒。

我还尝试过使用vendoring,但是它并没有帮助减少构建时间,始终需要显式安装库才能实现这一点。

## Build
FROM golang:1.16-buster AS build
WORKDIR /app
 
# Download dependencies
COPY go.mod .
COPY go.sum .
RUN go mod download

RUN go install github.com/mattn/go-sqlite3 //这将构建时间减少到大约17秒

COPY . .

RUN go build -o /myapp

但是不知何故,我对这个解决方案仍然不满意。
我不明白为什么添加这个包会导致我的构建时间如此长,并且为什么我需要显式安装它才能避免这样长的构建时间。
另外,将所有包下载后再安装是否更好?

你是否看到任何明显改进当前Docker构建的方法?

英文:

Since I've installed go-sqlite3 as dependency in my go project my docker build time started oscillating around 1 min.

I tried to optimize the build by using go mod download to cache dependencies
But it didn't reduce overall build time.

Then I found out that

> go-sqlite3 is a CGO enabled package you are required to
> set the environment variable CGO_ENABLED=1 and have a gcc compile
> present within your path.

So I run go install github.com/mattn/go-sqlite3 as an extra step and it reduced build time to 17s~

I also tried vendoring, but it didn't help with reducing the build time, installing library explicitly was always necessary to achieve that.

## Build
FROM golang:1.16-buster AS build
WORKDIR /app
 
# Download dependencies
COPY go.mod .
COPY go.sum .
RUN go mod download

RUN go install github.com/mattn/go-sqlite3 //this reduced build time to around 17s~

COPY . .

RUN go build -o /myapp

But somehow I am still not happy with this solution.
I don't get why adding this package makes my build so long and why I need to explicitly install it in order to avoid such long build times.
Also, wouldn't it be better to install all packages after downloading them?

Do you see any obvious way of improving my current docker build?

答案1

得分: 1

事实是,基于C的SQLite包构建时间很长。我目前正在使用它,每次构建都很痛苦。我对它也不满意,并一直在寻找替代方案。我一直忙于其他项目,但我找到了这个包QL [1],你可以在不使用C的情况下构建它 [2]:

go build -tags purego

或者如果你只需要只读功能,可以尝试SQLittle [3]。

  1. https://pkg.go.dev/modernc.org/ql
  2. https://pkg.go.dev/modernc.org/ql#hdr-Building_non_CGO_QL
  3. https://github.com/alicebob/sqlittle
英文:

The fact of the matter is that the C-based SQLite package just takes a long time to build. I use it myself currently, and yes it's painful every time. I have also been unhappy with it, and have been looking for alternatives. I have been busy with other projects, but I did find this package QL [1], which you can build without C [2]:

go build -tags purego

or if you just need read only, you can try SQLittle [3].

  1. <https://pkg.go.dev/modernc.org/ql>
  2. <https://pkg.go.dev/modernc.org/ql#hdr-Building_non_CGO_QL>
  3. https://github.com/alicebob/sqlittle

huangapple
  • 本文由 发表于 2021年7月20日 22:36:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/68456778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定