-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
127 lines (100 loc) · 4.27 KB
/
Dockerfile
File metadata and controls
127 lines (100 loc) · 4.27 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# syntax=docker/dockerfile:1
# Hanzo Commerce - E-commerce Platform
# Multi-stage build for minimal production image
# ── Stage 1: Build admin SPA (Next.js static export) ─────────────────────
FROM node:22-alpine AS admin-build
WORKDIR /web
RUN apk add --no-cache libc6-compat && corepack enable pnpm
COPY app/package.json app/pnpm-lock.yaml app/pnpm-workspace.yaml ./
COPY app/admin/package.json admin/
COPY app/packages/ packages/
RUN pnpm install --frozen-lockfile
COPY app/admin/ admin/
WORKDIR /web/admin
RUN pnpm build
# ── Stage 2: Build pay UI (Vite SPA from hanzoai/pay) ────────────────────
# Canonical source lives at github.com/hanzoai/pay. Forks override PAY_REPO
# and PAY_VERSION via --build-arg; default tracks the latest tagged release.
FROM node:22-alpine AS pay-build
ARG PAY_REPO=https://github.com/hanzoai/pay.git
ARG PAY_VERSION=v0.1.0
WORKDIR /pay
RUN apk add --no-cache git && corepack enable pnpm
RUN git clone --depth=1 --branch=${PAY_VERSION} ${PAY_REPO} /pay
RUN pnpm install --frozen-lockfile && pnpm build
# ── Stage 3: Build billing admin UI (Next.js export from hanzoai/billing) ─
# Canonical source lives at github.com/hanzoai/billing. Forks override
# BILLING_REPO + BILLING_VERSION via --build-arg; default tracks the latest
# tagged release. The Next config emits a static bundle under out/ with
# basePath=/admin/billing, which commerce serves under the same prefix from
# billing/ui/dist (go:embed target).
FROM node:22-alpine AS billing-build
ARG BILLING_REPO=https://github.com/hanzoai/billing.git
ARG BILLING_VERSION=v0.1.0
WORKDIR /billing
RUN apk add --no-cache git && corepack enable pnpm
RUN git clone --depth=1 --branch=${BILLING_VERSION} ${BILLING_REPO} /billing
RUN pnpm install --frozen-lockfile && pnpm build
# ── Stage 4: Build Go binary (with embedded admin + pay + billing SPAs) ──
FROM golang:1.26-alpine AS builder
# Install build dependencies
RUN apk add --no-cache git ca-certificates tzdata gcc musl-dev
ARG TARGETARCH
WORKDIR /build
# Copy go mod files first for layer caching
COPY go.mod go.sum ./
# Download dependencies
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
# Copy source code
COPY . .
# Replace placeholder dist/ with the real Next.js export so go:embed picks up
# the actual SPA bundle at compile time.
RUN rm -rf admin/dist
COPY --from=admin-build /web/admin/out admin/dist
# Overlay the pay UI build into checkout/ui/dist so go:embed in
# checkout/embed.go picks up the real SPA bundle.
RUN rm -rf checkout/ui/dist && mkdir -p checkout/ui/dist
COPY --from=pay-build /pay/dist/ checkout/ui/dist/
# Overlay the billing admin UI build into billing/ui/dist so go:embed in
# billing/embed.go picks up the real Next.js export.
RUN rm -rf billing/ui/dist && mkdir -p billing/ui/dist
COPY --from=billing-build /billing/out/ billing/ui/dist/
# Build the binary with CGO for SQLite support
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=1 GOMAXPROCS=1 GOOS=linux GOARCH=${TARGETARCH} go build -p=1 \
-ldflags="-s -w \
-X github.com/hanzoai/commerce.GitCommit=$(git rev-parse --short HEAD) \
-X github.com/hanzoai/commerce.BuildTime=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
-o /build/commerce \
./cmd/commerce/main.go
# Production stage
FROM alpine:3.21
LABEL org.opencontainers.image.source="https://github.com/hanzoai/commerce"
# Install runtime dependencies
RUN apk add --no-cache ca-certificates tzdata curl
# Create non-root user
RUN addgroup -S hanzo && adduser -S hanzo -G hanzo
WORKDIR /app
# Copy binary from builder
COPY --from=builder /build/commerce /app/commerce
# Copy templates and static assets
COPY --from=builder /build/templates /app/templates
COPY --from=builder /build/api/templates /app/api/templates
# Create data directories
RUN mkdir -p /app/data /app/logs && \
chown -R hanzo:hanzo /app
USER hanzo
# Expose default port
EXPOSE 8001
# Environment variables
ENV COMMERCE_DIR=/app/data
ENV COMMERCE_DEV=false
ENV PORT=8001
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
CMD curl -f http://localhost:8001/healthz || exit 1
# Default command
ENTRYPOINT ["/app/commerce"]
CMD ["serve", "0.0.0.0:8001"]