DEV Community

Discussion on: Creating a Dockerfile for your Go Backend

Collapse
 
sibprogrammer profile image
Alexey Yuzhakov

The common way of doing that is to use multi-stage build. Go is a compiled language. You don't need Go toolchain to run your services. So the general concept is the following: you build the binary in one stage and use another minimal image to launch it.
Here is an example:

FROM golang:1.15 as builder

ARG CGO_ENABLED=0
WORKDIR /app

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

RUN go build

FROM scratch
COPY --from=builder /app/server /server
ENTRYPOINT ["/server"]
Enter fullscreen mode Exit fullscreen mode

It bring the huge difference. The latest "golang" image is about 800 Mb (it's based on Debian). The final image of your service can be just 5-10 Mb.

Collapse
 
sadeedpv profile image
Sadeedpv🥇

I was about to explain that, But I accidentally published the post instead of saving to draft; I just saw it today