title: Very slow docker build with go-sqlite3 CGO enabled package
body: <p>Since I've installed <code>go-sqlite3</code> as dependency in my go project my docker build time started oscillating around 1 min.</p>
  <p>I tried to optimize the build by using <code>go mod download</code> to cache dependencies
  But it didn't reduce overall build time.</p>
  <p>Then I found out that</p>
  <blockquote>
  <p>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.</p>
  </blockquote>
  <p>So I run <code>go install github.com/mattn/go-sqlite3</code> as an extra step and it reduced build time to 17s~</p>
  <p>I also tried vendoring, but it didn't help with reducing the build time, installing library explicitly was always necessary to achieve that.</p>
  <pre><code>## 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
  </code></pre>
  <p>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?</p>
  <p>Do you see any obvious way of improving my current docker build?</p>