-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcached.Dockerfile
53 lines (40 loc) · 1.39 KB
/
cached.Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
############################
# STEP 1 build executable binary
############################
FROM --platform=$BUILDPLATFORM golang:alpine AS builder
# Install git.
RUN apk update && apk add --no-cache git=~2
# Set up working directory
WORKDIR /app
# Copy go.mod and go.sum separately so we only invalidate the downloading layers if we need to
COPY go.mod go.sum ./
# Fetch dependencies and build the binary
ENV GO111MODULE=on
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=bind,source=go.sum,target=go.sum \
--mount=type=bind,source=go.mod,target=go.mod \
go mod download -x
# Copy the rest of the project to ensure code changes doesnt trigger re-download of all deps
COPY . .
ENV GOCACHE=/root/.cache/go-build
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=cache,target="/root/.cache/go-build" \
CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$GOARCH go build -a -installsuffix cgo -o main .
############################
# STEP 2 build a small image
############################
FROM alpine:3
# Set up the working directory
WORKDIR /go
# Copy the binary from the builder stage
COPY --from=builder /app/main .
# Copy the "index" folder
COPY --from=builder /app/index index
# Copy the "docs" folder
COPY --from=builder /app/docs docs
# Set environment variables and expose necessary port
ENV PORT=8080
ENV GIN_MODE=release
EXPOSE 8080
# Run the Go Gin binary
ENTRYPOINT ["./main"]