Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speeding up Go builds with go 1.10 build cache in Docker containers

Tags:

docker

go

I have a Go project with a large vendor/ directory which almost never changes.

I am trying to use the new go 1.10 build cache feature to speed up my builds in Docker engine locally.

Avoiding recompilation of my vendor/ directory would be enough optimization. So I'm trying to do Go equivalent of this common Dockerfile pattern for Python:

FROM python
COPY requirements.txt .              # <-- copy your dependency list
RUN pip install -r requirements.txt  # <-- install dependencies
COPY ./src ...                       # <-- your actual code (everything above is cached)

Similarly I tried:

FROM golang:1.10-alpine
COPY ./vendor ./src/myproject/vendor
RUN go build -v myproject/vendor/... # <-- pre-build & cache "vendor/"
COPY . ./src/myproject

However this is giving "cannot find package" error (likely because you cannot build stuff in vendor/ directly normally either).

Has anyone been able to figure this out?

like image 390
ahmet alp balkan Avatar asked May 25 '18 01:05

ahmet alp balkan


1 Answers

As of Go 1.11, you would use go modules to accomplish this;

FROM golang
WORKDIR /src/myproject
COPY go.mod go.sum ./             # <-- copy your dependency list
RUN go mod download               # <-- install dependencies
COPY . .                          # <-- your actual code (everything above is cached)

As long as go.sum doesn't change, the image layer created by go mod download shall be reused from cache.

like image 93
Ferdy Avatar answered Oct 09 '22 18:10

Ferdy