-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
44 lines (31 loc) · 1.05 KB
/
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
# Start from golang base image
FROM golang:alpine as builder
# Enable go modules
ENV GO111MODULE=on
# Install git. (alpine image does not have git in it)
RUN apk update && apk add --no-cache git
# Set current working directory
WORKDIR /first-go-app
# Note here: To avoid downloading dependencies every time we
# build image. Here, we are caching all the dependencies by
# first copying go.mod and go.sum files and downloading them,
# to be used every time we build the image if the dependencies
# are not changed.
# Copy go mod and sum files
COPY go.mod ./
# Download all dependencies.
RUN go mod download
# Now, copy the source code
COPY . .
COPY cmd/app/main.go .
# Note here: CGO_ENABLED is disabled for cross system compilation
# It is also a common best practise.
# Build the application.
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./bin/main .
# Finally our multi-stage to build a small image
# Start a new stage from scratch
FROM scratch
# Copy the Pre-built binary file
COPY --from=builder /first-go-app/bin/main .
# Run executable
CMD ["./main"]