Try the following
# GO Repo base repo FROM golang:1.12.0-alpine3.9 as builder RUN apk add git # Add Maintainer Info LABEL maintainer="<>" RUN mkdir /app ADD . /app WORKDIR /app COPY go.mod go.sum ./ # Download all the dependencies RUN go mod download COPY . . # Build the Go app RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main . # GO Repo base repo FROM alpine:latest RUN apk --no-cache add ca-certificates curl RUN mkdir /app WORKDIR /app/ # Copy the Pre-built binary file from the previous stage COPY --from=builder /app/main . # Expose port 8000 EXPOSE 8000 # Run Executable CMD ["./main"]
Here, we are creating an intermediate
docker builder container, copying the code into it, build the code inside the
builder container and then copy the
binary image to the actual docker.
This will help in both having all the dependencies in the final container and also, the size of the final image will be very small