I’m new to both docker and go , while trying to containerize a go api , I’ve remarked something weird .
#1st_stage
FROM golang:1.23rc2-alpine3.20 AS build
WORKDIR /app
ENV GO111MODULE=on
COPY ./go.mod ./go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod
go mod download
RUN apk add --no-cache shadow
RUN useradd -u 1000 go
COPY . .
RUN go build -o ./binary
#2nd_stage
FROM scratch
ENV GIN_MODE=release
EXPOSE 8080
COPY --from=build /etc/passwd /etc/passwd
COPY --from=build /app/binary .
USER go
CMD ["./binary"]
isn’t go mod download the reponsible command for downloading the dependencies (modules) which actually worked as intended , in the first build it took a while to download the modules and in the 2nd build it was pretty fast to work even though i’ve invalidated the cache of a previous layer (up to here it’s okay) , but the build commands outputs to me that it’s fetching each module from the go.mod .
I don’t get why is this happening , so an explanation will be much appreciated , and i know that my docker file may not be the most optimized so i would appreciate any remarks and tips to improve it .