Skip to content

Commit

Permalink
dockerfiles: cache go mod dependencies in their own layer
Browse files Browse the repository at this point in the history
this change causes docker to create an additional intermediate layer containing
just the go.mod file and the downloaded dependencies cited therefrom. this
prevents having to re-download the go.mod dependencies on EVERY image rebuild.
more specifically, as long as you don't change the go.mod file, docker will use
the cached layer.
  • Loading branch information
feuGeneA committed Sep 19, 2024
1 parent f019d90 commit 8fad770
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cli.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
FROM golang:1.21 AS builder
ARG VERSION
WORKDIR /build
ADD go.mod /build/
RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 GOOS=linux \
go mod download
ADD . /build/
RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 GOOS=linux \
go build \
Expand Down
3 changes: 3 additions & 0 deletions httpserver.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
FROM golang:1.21 AS builder
ARG VERSION
WORKDIR /build
ADD go.mod /build/
RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 GOOS=linux \
go mod download
ADD . /build/
RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 GOOS=linux \
go build \
Expand Down

0 comments on commit 8fad770

Please # to comment.