Docker Multi-state Builds and Go
I recently ran into an issue with building my Go app in one container and then running it in another one. I do this to make my container image smaller.
Without CGO_ENABLED=0
, in a multi-stage build, it will say something like the
following.
exec /app/main: no such file or directory.
FROM golang:1.16 AS builder
WORKDIR /src/
COPY main.go go.mod go.sum /src/
RUN go get
# Without `CGO_ENABLED=0`, in a multi-stage build, it will say something like
# exec /app/main: no such file or directory
RUN CGO_ENABLED=0 go build -o /src/main /src/main.go
FROM alpine:latest
WORKDIR /app/
COPY --from=builder /src/main /app/main
COPY index.html /app/
EXPOSE 8080
ENTRYPOINT [ "/app/main" ]