-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockerfile
66 lines (49 loc) · 1.57 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
FROM golang:1.22.5-alpine3.20 AS build
LABEL site="api"
LABEL stage="builder"
# Create webapiuser.
ENV USER=webapiuser
ENV UID=10001
# See https://stackoverflow.com/a/55757473/12429735RUN
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
"${USER}"
WORKDIR /src/
# Stores our dependencies
COPY go.mod .
COPY go.sum .
# Download dependencies
RUN go mod download
# Copy source
COPY . .
# Generate documentation
RUN go install github.com/swaggo/swag/cmd/swag@latest
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go generate
# Download git
RUN apk update && apk upgrade && \
apk add --no-cache git ca-certificates tzdata && \
update-ca-certificates
ARG WAPI_VERSION_ARG
# Set build variables
RUN echo -n "-X 'main.Version=$WAPI_VERSION_ARG" > ./ldflags && \
tr -d \\n < ./ldflags > ./temp && mv ./temp ./ldflags && \
echo -n "' -X 'main.Commit=$(git log --format="%H" -n 1)" >> ./ldflags && \
tr -d \\n < ./ldflags > ./temp && mv ./temp ./ldflags && \
echo -n "'" >> ./ldflags
RUN GOOS=linux GOARCH=amd64 go build -ldflags="$(cat ./ldflags)" -o /bin/api
FROM scratch
LABEL site="api"
# Import the user and group files from the builder.
COPY --from=build /etc/passwd /etc/passwd
COPY --from=build /etc/group /etc/group
COPY --from=build /bin/api /bin/api
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=build /usr/share/zoneinfo /usr/share/zoneinfo
# Use an unprivileged user.
USER webapiuser:webapiuser
ENTRYPOINT ["/bin/api"]