-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
60 lines (45 loc) · 1.29 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
48
49
50
51
52
53
54
55
56
57
58
59
60
############################
# STEP 1 build executable binary
############################
FROM golang:1.21-alpine AS build
# Install git + SSL ca certificates.
## Git is required for fetching the dependencies.
## Ca-certificates is required to call HTTPS endpoints.
RUN apk update && apk add --no-cache git ca-certificates tzdata && update-ca-certificates
# Set docker environment
ARG env
ARG GITLAB_USER
ARG GITLAB_PASSWORD
# For get private gitlab repository
RUN echo "machine gitlab.com login ${GITLAB_USER} password ${GITLAB_PASSWORD}" > ~/.netrc && chmod 600 ~/.netrc
# Set Golang Env
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64 \
env=${env} \
GOPRIVATE=gitlab.com
WORKDIR /go-app
COPY . .
# Fetch dependencies.
## Using go get.
## RUN go get -d -v
## Using go mod.
RUN go mod download
RUN go mod verify
# Build the binary.
RUN go build -a -ldflags="-w -s" -o goapp .
############################
# STEP 2 build a small image
############################
FROM scratch
# Import from builder.
COPY --from=build /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
WORKDIR /go-app
# Copy our static executable
COPY --from=build /go-app .
# Open Port
EXPOSE 8080
# Run the binary.
ENTRYPOINT ["./goapp"]