forked from gabe565/portfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockerfile.prod
83 lines (64 loc) · 2.09 KB
/
Dockerfile.prod
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
78
79
80
81
82
83
# syntax=docker/dockerfile:1.12
### Go Backend ###
FROM --platform=$BUILDPLATFORM golang:1.23.4-alpine AS go-builder
WORKDIR /app
# Copy Go dependencies files
COPY go.mod go.sum ./
RUN go mod download
# Copy the Go source code and other necessary files
COPY *.go ./
COPY migrations/ migrations/
COPY internal/ internal/
RUN apk --no-cache add build-base
# Set Golang build envs based on Docker platform string
ARG TARGETPLATFORM
RUN <<EOT
set -eux
case "$TARGETPLATFORM" in
'linux/amd64') export GOARCH=amd64 ;;
'linux/arm/v6') export GOARCH=arm GOARM=6 ;;
'linux/arm/v7') export GOARCH=arm GOARM=7 ;;
'linux/arm64') export GOARCH=arm64 ;;
*) echo "Unsupported target: $TARGETPLATFORM" && exit 1 ;;
esac
export CGO_ENABLED=1
go build -o portfolio -ldflags='-w -s' -trimpath -tags disable_automigrate
EOT
### React Frontend ###
FROM --platform=$BUILDPLATFORM node:22.8.0-alpine AS node-builder
WORKDIR /app
COPY frontend/package.json frontend/package-lock.json frontend/.npmrc ./
RUN npm ci
COPY frontend/ ./
RUN npm run build
# Runtime stage
FROM alpine:3.20 AS backend
WORKDIR /app
# Install necessary system dependencies
RUN apk add --no-cache tzdata
# Create a non-root user for running the app
ARG USERNAME=portfolio
ARG UID=1000
ARG GID=$UID
RUN addgroup -g "$GID" "$USERNAME" \
&& adduser -S -u "$UID" -G "$USERNAME" "$USERNAME" \
&& mkdir pb_data && chown 1000:1000 -R pb_data
# Copy the built Go binary from the builder stage
COPY --from=go-builder /app/portfolio /app/portfolio
# Ensure the binary is executable
RUN chmod +x /app/portfolio
# Switch to the non-root user
USER $USERNAME
CMD ["./portfolio", "serve", "--http=0.0.0.0:8090", "--dir=/data"]
### NGINX Web Server ###
FROM nginx:alpine AS frontend
# Remove the default NGINX configuration file
RUN rm /etc/nginx/conf.d/default.conf
# Copy the custom NGINX configuration file
COPY nginx.conf /etc/nginx/conf.d
# Copy the build artifacts from the first stage
COPY --from=node-builder /app/dist /usr/share/nginx/html
# Expose port 80
EXPOSE 80
# Start NGINX (without daemon mode)
CMD ["nginx", "-g", "daemon off;"]