-
Notifications
You must be signed in to change notification settings - Fork 14
/
Dockerfile
53 lines (37 loc) · 1.11 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
# argument for Go version
ARG GO_VERSION=1.15.6
# STAGE 1: building the executable
FROM golang:${GO_VERSION}-alpine AS build
# git required for go mod
RUN apk add --no-cache git
# certs
RUN apk --no-cache add ca-certificates
# add a user here because addgroup and adduser are not available in scratch
RUN addgroup -S myapp \
&& adduser -S -u 10000 -g myapp myapp
# Working directory will be created if it does not exist
WORKDIR /src
# We use go modules; copy go.mod and go.sum
COPY ./go.mod ./go.sum ./
RUN go mod download
# Import code
COPY ./ ./
# Run tests
RUN CGO_ENABLED=0 go test -timeout 30s -v github.com/gbaeke/go-template/pkg/api
# Build the executable
RUN CGO_ENABLED=0 go build \
-installsuffix 'static' \
-o /app ./cmd/app
# STAGE 2: build the container to run
FROM scratch AS final
# add maintainer label
LABEL maintainer="gbaeke"
# copy compiled app
COPY --from=build /app /app
# copy ca certs
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
# copy users from builder which contains myapp user
COPY --from=0 /etc/passwd /etc/passwd
USER myapp
# run binary
ENTRYPOINT ["/app"]