diff --git a/deploy/compose-build-at-deploy.yml b/deploy/compose-build-at-deploy.yml new file mode 100644 index 0000000..e943e4f --- /dev/null +++ b/deploy/compose-build-at-deploy.yml @@ -0,0 +1,140 @@ +# Build-from-source stack for a host that builds at deploy time (Dokploy and friends). +# +# This is the same five-service shape the instance at gym.dploy.lol runs: `source` clones the +# repository into a volume, `api` installs and runs the backend from that clone, `web-build` +# compiles the frontend, and `web` serves the result with nginx. Nothing is pulled from a +# registry, so `scripts/build-images.sh` and `.gitea/workflows/docker-publish.yml` are not part +# of this path at all. +# +# Three things here are easy to get wrong, and all three are load-bearing: +# +# 1. `web-build` copies the WHOLE repository into /app and builds from /app/frontend, not +# frontend/ on its own. The frontend imports the Coach's core from outside its own directory +# (`frontend/src/lib/coach-local.js` and `views/CoachSetup.jsx` reach into +# `../../../api/coach/core/`), so a build stage that only has frontend/ fails with nine +# UNRESOLVED_IMPORT errors. The repo's own web/Dockerfile copies api/coach/core/ into its +# build stage for the same reason. +# +# 2. The volumes are pinned with `name:`. They hold the users, passkeys, workouts and weigh-ins +# — `kuentagym-data` above all. Those names are this deployment's existing volumes; they are +# spelled out so a restyled stack keeps pointing at the same data instead of silently +# creating empty ones. Never change them without moving the data first, and never run +# `docker compose down -v` — `-v` deletes the volumes, which is every logged set and every +# weigh-in. A plain `docker compose down` is safe. Check yours with `docker volume ls`. +# +# 3. nginx needs `client_max_body_size`. Its default request limit is 1 MB, and the app PUTs +# the whole profile to /api/data on every change — a couple of years of training passes 1 MB +# and the sync then fails with a 413 the UI cannot explain. web/nginx.conf.template in the +# repository sets 5m, and the API's own MAX_BODY is 5 MB; this matches both. +# +# To deploy a change to the code, force the one-shot services to run again — `source` and +# `web-build` have `restart: "no"`, so compose leaves them alone when their configuration has not +# changed: +# +# docker compose up -d --force-recreate +# +# Set RP_ID, RP_NAME and ORIGIN in the host's environment (or its .env). RP_ID must be the exact +# hostname the app is served from — passkeys are bound to it — and RP_NAME is what the passkey +# prompt shows, so set it to KuentaGYM. + +name: kuentagym + +services: + + # One clone, shared by the two build services below. Shallow: only the tip of the branch is + # ever built, and a full history here would just be a bigger volume. + source: + image: alpine:3.20 + volumes: + - kuentagym-source:/work + command: + - /bin/sh + - -c + - "apk add --no-cache git ca-certificates && rm -rf /work/repo && git clone --depth 1 https://git.dploy.lol/dsantillan/kuentagym.git /work/repo" + restart: "no" + + # The exercise images and GIFs (~140 MB), fetched once and kept in their own volumes. See + # NOTICE.md: the media belongs to Gym visual and is downloaded here rather than redistributed. + media: + image: alpine:3.20 + volumes: + - kuentagym-media-img:/out/img + - kuentagym-media-gif:/out/gif + command: + - /bin/sh + - -c + - "apk add --no-cache git ca-certificates && mkdir -p /out/img /out/gif && if [ -z \"$$(ls -A /out/img 2>/dev/null)\" ]; then rm -rf /tmp/exercises && git clone --depth 1 https://github.com/hasaneyldrm/exercises-dataset.git /tmp/exercises && cp /tmp/exercises/images/*.jpg /out/img/ && cp /tmp/exercises/videos/*.gif /out/gif/; fi" + restart: "no" + + api: + image: node:22-alpine + depends_on: + source: + condition: service_completed_successfully + working_dir: /app + volumes: + - kuentagym-source:/source:ro + - kuentagym-data:/data + environment: + PORT: "3000" + DATA_DIR: /data + NODE_ENV: production + RP_ID: ${RP_ID} + RP_NAME: ${RP_NAME} + ORIGIN: ${ORIGIN} + command: + - /bin/sh + - -c + - "rm -rf /app/* && cp -a /source/repo/api/. /app/ && npm install --omit=dev && exec node server.js" + restart: unless-stopped + + web-build: + image: node:22-alpine + depends_on: + source: + condition: service_completed_successfully + media: + condition: service_completed_successfully + working_dir: /app + volumes: + - kuentagym-source:/source:ro + - kuentagym-web-dist:/dist + - kuentagym-media-img:/media-img:ro + - kuentagym-media-gif:/media-gif:ro + command: + - /bin/sh + - -c + - "rm -rf /app/* && cp -a /source/repo/. /app/ && cd /app/frontend && npm install && npm run build && rm -rf /dist/* && cp -a /app/frontend/dist/. /dist/ && mkdir -p /dist/img /dist/gif && cp -a /media-img/. /dist/img/ && cp -a /media-gif/. /dist/gif/" + restart: "no" + + web: + image: nginx:alpine + depends_on: + web-build: + condition: service_completed_successfully + api: + condition: service_started + ports: + - "${WEB_PORT:-8080}:80" + volumes: + - kuentagym-web-dist:/usr/share/nginx/html:ro + command: + - /bin/sh + - -c + - "printf 'server { listen 80; server_name _; root /usr/share/nginx/html; index index.html; client_max_body_size 5m; location /api/ { proxy_pass http://api:3000; proxy_http_version 1.1; proxy_set_header Host $$host; proxy_set_header X-Real-IP $$remote_addr; proxy_set_header X-Forwarded-For $$proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $$scheme; } location / { try_files $$uri $$uri/ /index.html; } }' > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'" + restart: unless-stopped + +volumes: + # `name:` is what keeps these pointing at the volumes this deployment already has. Change a + # value here and compose uses a different, empty volume — the profiles and the training history + # do not move with it. + kuentagym-source: + name: personales-gymfv-3bahxv_opengym-source + kuentagym-data: + name: personales-gymfv-3bahxv_opengym-data + kuentagym-web-dist: + name: personales-gymfv-3bahxv_opengym-web-dist + kuentagym-media-img: + name: personales-gymfv-3bahxv_opengym-media-img + kuentagym-media-gif: + name: personales-gymfv-3bahxv_opengym-media-gif