Very slow docker build with go-sqlite3 CGO enabled package

  docker, go, sqlite

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?

Source: Docker Questions

LEAVE A COMMENT