[EDIT - added clarity]
Here is my current env setup :
$GOPATH = /home/fzd/go
projectDir = /home/fzd/go/src/github.com/fzd/amazingo
amazingo
has a go.mod
file that lists several (let's say thousands) dependencies.
So far, I used to go build -t bin/amazingo cmd/main.go
, but I want to share this with other people and have a build command that is environment-independent. Using go build
has the advantage of downloading each dependency once -- and then using those in ${GOPATH}/pkg/mod
, which saves time and bandwidth.
I want to build in a multistage docker image, so I go with
> cat /home/fzd/go/src/github.com/fzd/amazingo/Dockerfile
FROM golang:1.17 as builder
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /bin/amazingo cmd/main.go
FROM alpine:latest
COPY --from=builder /bin/amazingo /amazingo
ENTRYPOINT ["/amazingo"]
As you can expect it, the builder is "naked" when I start it, so it has to download all my dependencies when I docker build -t amazingo:0.0.1 .
. But it will do so everytime I call it, which can be several times a day.
Fortunately, I already have most of these dependencies on my disk. I would be happy to share these files (that are located in my $GOPATH/pkg/mod
) with the builder, and help it build faster on my machine.
So the question is: how can I share my ${GOPATH}
(or ${GOPATH}/mod/pkg
) with the builder
?
I tried adding the following to the builder
ARG SRC_GOPATH
COPY ${SRC_GOPATH} /go
and call docker build --build-arg SRC_GOPATH=${GOPATH} -o amazingo:0.0.1 .
, but it wasn't good enough - I got an error (COPY failed: file not found in build context or excluded by .dockerignore: stat home/fzd/go: file does not exist
)
I hope this update brings a bit more clarity to the problem.
=======
I have a project with a go.mod file. I want to build that project using a multistage docker image. (this article is a perfect example)
The issue is that I have "lots" of dependencies, and each of them will be downloaded inside my Docker builder stage.
Is there a way to "share" my GOPATH/pkg/mod with the docker build...
command (in some ways, having a local cache) ?