-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
51 lines (42 loc) · 1.46 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
FROM golang:1.23 AS base
# Create a stage for downloading dependencies
FROM base AS deps
WORKDIR /app
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
# Stage for Templ generation
FROM ghcr.io/a-h/templ:v0.2.778 AS generate-stage
# FROM ghcr.io/a-h/templ:latest AS generate-stage
WORKDIR /app
COPY --from=deps /app /app
COPY . .
USER root
RUN ["templ", "generate"]
# Build the Go application and migrator
FROM base AS build-stage
WORKDIR /app
COPY --from=generate-stage /app /app
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=0 GOOS=linux go build -buildvcs=false -o /app/main ./cmd/main.go && \
CGO_ENABLED=0 GOOS=linux go build -buildvcs=false -o /app/migrator ./migrator/migrator.go
# Final deployment stage
FROM alpine:latest AS deploy-stage
WORKDIR /app
COPY --from=build-stage /app/main /app/main
COPY --from=build-stage /app/migrator /app/migrator
COPY --from=build-stage /app/static /app/static
# Install necessary packages
RUN apk update && apk add --no-cache bind-tools busybox-extras openssl
# Create a non-root user and switch to that user
RUN addgroup -S nonroot && adduser -S nonroot -G nonroot
# Set permissions for directories and switch to the non-root user
RUN mkdir -p /app/assets && \
chown -R nonroot:nonroot /app && \
chmod -R 755 /app
USER nonroot
# Expose the application port
EXPOSE 3030
# Set the entry point for the application
ENTRYPOINT ["/app/main"]