Golang builds in docker and requirements.txt equivalent

I am aware of Golang requirements.txt equivalent , but the context of the question is rather different.

I am trying to optimize builds of golang programs inside a docker container. My dockerfile looks something like this:

FROM golang:1.12.5 as builder

WORKDIR $GOPATH/src/test-ldap/

COPY main.go .

RUN go get -d -v ./...
...

while my main.go looks like

package main

import (
  "log"
  "fmt"
  "gopkg.in/ldap.v3"
)

func main() {
...

Of course, every time I make a change in the source code, the docker layer
COPY main.go . is changed, hence the go get command needs to be re-run and cannot be reused from the docker build cache even though the import block is unchanged.

Now of course I could type something like

RUN go get -d -v log fmt gopkg.in/ldap.v3

and place this before the COPY statement, but this violates the so-called single source of truth principle. I would then have to change the same thing in two different places in my codebase if I will ever wish to add extra imports.

How can I store my import requirements in a separate file? What is an idiomatic way to do this in go development?

1 answer

  • answered 2019-07-18 21:29 helmbert

    Use Go modules. Then, treat the resulting go.mod file just as you would a requirements.txt:

    FROM golang:1.12.5 as builder
    
    # NOT in $GOPATH (or explicitly set GO111MODULES=on)
    WORKDIR /usr/src/test-ldap/
    
    COPY go.mod .
    RUN go mod download  # alternatively: "go mod vendor" to build a vendor/ dir instead
    
    COPY main.go .
    # ...