Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dockerfile: cache go build directories across builds #240

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nadiamoe
Copy link

This is a small, backwards compatible improvement that allows reusing the go build and package cache across container runs. This drastically reduces build times after the first build:

First build (36s):

13:07:30 ~/Staging/yarr $> docker build . -f etc/dockerfile -t ghcr.io/nadiamoe/yarr
[+] Building 36.1s (14/14) FINISHED                                                                                                                                                                                      docker:default
 => [internal] load build definition from dockerfile                                                                                                                                                                               0.1s
 => => transferring dockerfile: 486B                                                                                                                                                                                               0.0s
 => [internal] load metadata for docker.io/library/golang:alpine3.18                                                                                                                                                               0.5s
 => [internal] load metadata for docker.io/library/alpine:latest                                                                                                                                                                   0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                  0.1s
 => => transferring context: 2B                                                                                                                                                                                                    0.0s
 => [build 1/5] FROM docker.io/library/golang:alpine3.18@sha256:d1a601b64de09e2fa38c95e55838961811d5ca11062a8f4230a5c434b3ae2a34                                                                                                   0.0s
 => [stage-1 1/3] FROM docker.io/library/alpine:latest                                                                                                                                                                             0.0s
 => [internal] load build context                                                                                                                                                                                                  0.1s
 => => transferring context: 50.38kB                                                                                                                                                                                               0.0s
 => CACHED [build 2/5] RUN apk add build-base git                                                                                                                                                                                  0.0s
 => CACHED [build 3/5] WORKDIR /src                                                                                                                                                                                                0.0s
 => [build 4/5] COPY . .                                                                                                                                                                                                           0.3s
 => [build 5/5] RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/go/pkg   make build_linux                                                                                                     33.4s
 => CACHED [stage-1 2/3] RUN apk add --no-cache ca-certificates &&     update-ca-certificates                                                                                                                                      0.0s
 => [stage-1 3/3] COPY --from=build /src/_output/linux/yarr /usr/local/bin/yarr                                                                                                                                                    0.3s
 => exporting to image                                                                                                                                                                                                             1.0s
 => => exporting layers                                                                                                                                                                                                            0.9s
 => => writing image sha256:fa554988342c598f7867fe6e0db1761ebf2791cc8de99ca3b14f92b89c24b92e                                                                                                                                       0.0s
 => => naming to ghcr.io/nadiamoe/yarr                                                                                                                                                                                             0.0s

Subsequent build (4s):

13:08:25 ~/Staging/yarr $> docker build . -f etc/dockerfile -t ghcr.io/nadiamoe/yarr
[+] Building 4.0s (14/14) FINISHED                                                                                                                                                                                       docker:default
 => [internal] load build definition from dockerfile                                                                                                                                                                               0.1s
 => => transferring dockerfile: 486B                                                                                                                                                                                               0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                                                                                                                                                                   0.0s
 => [internal] load metadata for docker.io/library/golang:alpine3.18                                                                                                                                                               0.4s
 => [internal] load .dockerignore                                                                                                                                                                                                  0.0s
 => => transferring context: 2B                                                                                                                                                                                                    0.0s
 => [build 1/5] FROM docker.io/library/golang:alpine3.18@sha256:d1a601b64de09e2fa38c95e55838961811d5ca11062a8f4230a5c434b3ae2a34                                                                                                   0.0s
 => [internal] load build context                                                                                                                                                                                                  0.1s
 => => transferring context: 49.92kB                                                                                                                                                                                               0.0s
 => [stage-1 1/3] FROM docker.io/library/alpine:latest                                                                                                                                                                             0.0s
 => CACHED [build 2/5] RUN apk add build-base git                                                                                                                                                                                  0.0s
 => CACHED [build 3/5] WORKDIR /src                                                                                                                                                                                                0.0s
 => [build 4/5] COPY . .                                                                                                                                                                                                           0.8s
 => [build 5/5] RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/go/pkg   make build_linux                                                                                                      1.2s
 => CACHED [stage-1 2/3] RUN apk add --no-cache ca-certificates &&     update-ca-certificates                                                                                                                                      0.0s
 => [stage-1 3/3] COPY --from=build /src/_output/linux/yarr /usr/local/bin/yarr                                                                                                                                                    0.2s
 => exporting to image                                                                                                                                                                                                             0.8s
 => => exporting layers                                                                                                                                                                                                            0.8s
 => => writing image sha256:cb56047ebd0c9dfdfdf48a7a879b6c6b293c3fc3236e2f471b703d06f1fb75b6                                                                                                                                       0.0s
 => => naming to ghcr.io/nadiamoe/yarr                                                                                                                                                                                             0.0s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant