You can see a full example of using batect with Golang in the Golang sample project.
The Golang bundle provides a container with all of these options pre-configured.
containers: build-env: image: golang:1.14.0-stretch volumes: - local: . container: /code options: cached - type: cache name: go-cache container: /go working_directory: /code environment: # With the image above, GOPATH defaults to /go, so we don't need to set it explicitly. GOCACHE: /go/cache
Mount a cache into your container for your
GOCACHE, otherwise you'll have to download and compile your dependencies every
time the build runs
Golang caches the source for dependencies under your
GOPATH. By default, this is at
However, because batect destroys all of your containers once the task finishes, this directory is lost at the end of every task run - which means that Golang
will have to download all of your dependencies again next time you run the task, significantly slowing down the build.
The solution to this is to mount a cache that persists between builds into your container for your
For example, the official Golang Docker images set
/go, so mounting a cache at
/go inside the container will
allow your dependencies to be persisted across builds.
The example configuration above demonstrates how to do this.
The Golang compiler caches intermediate build output (such as built libraries) in
Just like for
GOPATH, the contents of
GOCACHE will be lost when the task finishes and the container is removed, which means that Golang will have to recompile
all code for your project, even if it has not changed. This can also significantly slow down the build.
Again, the solution is to mount a cache that persists between builds into your container for your
The official Golang Docker images do not set a default for
GOCACHE, so you will need to set this yourself.
In the example above,
GOCACHE has been placed inside
/go (which is the default
GOPATH) so that both use the same cache.
Mount a cache into your container at
~/.cache/golangci-lint/, otherwise you'll experience slow performance from
golangci-lint caches build artifacts used during analysis to speed up subsequent runs. By default, it stores this cache at
Without a mounted cache, this directory would be lost every time batect starts a new container, causing
golangci-lint to need to rebuild your application
every time you run it.
The solution to this is to mount a cache that persists between builds into your container for
For example, assuming the home directory for your container's user is
containers: build-env: volumes: - type: cache name: golangci-cache container: /home/container-user/.cache/golangci-lint/