/
Dockerfile
47 lines (39 loc) · 1.36 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
45
46
47
# Start by building the application.
FROM golang:alpine as build
RUN apk add --no-cache git build-base && \
echo "http://dl-cdn.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories && \
echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories && \
echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories && \
apk add --no-cache upx
ENV GO111MODULE=on
WORKDIR /go/src/app
COPY . .
# This section aims to do the go dep & go build ( Download the dependency )
# Go tool dep https://github.com/golang/dep
# RUN go-wrapper download # "go get -d -v ./..."
# RUN go-wrapper install
# End section
# Reference https://github.com/GoogleCloudPlatform/distroless
# RUN go get -u github.com/golang/dep/cmd/dep
# RUN dep ensure -v -vendor-only
# Check syntax error
RUN go fmt ./...
# https://golang.org/cmd/go/#hdr-Compile_packages_and_dependencies
# More go build document
# About the usage of upx https://gitlab.com/snippets/1689274
RUN CGO_ENABLE=0 GOOS=linux \
go build \
-tags netgo \
-installsuffix netgo,cgo \
-v -a \
-ldflags '-s -w -extldflags "-static"' \
-o app
# Compress go binary
# https://linux.die.net/man/1/upx
RUN upx -7 -qq app && \
upx -t app && \
mv ./app /go/bin/app
# Now copy it into our base image.
FROM gcr.io/distroless/base
COPY --from=build /go/bin/app /
ENTRYPOINT ["/app"]