-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
51 lines (41 loc) · 1.22 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
# syntax=docker/dockerfile:1
# Create a stage for building the application.
ARG GO_VERSION=1.21
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS build
WORKDIR /src
# Move go.mod and go.sum to a different directory and copy them into /src.
COPY go.mod go.sum /app/
WORKDIR /app
RUN go mod download -x
# Switch back to /src and copy the rest of the application source code.
WORKDIR /src
COPY . .
# Build the application.
RUN --mount=type=cache,target=/go/pkg/mod/ \
CGO_ENABLED=0 GOARCH=$TARGETARCH go build -o /bin/server .
# Create a new stage for running the application.
FROM alpine:latest AS final
# Install runtime dependencies.
RUN --mount=type=cache,target=/var/cache/apk \
apk --update add \
ca-certificates \
tzdata \
&& \
update-ca-certificates
# Create a non-privileged user.
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser
USER appuser
# Copy the executable from the "build" stage.
COPY --from=build /bin/server /bin/
# Expose the port that the application listens on.
EXPOSE 8080
# Set the container entry point.
ENTRYPOINT [ "/bin/server" ]