2024-02-12 12:30:38 +01:00
|
|
|
# Client/frontend build in an isolated stage
|
|
|
|
# We use node:latest as the base image.
|
|
|
|
# Essentially we build the frontend SPA with vite and
|
|
|
|
# make it available in the public directory.
|
2024-02-12 18:56:26 +01:00
|
|
|
FROM docker.io/node:alpine as client
|
2024-02-12 12:30:38 +01:00
|
|
|
WORKDIR /build
|
2024-02-20 19:44:26 +01:00
|
|
|
ADD frontend ./
|
2024-02-12 12:30:38 +01:00
|
|
|
RUN npm install
|
|
|
|
RUN npm run build
|
|
|
|
|
|
|
|
# Builds the server in an isolated stage
|
2024-02-12 18:56:26 +01:00
|
|
|
FROM docker.io/golang:alpine as go
|
|
|
|
RUN apk add gcompat
|
|
|
|
RUN apk add gcc
|
|
|
|
RUN apk add musl-dev
|
2024-02-20 19:44:26 +01:00
|
|
|
RUN apk add sqlite
|
2024-02-12 12:30:38 +01:00
|
|
|
WORKDIR /build
|
2024-02-20 19:44:26 +01:00
|
|
|
ADD backend/go.mod backend/go.sum ./
|
2024-02-12 12:30:38 +01:00
|
|
|
|
|
|
|
# Get the dependencies
|
|
|
|
RUN go mod download
|
|
|
|
|
2024-02-20 19:44:26 +01:00
|
|
|
# Add the source code
|
|
|
|
ADD backend .
|
|
|
|
|
2024-03-21 01:24:17 +01:00
|
|
|
RUN go build -o server
|
2024-03-09 15:07:42 +01:00
|
|
|
RUN CGO_ENABLED=1 GOOS=linux go build -a -installsuffix cgo -o ./server ./main.go
|
2024-02-20 19:44:26 +01:00
|
|
|
|
|
|
|
# Strip the binary for a smaller image
|
2024-02-13 09:31:38 +01:00
|
|
|
RUN strip ./server
|
2024-02-12 12:30:38 +01:00
|
|
|
|
2024-02-12 18:56:26 +01:00
|
|
|
# The final stage for building a minimal image
|
|
|
|
FROM docker.io/alpine:latest as runner
|
2024-02-20 19:44:26 +01:00
|
|
|
RUN adduser -D nonroot
|
|
|
|
RUN addgroup nonroot nonroot
|
2024-02-12 18:56:26 +01:00
|
|
|
WORKDIR /app
|
2024-03-21 01:24:17 +01:00
|
|
|
RUN chown nonroot:nonroot /app
|
2024-02-12 18:56:26 +01:00
|
|
|
|
|
|
|
# Copy the frontend SPA build into public
|
2024-02-20 19:44:26 +01:00
|
|
|
COPY --from=client /build/dist static
|
2024-02-12 18:56:26 +01:00
|
|
|
|
|
|
|
# Copy the server binary
|
2024-02-20 19:44:26 +01:00
|
|
|
COPY --from=go /build/server server
|
2024-02-12 18:56:26 +01:00
|
|
|
|
2024-02-12 12:30:38 +01:00
|
|
|
# Expose port 8080
|
|
|
|
EXPOSE 8080
|
|
|
|
|
2024-02-20 19:44:26 +01:00
|
|
|
# Set the user to nonroot
|
|
|
|
USER nonroot:nonroot
|
|
|
|
|
2024-02-12 12:30:38 +01:00
|
|
|
# Run the server
|
2024-03-09 15:07:42 +01:00
|
|
|
CMD ["./server"]
|