generated from fr123k/golang-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
executable file
·77 lines (59 loc) · 2.3 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Accept the Go version for the image to be set as a build argument.
ARG GO_VERSION=1.17
# Second stage: build the executable
FROM golang:${GO_VERSION}-alpine AS builder
# Create the user and group files that will be used in the running container to
# run the process an unprivileged user.
RUN mkdir /user && \
echo 'nobody:x:65534:65534:nobody:/:' > /user/passwd && \
echo 'nobody:x:65534:' > /user/group
# Import the Certificate-Authority certificates for the app to be able to send
# requests to HTTPS endpoints.
# RUN apk add --no-cache ca-certificates
# Accept the version of the app that will be injected into the compiled
# executable.
ARG APP_VERSION=undefined
# Set the environment variables for the build command.
ENV CGO_ENABLED=0 GOFLAGS=-mod=vendor
# Set the working directory outside $GOPATH to enable the support for modules.
WORKDIR /ping
COPY go.mod ./
COPY go.sum ./
COPY vendor ./vendor
# Import the code from the first stage.
# COPY --from=code /infra-hook ./
COPY srv ./srv
COPY pkg ./pkg
# Build the executable to `/app`. Mark the build as statically linked and
# inject the version as a global variable.
RUN go build \
-installsuffix 'static' \
-ldflags "-X main.Version=${APP_VERSION}" \
-o /app \
./srv/ping.go
RUN go test -v -timeout 60s ./...
# Final stage: the running container
FROM scratch AS final
# This variable should be pass in the ci build pipeline
ARG APP_VERSION=undefined
ARG GIT_COMMIT=undefined
ARG BUILD_DATE=undefined
LABEL org.label-schema.build-date="$BUILD_DATE"
LABEL org.label-schema.name="ping"
LABEL org.label-schema.description="Ping web service"
LABEL org.label-schema.vcs-url="https://github.com/fr123k/fred-the.guardian"
LABEL org.label-schema.vcs-ref="$GIT_COMMIT"
LABEL org.label-schema.version="$APP_VERSION"
LABEL org.label-schema.schema-version="1.0"
LABEL go-version="${GO_VERSION}"
# Declare the port on which the application will be run.
EXPOSE 8080
# Import the user and group files.
COPY --from=builder /user/group /user/passwd /etc/
# Import the Certificate-Authority certificates for enabling HTTPS.
# COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
# Import the compiled executable from the second stage.
COPY --from=builder /app /app
# Run the container as an unprivileged user.
USER nobody:nobody
ENTRYPOINT ["/app"]