Skip to content

armgit / lyftr

Public Imported from GitHub
0

Self-hosted fitness tracker. Log workouts, track nutrition, and monitor progress. Docker deployment, mobile-friendly.

3.2 KB
Raw Blame
# Lyftr demo — single-container build for Fly.io
#
# Architecture: nginx (port 80) serves the React SPA and proxies /api/ to
# the Go backend running on localhost:3000 inside the same container.
# A persistent Fly volume at /app/data holds the SQLite DB across deploys.
#
# Hourly demo reset: crond inside the container runs reset.sh every hour,
# which restores lyftr.db from a seed snapshot so the demo stays fresh.
# The seed snapshot is created manually on first deploy (see fly/SETUP.md).

# ── Stage 1: Frontend ────────────────────────────────────────────────────────
# web is an npm workspace member, so this installs from the root lockfile. `-w lyfter-web`
# limits it to web's slice; mobile/package.json is copied only because `npm ci` validates
# the lockfile against every declared workspace. Mirrors web/Dockerfile — keep them in step.
FROM --platform=$BUILDPLATFORM node:20-alpine AS frontend
WORKDIR /app
COPY package.json package-lock.json ./
COPY web/package.json ./web/
COPY mobile/package.json ./mobile/
COPY packages/shared/package.json ./packages/shared/
RUN npm ci -w lyfter-web --include-workspace-root
COPY packages/shared ./packages/shared
COPY web/ ./web/
RUN npm run build -w lyfter-web

# ── Stage 2: Backend ─────────────────────────────────────────────────────────
# CGO_ENABLED=0 produces a fully static binary — no libc dependency in runtime.
FROM --platform=$BUILDPLATFORM golang:1.26-alpine AS backend
ARG TARGETOS TARGETARCH
# Version baked into the binary. The deploy workflow passes the git tag/describe.
ARG VERSION=dev
WORKDIR /build
COPY backend/go.mod backend/go.sum ./
RUN go mod download
COPY backend/ .
# Build via backend/build.sh (copied above) — shared with backend/Dockerfile so the
# version ldflags live in one place and can't drift between the two images.
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH VERSION=$VERSION sh build.sh

# ── Stage 3: Runtime ─────────────────────────────────────────────────────────
FROM nginx:alpine
# ca-certificates: needed for Go TLS (exercise seed fetch from GitHub)
# wget: used in health checks if needed
RUN apk add --no-cache ca-certificates wget

COPY --from=backend /build/lyftr-api     /app/lyftr-api
COPY --from=frontend /app/web/dist       /usr/share/nginx/html

# Fly-specific nginx config: proxy_pass uses localhost:3000 (not :3000 service name)
COPY fly/nginx.fly.conf /etc/nginx/conf.d/default.conf

# sed strips Windows \r\n → \n so the #!/bin/sh shebang works on Linux
COPY fly/entrypoint.sh /app/entrypoint.sh
COPY fly/reset.sh      /app/reset.sh
COPY fly/snapshot.sh   /app/snapshot.sh
RUN sed -i 's/\r$//' /app/entrypoint.sh /app/reset.sh /app/snapshot.sh && chmod +x /app/entrypoint.sh /app/reset.sh /app/snapshot.sh

# /app/data is a Fly volume mount — this mkdir is a fallback for local testing
RUN mkdir -p /app/data

EXPOSE 80

ENTRYPOINT ["/app/entrypoint.sh"]