mingrammer / flog

:tophat: A fake log generator for common log formats
MIT License
1.12k stars 135 forks source link

Ask; Why don't build directly, not copying go module file? #43

Closed sejin-P closed 3 years ago

sejin-P commented 3 years ago

Current Dockerfile:

FROM golang

ENV CGO_ENABLED=0
ENV GO111MODULE=on

WORKDIR /go/src/flog

COPY go.mod go.sum ./
RUN go mod download

COPY . ./
RUN go build -o /bin/flog

FROM scratch
COPY --from=0 /bin/flog /bin/flog
ENTRYPOINT ["flog"]

But we can build binary file directly like this:

FROM golang

ENV CGO_ENABLED=0
ENV GO111MODULE=on

WORKDIR /go/src/flog

COPY . ./
RUN go build -ldflags="-w -s" -o /bin/flog

FROM scratch
COPY --from=0 /bin/flog /bin/flog
ENTRYPOINT ["flog"]

Is there any problem to build binary file directly? I know it's not essential or critical point, but I want to make it simpler. Thanks for reading.

mingrammer commented 3 years ago

Pre-downloading the modules makes the image layer with Go module caches. With the module cache, we don't have to re-download the modules if there are no module changes, then we can only build source code.