https://github.com/acj/depender
A tool for precompiling Go dependencies during Docker builds
https://github.com/acj/depender
Last synced: 10 months ago
JSON representation
A tool for precompiling Go dependencies during Docker builds
- Host: GitHub
- URL: https://github.com/acj/depender
- Owner: acj
- License: mit
- Created: 2019-11-09T23:34:25.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-04-05T19:27:54.000Z (about 6 years ago)
- Last Synced: 2025-02-23T04:42:40.828Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# depender
A tool for precompiling Go dependencies during Docker builds
Downloading and compiling the dependencies for your Go code can take a while. And if you're building container images from a Dockerfile, the problem is made worse by Docker's caching system: you need to copy your app code into the container in order to fetch and build its dependencies, but the cache gets invalidated every time you make the slightest change to your code. If an app depends on larger Go packages like [kubernetes/kubernetes](https://github.com/kubernetes/kubernetes) or [aws-sdk-go](https://github.com/aws/aws-sdk-go), the build might take several minutes. Every time.
depender solves this problem by generating a Go source file that contains only imports. This file can be compiled after copying `go.{mod,sum}` but _before_ copying app code so that the slow dependency compilation step remains cached.
## Quick start
```
$ go get github.com/acj/depender
$ depender -h
Usage of depender:
-exclude string
package paths (or sub-strings) to exclude
-output string
path to output source file (default "deps.go")
```
## Usage
First, use depender to generate a source file that includes the dependencies for your app's packages.
```
$ cd my-go-app
$ depender ./...
Dummy source file written to deps.go
```
Then run `go build` on the dummy source file near top of your Dockerfile to compile the dependencies:
```
FROM golang:...
# Precompile dependencies
COPY go.mod go.sum ./
COPY deps.go ./
RUN go build deps.go && rm deps.go
# Now, copy your app source
COPY . .
...
```
Because the dependencies are compiled before your app's source code is copied, they will remain cached until you update go.mod, go.sum, or deps.go.
The `deps.go` file should be checked into version control. It contains a `+build ignore` directive so that it won't be picked up by the normal build tooling.
### Usage with `go generate`
If your dependencies change often, depender can be paired with `go generate` to streamline the workflow. For example, you can create a file containing the following:
```go
//go:generate go run github.com/acj/depender ./...
```
Then, running `go generate` will update `deps.go` with the current list of dependencies.
## Does it help?
For a recent project that imported kubernetes/kubernetes, aws-sdk-go, and several other nontrivial packages, this reduced my incremental container build times from ~110 seconds to roughly 15 seconds.