commit c370bfdc5b8c0b81b860990c558b384a2435350c Author: tajniak81 <13187254+tajniak81@users.noreply.github.com> Date: Thu Nov 19 00:09:11 2026 +0100 Add TrailBase Docker packaging (root and rootless) Multi-stage Alpine images that download a pinned TrailBase release, verify its SHA-256 digests, and ship only the static binary plus the auth UI component. The entrypoint bootstraps the admin from .env by taking over the auto-created admin@localhost on a fresh depot. Includes compose files, .env.example, bump.sh and README. Co-Authored-By: Claude Opus 5.5 diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..0486e0e --- /dev/null +++ b/.env.example @@ -0,0 +1,33 @@ +# Copy this file to `.env` and adjust as needed: +# cp .env.example .env +# Compose reads `.env` automatically. Keep `.env` OUT of version control. + +# ── General ─────────────────────────────────────────────────────────── +# TrailBase version to build (must match a published release tag, without the leading "v"). +# Change it with ./bump.sh so the pinned checksums in the Dockerfiles stay in sync. +TB_VERSION=0.36.1 + +# Name of the running container +CONTAINER_NAME=trailbase + +# Port TrailBase listens on (used both inside the container and for the host mapping) +TB_PORT=4000 + +# Port for the root variant (only used with docker-compose.root.yml) +TB_PORT_ROOT=4000 + +# Public URL TrailBase is reached at (e.g. https://api.example.com). +# Needed for valid links in auth emails and for OAuth redirects. Leave blank for local use. +TB_PUBLIC_URL= + +# ── Admin bootstrap ─────────────────────────────────────────────────── +# On every start the entrypoint checks whether TB_ADMIN_EMAIL already exists: +# • admin exists → nothing happens (TB_ADMIN_PASSWORD is ignored/optional) +# • user, not admin → the user is promoted to admin +# • missing → the admin is created with TB_ADMIN_PASSWORD (required in that case) +# On a fresh depot the admin is made by taking over TrailBase's auto-created +# `admin@localhost` account, so no stray default admin is left behind. +# No defaults. Leave blank to skip bootstrapping entirely — TrailBase then creates +# `admin@localhost` on first start and prints its random password in the container log. +TB_ADMIN_EMAIL= +TB_ADMIN_PASSWORD= diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..d3d2dd0 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,6 @@ +# Shell scripts and Docker files run inside Linux containers — they MUST keep +# LF endings or the container fails with "no such file or directory". +*.sh text eol=lf +Dockerfile* text eol=lf +docker-compose*.yml text eol=lf +.env.example text eol=lf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8d16d6c --- /dev/null +++ b/.gitignore @@ -0,0 +1,25 @@ +# ── Secrets / local config ──────────────────────────────────────────── +# .env holds admin credentials — never commit it. +.env +.env.* +!.env.example + +# ── TrailBase runtime data ──────────────────────────────────────────── +# Local bind-mounts of the depot (SQLite DBs, config, secrets, uploads, backups). +traildepot/ +trail +trail.exe + +# Migrations are usually versioned; ignore only local scratch copies. +# migrations/ + +# ── Tooling ─────────────────────────────────────────────────────────── +.claude/settings.local.json + +# ── OS / editor cruft ───────────────────────────────────────────────── +.DS_Store +Thumbs.db +*.swp +*~ +.vscode/ +.idea/ diff --git a/Dockerfile.root b/Dockerfile.root new file mode 100644 index 0000000..f574dc5 --- /dev/null +++ b/Dockerfile.root @@ -0,0 +1,67 @@ +# syntax=docker/dockerfile:1 + +# ---- build stage: fetch + verify + unzip TrailBase ---- +FROM alpine:3.22 AS build + +ARG TB_VERSION=0.36.1 +# SHA-256 of the release zips (GitHub release asset digests). Refreshed by bump.sh. +ARG TB_SHA256_AMD64=62f74261cf35b798f6e4d4e54b5477ae0fae26ffb5027c83f34d5679ba38ed9f +ARG TB_SHA256_ARM64=3ddc605191bdb283d97de824fa1ec3a25d96b08abff54452edd5c9a241c76f4e +ARG TB_SHA256_AUTH_UI=878853f4a3e743a425c16230e1dd54a6b70590e97a81d64a40d2bfbb643290d8 +# provided automatically by buildx (e.g. amd64, arm64) +ARG TARGETARCH + +RUN apk add --no-cache unzip ca-certificates + +# download the static (musl) Linux binary and the prebuilt auth UI WASM component +ADD https://github.com/trailbaseio/trailbase/releases/download/v${TB_VERSION}/trailbase_v${TB_VERSION}_x86_64_linux.zip /tmp/tb_amd64.zip +ADD https://github.com/trailbaseio/trailbase/releases/download/v${TB_VERSION}/trailbase_v${TB_VERSION}_aarch64_linux.zip /tmp/tb_arm64.zip +ADD https://github.com/trailbaseio/trailbase/releases/download/v${TB_VERSION}/trailbase_v${TB_VERSION}_wasm_auth_ui.zip /tmp/tb_auth_ui.zip + +# verify integrity against the pinned digests, then unzip +RUN case "${TARGETARCH}" in \ + amd64) SHA="${TB_SHA256_AMD64}" ;; \ + arm64) SHA="${TB_SHA256_ARM64}" ;; \ + *) echo "unsupported TARGETARCH: ${TARGETARCH}" >&2; exit 1 ;; \ + esac \ + && echo "${SHA} /tmp/tb_${TARGETARCH}.zip" | sha256sum -c - \ + && echo "${TB_SHA256_AUTH_UI} /tmp/tb_auth_ui.zip" | sha256sum -c - \ + && mkdir -p /out/wasm \ + && unzip -j /tmp/tb_${TARGETARCH}.zip trail -d /out/ \ + && unzip -j /tmp/tb_auth_ui.zip trailbase_auth_ui_component.wasm -d /out/wasm/ \ + && chmod 0755 /out/trail + +# ---- runtime stage: minimal image with just the binary ---- +# NOTE: this variant runs TrailBase as root (no dedicated user / USER directive). +FROM alpine:3.22 + +# ca-certificates: outbound TLS (e.g. OAuth, SMTP) +# sqlite: used by the entrypoint to check whether the admin user already exists +# tini: PID 1 that forwards signals and reaps zombies +RUN apk add --no-cache ca-certificates sqlite tini \ + && mkdir -p /app/traildepot/wasm + +COPY --from=build /out/trail /app/trail +# the auth UI (login/register pages); copied into a fresh named volume on first start +COPY --from=build /out/wasm/ /app/traildepot/wasm/ +COPY --chmod=0755 entrypoint.root.sh /app/entrypoint.root.sh + +# uncomment to copy the local migrations dir into the image +# COPY ./migrations /app/traildepot/migrations + +# persist databases, config, uploads, secrets and backups across container recreations +VOLUME /app/traildepot + +WORKDIR /app + +# default listen port; override at runtime with -e TB_PORT=... +ENV TB_PORT=4000 +EXPOSE 4000 + +# shell form so ${TB_PORT} is expanded from the runtime environment +HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \ + CMD wget -qO- "http://127.0.0.1:${TB_PORT}/api/healthcheck" || exit 1 + +# runs as root (default user — no USER directive) +# entrypoint handles admin bootstrap, then execs `trail run` +ENTRYPOINT ["/sbin/tini", "--", "/app/entrypoint.root.sh"] diff --git a/Dockerfile.rootless b/Dockerfile.rootless new file mode 100644 index 0000000..3ea83fd --- /dev/null +++ b/Dockerfile.rootless @@ -0,0 +1,69 @@ +# syntax=docker/dockerfile:1 + +# ---- build stage: fetch + verify + unzip TrailBase ---- +FROM alpine:3.22 AS build + +ARG TB_VERSION=0.36.1 +# SHA-256 of the release zips (GitHub release asset digests). Refreshed by bump.sh. +ARG TB_SHA256_AMD64=62f74261cf35b798f6e4d4e54b5477ae0fae26ffb5027c83f34d5679ba38ed9f +ARG TB_SHA256_ARM64=3ddc605191bdb283d97de824fa1ec3a25d96b08abff54452edd5c9a241c76f4e +ARG TB_SHA256_AUTH_UI=878853f4a3e743a425c16230e1dd54a6b70590e97a81d64a40d2bfbb643290d8 +# provided automatically by buildx (e.g. amd64, arm64) +ARG TARGETARCH + +RUN apk add --no-cache unzip ca-certificates + +# download the static (musl) Linux binary and the prebuilt auth UI WASM component +ADD https://github.com/trailbaseio/trailbase/releases/download/v${TB_VERSION}/trailbase_v${TB_VERSION}_x86_64_linux.zip /tmp/tb_amd64.zip +ADD https://github.com/trailbaseio/trailbase/releases/download/v${TB_VERSION}/trailbase_v${TB_VERSION}_aarch64_linux.zip /tmp/tb_arm64.zip +ADD https://github.com/trailbaseio/trailbase/releases/download/v${TB_VERSION}/trailbase_v${TB_VERSION}_wasm_auth_ui.zip /tmp/tb_auth_ui.zip + +# verify integrity against the pinned digests, then unzip +RUN case "${TARGETARCH}" in \ + amd64) SHA="${TB_SHA256_AMD64}" ;; \ + arm64) SHA="${TB_SHA256_ARM64}" ;; \ + *) echo "unsupported TARGETARCH: ${TARGETARCH}" >&2; exit 1 ;; \ + esac \ + && echo "${SHA} /tmp/tb_${TARGETARCH}.zip" | sha256sum -c - \ + && echo "${TB_SHA256_AUTH_UI} /tmp/tb_auth_ui.zip" | sha256sum -c - \ + && mkdir -p /out/wasm \ + && unzip -j /tmp/tb_${TARGETARCH}.zip trail -d /out/ \ + && unzip -j /tmp/tb_auth_ui.zip trailbase_auth_ui_component.wasm -d /out/wasm/ \ + && chmod 0755 /out/trail + +# ---- runtime stage: minimal image with just the binary ---- +FROM alpine:3.22 + +# ca-certificates: outbound TLS (e.g. OAuth, SMTP) +# sqlite: used by the entrypoint to check whether the admin user already exists +# tini: PID 1 that forwards signals and reaps zombies +RUN apk add --no-cache ca-certificates sqlite tini \ + # non-root user to run TrailBase (needs a writable $HOME for the wasmtime cache) + && addgroup -S trailbase && adduser -S -G trailbase -D -h /home/trailbase trailbase \ + && mkdir -p /app/traildepot/wasm \ + && chown -R trailbase:trailbase /app + +COPY --from=build --chown=trailbase:trailbase /out/trail /app/trail +# the auth UI (login/register pages); copied into a fresh named volume on first start +COPY --from=build --chown=trailbase:trailbase /out/wasm/ /app/traildepot/wasm/ +COPY --chown=trailbase:trailbase --chmod=0755 entrypoint.rootless.sh /app/entrypoint.rootless.sh + +# uncomment to copy the local migrations dir into the image +# COPY --chown=trailbase:trailbase ./migrations /app/traildepot/migrations + +# persist databases, config, uploads, secrets and backups across container recreations +VOLUME /app/traildepot + +WORKDIR /app +USER trailbase + +# default listen port; override at runtime with -e TB_PORT=... +ENV TB_PORT=4000 +EXPOSE 4000 + +# shell form so ${TB_PORT} is expanded from the runtime environment +HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \ + CMD wget -qO- "http://127.0.0.1:${TB_PORT}/api/healthcheck" || exit 1 + +# entrypoint handles admin bootstrap, then execs `trail run` +ENTRYPOINT ["/sbin/tini", "--", "/app/entrypoint.rootless.sh"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..f4202d3 --- /dev/null +++ b/README.md @@ -0,0 +1,195 @@ +# TrailBase Docker + +Minimal, production-minded Docker images for [TrailBase](https://trailbase.io/), shipped in two flavors: + +- **Rootless** (default, recommended) — runs as a dedicated non-root `trailbase` user. +- **Root** — runs as `root`, for environments where that's simpler or required. + +Both use a multi-stage Alpine build that downloads a pinned TrailBase release (the static musl `trail` binary plus the prebuilt auth UI WASM component), **verifies their SHA-256 digests**, and ships only those files in the runtime image. An entrypoint script optionally bootstraps an admin user on every start. + +## Features + +- 🪶 Small Alpine-based image, multi-stage build (only the `trail` binary and auth UI in the final layer). +- 🔒 SHA-256 verification of the downloaded release during build (digests pinned in the Dockerfiles, refreshed by `bump.sh`). +- 👤 Rootless and root variants, kept isolated via distinct Compose project names. +- 🌱 Idempotent admin bootstrap — creates (or promotes) the admin only if needed. +- 🧹 Takes over the auto-generated `admin@localhost` account on first start, so no stray default admin remains. +- 🏗️ Multi-arch (`amd64` / `arm64`) via buildx `TARGETARCH`. +- ❤️ Built-in `HEALTHCHECK` hitting `/api/healthcheck`. +- 🛑 `tini` as PID 1 for clean signal handling. +- 💾 Persistent `traildepot` volume for databases, config, uploads, secrets and backups. + +## Requirements + +- Docker with Compose v2 (`docker compose`). +- Optionally buildx for multi-arch builds. + +## Quick start + +```bash +# 1. Configure +cp .env.example .env +# edit .env — at minimum set TB_ADMIN_EMAIL and TB_ADMIN_PASSWORD to bootstrap an admin + +# 2. Build & run (rootless variant, default) +docker compose -f docker-compose.rootless.yml up -d --build + +# 3. Open the admin UI +# http://localhost:4000/_/admin/ +``` + +For the root variant: + +```bash +docker compose -f docker-compose.root.yml up -d --build +``` + +The two stacks use separate Compose project names (`trailbase` vs `trailbase-root`), so their containers and volumes stay isolated. Both default to port `4000`; set a different `TB_PORT_ROOT` to run them side by side. + +## Configuration + +Copy `.env.example` to `.env` and adjust. Compose reads `.env` automatically; **keep it out of version control** (already covered by `.gitignore`). + +| Variable | Default | Description | +|---|---|---| +| `TB_VERSION` | `0.36.1` | TrailBase release to build (published tag without the leading `v`). Change it with `bump.sh`. | +| `CONTAINER_NAME` | `trailbase` | Base name for the container. | +| `TB_PORT` | `4000` | Port for the rootless stack (used inside the container and for the host mapping). | +| `TB_PORT_ROOT` | `4000` | Port for the root stack. | +| `TB_PUBLIC_URL` | *(empty)* | Public URL of the instance, passed as `--public-url`. Needed for auth email links and OAuth redirects. | +| `TB_ADMIN_EMAIL` | *(empty)* | Admin email to bootstrap. Leave blank to skip bootstrapping. | +| `TB_ADMIN_PASSWORD` | *(empty)* | Admin password. Required only when the user doesn't yet exist. | + +### Admin bootstrap + +On every start the entrypoint looks up `TB_ADMIN_EMAIL` in `traildepot/data/main.db`: + +- **admin exists** → nothing happens (`TB_ADMIN_PASSWORD` is ignored). +- **user exists but is not an admin** → it is promoted with `trail admin promote`. +- **missing** → the admin is created with `TB_ADMIN_PASSWORD` (required in that case, otherwise the container exits with an error): + - **fresh depot (first start)** → TrailBase auto-creates `admin@localhost` with a random password; the entrypoint takes that account over by renaming it to `TB_ADMIN_EMAIL` (`trail user change-email`) and setting `TB_ADMIN_PASSWORD` (`trail user change-password`). No stray default admin is left behind. + - **existing depot** → the user is created with `trail user add` and promoted. ⚠️ `trail user add` is broken in TrailBase 0.36.x (it inserts into a column a migration removed); in that case the container exits with a hint — create the user in the admin UI and restart to have it promoted. +- `TB_ADMIN_EMAIL` blank → bootstrap is skipped entirely. + +Passwords set through the CLI are not checked against TrailBase's password policy. + +Without bootstrapping, TrailBase's own `admin@localhost` account remains; find its random password in the first start's log: + +```bash +docker compose -f docker-compose.rootless.yml logs trailbase | grep -A2 "Created new admin user" +``` + +### TrailBase config overrides + +TrailBase stores its settings in `traildepot/config.textproto` (editable from the admin UI). Any field can be overridden with a `TRAIL_` environment variable — add them under `environment:` in the Compose file, e.g. `TRAIL_SERVER_APPLICATION_NAME: "My App"`. Secrets such as OAuth client secrets or SMTP passwords belong in `traildepot/secrets/` or in those environment variables, never in the config file. + +TrailBase has no equivalent of PocketBase's settings-encryption key; protect the `traildepot` volume instead. + +## Running CLI commands + +Any arguments given to the container are forwarded to `trail --depot=/app/traildepot` instead of starting the server: + +```bash +# against the running container +docker compose -f docker-compose.rootless.yml exec trailbase /app/trail --depot=/app/traildepot admin list + +# as a one-off container (stop the server first for write commands) +docker compose -f docker-compose.rootless.yml run --rm trailbase user change-password admin@example.com 'new-secret' +``` + +## Data persistence + +Everything TrailBase writes lives in `/app/traildepot`, backed by the named Docker volume `traildepot`: + +``` +traildepot/ +├── config.textproto # settings +├── data/ # main.db, logs.db, session.db, queue.db +├── migrations/ # SQL migrations +├── uploads/ # file uploads +├── secrets/ # keys and secrets +├── backups/ +└── wasm/ # WASM components (auth UI baked into the image) +``` + +Data survives container recreation. To develop migrations against the running instance, uncomment the bind mount in the Compose file: + +```yaml +# - ./migrations:/app/traildepot/migrations +``` + +You can also bake `migrations` into the image by uncommenting the corresponding `COPY` line in the Dockerfile. + +> **Note:** if you switch the named volume to a host bind mount (e.g. `./traildepot:/app/traildepot`) with the rootless image, make sure the host folder exists and is writable by the container user, or TrailBase fails with `PermissionDenied`. A bind mount also hides the auth UI component baked into the image; reinstall it with `trail components add trailbase/auth_ui`. + +## Building for a specific version or architecture + +```bash +# multi-arch build with buildx +docker buildx build \ + --platform linux/amd64,linux/arm64 \ + --build-arg TB_VERSION=0.36.1 \ + -f Dockerfile.rootless \ + -t trailbase:0.36.1 . +``` + +The Dockerfiles pin the SHA-256 digests of one release, so building another version with `--build-arg TB_VERSION=...` alone fails checksum verification. Use `bump.sh`. + +### Bumping the pinned version + +`bump.sh` rewrites the version in every file that pins it (`.env.example`, both +Dockerfiles, both Compose files, and this README), fetches the new release's +asset digests from the GitHub API into both Dockerfiles, and commits the result: + +```bash +./bump.sh +``` + +Pass `--no-commit` to rewrite the files without committing. The script refuses +to run on a dirty working tree when it would commit, so unrelated changes don't +end up in the bump. When adding a new file that pins the version, add it to the +`FILES` list at the top of the script. + +## Health check + +Both images define a `HEALTHCHECK` that polls `http://127.0.0.1:${TB_PORT}/api/healthcheck`. Check status with: + +```bash +docker ps +docker inspect --format '{{.State.Health.Status}}' trailbase +``` + +## Managing the stack + +```bash +# logs +docker compose -f docker-compose.rootless.yml logs -f + +# stop +docker compose -f docker-compose.rootless.yml down + +# stop and remove the data volume (destroys the database!) +docker compose -f docker-compose.rootless.yml down -v +``` + +## Project layout + +``` +. +├── Dockerfile.rootless # non-root image (default) +├── Dockerfile.root # root image +├── docker-compose.rootless.yml # rootless stack (project: trailbase) +├── docker-compose.root.yml # root stack (project: trailbase-root) +├── entrypoint.rootless.sh # bootstrap + run (non-root) +├── entrypoint.root.sh # bootstrap + run (root) +├── bump.sh # bump pinned version + digests +├── .env.example # configuration template +├── .gitattributes # forces LF endings for shell/Docker files +└── .gitignore +``` + +> **Note:** shell scripts and Dockerfiles must keep LF line endings (enforced via `.gitattributes`) — CRLF endings break execution inside the Linux container. + +## License + +TrailBase is distributed under its own [license](https://github.com/trailbaseio/trailbase/blob/main/LICENSE). This repository contains only the Docker packaging. diff --git a/bump.sh b/bump.sh new file mode 100644 index 0000000..d5b10cc --- /dev/null +++ b/bump.sh @@ -0,0 +1,109 @@ +#!/bin/sh +# Bumps the pinned TrailBase release across every file that names it, refreshes +# the pinned SHA-256 digests in both Dockerfiles, then commits the result. +# Run on the host (Git Bash / Linux), not in the container. Needs curl. +# +# ./bump.sh 0.36.2 bump and commit +# ./bump.sh 0.36.2 --no-commit rewrite the files only +set -e + +# Every file carrying a hard-coded TB_VERSION. Keep this list in sync when +# adding a file that pins the version. +FILES=".env.example Dockerfile.root Dockerfile.rootless docker-compose.root.yml docker-compose.rootless.yml README.md" +DOCKERFILES="Dockerfile.root Dockerfile.rootless" + +NEW_VERSION="$1" +COMMIT=1 +[ "$2" = "--no-commit" ] && COMMIT=0 + +if [ -z "${NEW_VERSION}" ]; then + echo "usage: $0 [--no-commit] e.g. $0 0.36.2" >&2 + exit 1 +fi + +# Published tag without the leading "v". +if ! echo "${NEW_VERSION}" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$'; then + echo "[bump] ERROR: '${NEW_VERSION}' is not a MAJOR.MINOR.PATCH version." >&2 + exit 1 +fi + +cd "$(dirname "$0")" + +# Dockerfile.rootless is the source of truth for what is currently pinned. +OLD_VERSION=$(sed -n 's/^ARG TB_VERSION=//p' Dockerfile.rootless) +if [ -z "${OLD_VERSION}" ]; then + echo "[bump] ERROR: could not read the current version from Dockerfile.rootless." >&2 + exit 1 +fi + +if [ "${OLD_VERSION}" = "${NEW_VERSION}" ]; then + echo "[bump] Already pinned to ${NEW_VERSION} — nothing to do." + exit 0 +fi + +# A dirty tree would get swept into the bump commit. +if [ "${COMMIT}" -eq 1 ] && [ -n "$(git status --porcelain)" ]; then + echo "[bump] ERROR: working tree is not clean. Commit or stash first, or pass --no-commit." >&2 + exit 1 +fi + +# ---- fetch the asset digests GitHub publishes for the new release ---- +RELEASE_JSON=$(curl -fsSL "https://api.github.com/repos/trailbaseio/trailbase/releases/tags/v${NEW_VERSION}") || { + echo "[bump] ERROR: release v${NEW_VERSION} not found on GitHub." >&2 + exit 1 +} + +# Prints the sha256 of the named asset. Relies on GitHub listing "digest" after "name". +asset_sha() { + echo "${RELEASE_JSON}" \ + | tr -d '\r' \ + | grep -E '"name":|"digest":' \ + | grep -A1 "\"name\": *\"$1\"" \ + | sed -n 's/.*"digest": *"sha256:\([0-9a-f]\{64\}\)".*/\1/p' \ + | head -n1 +} + +SHA_AMD64=$(asset_sha "trailbase_v${NEW_VERSION}_x86_64_linux.zip") +SHA_ARM64=$(asset_sha "trailbase_v${NEW_VERSION}_aarch64_linux.zip") +SHA_AUTH_UI=$(asset_sha "trailbase_v${NEW_VERSION}_wasm_auth_ui.zip") + +for v in SHA_AMD64 SHA_ARM64 SHA_AUTH_UI; do + eval "val=\${$v}" + if [ -z "${val}" ]; then + echo "[bump] ERROR: could not read the ${v} digest for v${NEW_VERSION}." >&2 + exit 1 + fi +done + +echo "[bump] ${OLD_VERSION} -> ${NEW_VERSION}" +# Escape the dots so the old version matches literally. +OLD_PATTERN=$(echo "${OLD_VERSION}" | sed 's/\./\./g') +# shellcheck disable=SC2086 +sed -i "s/${OLD_PATTERN}/${NEW_VERSION}/g" ${FILES} + +# shellcheck disable=SC2086 +sed -i \ + -e "s/^ARG TB_SHA256_AMD64=.*/ARG TB_SHA256_AMD64=${SHA_AMD64}/" \ + -e "s/^ARG TB_SHA256_ARM64=.*/ARG TB_SHA256_ARM64=${SHA_ARM64}/" \ + -e "s/^ARG TB_SHA256_AUTH_UI=.*/ARG TB_SHA256_AUTH_UI=${SHA_AUTH_UI}/" \ + ${DOCKERFILES} + +# Catch any file the replacement missed (e.g. a version written differently). +# shellcheck disable=SC2086 +if grep -n "${OLD_PATTERN}" ${FILES}; then + echo "[bump] ERROR: ${OLD_VERSION} still present in the files above." >&2 + exit 1 +fi + +# shellcheck disable=SC2086 +grep -n -e "${NEW_VERSION}" -e "^ARG TB_SHA256_" ${FILES} + +if [ "${COMMIT}" -eq 0 ]; then + echo "[bump] Files rewritten, not committed." + exit 0 +fi + +# shellcheck disable=SC2086 +git add ${FILES} +git commit -m "Bump TrailBase to ${NEW_VERSION}" +echo "[bump] Committed." diff --git a/docker-compose.root.yml b/docker-compose.root.yml new file mode 100644 index 0000000..c6084b2 --- /dev/null +++ b/docker-compose.root.yml @@ -0,0 +1,43 @@ +# Root TrailBase (runs as root inside the container). +# Run: docker compose -f docker-compose.root.yml up -d +# A distinct project name keeps its container/volume separate from the rootless stack. +name: trailbase-root + +services: + trailbase: + build: + context: . + dockerfile: Dockerfile.root + args: + # TrailBase version to bake into the image (see .env) + TB_VERSION: ${TB_VERSION:-0.36.1} + image: trailbase:${TB_VERSION:-0.36.1}-root + container_name: ${CONTAINER_NAME:-trailbase}-root + restart: unless-stopped + environment: + # server + TB_PORT: ${TB_PORT_ROOT:-4000} + TB_PUBLIC_URL: ${TB_PUBLIC_URL:-} + # admin bootstrap (checked/created by the entrypoint on every start) + TB_ADMIN_EMAIL: ${TB_ADMIN_EMAIL:-} + TB_ADMIN_PASSWORD: ${TB_ADMIN_PASSWORD:-} + RUST_BACKTRACE: "1" + # config overrides: any TRAIL_ variable overrides config.textproto, e.g. + # TRAIL_SERVER_APPLICATION_NAME: "My App" + ports: + # host:container + - "${TB_PORT_ROOT:-4000}:${TB_PORT_ROOT:-4000}" + volumes: + # persist databases, config, uploads, secrets and backups + - traildepot:/app/traildepot + # uncomment to develop migrations against the running instance + # - ./migrations:/app/traildepot/migrations + healthcheck: + test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:${TB_PORT_ROOT:-4000}/api/healthcheck || exit 1"] + interval: 30s + timeout: 5s + start_period: 10s + retries: 3 + +volumes: + traildepot: diff --git a/docker-compose.rootless.yml b/docker-compose.rootless.yml new file mode 100644 index 0000000..633a58e --- /dev/null +++ b/docker-compose.rootless.yml @@ -0,0 +1,43 @@ +# Rootless TrailBase (runs as the non-root `trailbase` user). +# Run: docker compose -f docker-compose.rootless.yml up -d +# For the root variant, use: docker compose -f docker-compose.root.yml up -d +name: trailbase + +services: + trailbase: + build: + context: . + dockerfile: Dockerfile.rootless + args: + # TrailBase version to bake into the image (see .env) + TB_VERSION: ${TB_VERSION:-0.36.1} + image: trailbase:${TB_VERSION:-0.36.1} + container_name: ${CONTAINER_NAME:-trailbase} + restart: unless-stopped + environment: + # server + TB_PORT: ${TB_PORT:-4000} + TB_PUBLIC_URL: ${TB_PUBLIC_URL:-} + # admin bootstrap (checked/created by the entrypoint on every start) + TB_ADMIN_EMAIL: ${TB_ADMIN_EMAIL:-} + TB_ADMIN_PASSWORD: ${TB_ADMIN_PASSWORD:-} + RUST_BACKTRACE: "1" + # config overrides: any TRAIL_ variable overrides config.textproto, e.g. + # TRAIL_SERVER_APPLICATION_NAME: "My App" + ports: + # host:container + - "${TB_PORT:-4000}:${TB_PORT:-4000}" + volumes: + # persist databases, config, uploads, secrets and backups + - traildepot:/app/traildepot + # uncomment to develop migrations against the running instance + # - ./migrations:/app/traildepot/migrations + healthcheck: + test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:${TB_PORT:-4000}/api/healthcheck || exit 1"] + interval: 30s + timeout: 5s + start_period: 10s + retries: 3 + +volumes: + traildepot: diff --git a/entrypoint.root.sh b/entrypoint.root.sh new file mode 100644 index 0000000..ab98b8e --- /dev/null +++ b/entrypoint.root.sh @@ -0,0 +1,93 @@ +#!/bin/sh +# TrailBase container entrypoint (root variant). +# Runs on every start: conditionally bootstraps the admin user, then execs the +# server. POSIX sh (busybox ash on Alpine). +# +# Any arguments are forwarded to `trail` instead of starting the server, e.g. +# docker compose -f docker-compose.root.yml run --rm trailbase admin list +set -e + +DEPOT="/app/traildepot" +DB_FILE="${DEPOT}/data/main.db" +TB_PORT="${TB_PORT:-4000}" +# TrailBase auto-creates this admin (random password, printed to the log) on a fresh depot. +DEFAULT_ADMIN="admin@localhost" + +trail() { + /app/trail --depot="${DEPOT}" "$@" +} + +# ---- one-off CLI command instead of the server ---- +if [ "$#" -gt 0 ]; then + exec /app/trail --depot="${DEPOT}" "$@" +fi + +# ---- look up a user's admin flag: prints 1 (admin), 0 (user) or nothing (missing) ---- +admin_flag() { + [ -f "${DB_FILE}" ] || return 0 + # escape single quotes for the SQL string literal + email_sql=$(printf '%s' "$1" | sed "s/'/''/g") + sqlite3 "${DB_FILE}" \ + "SELECT CASE WHEN admin > 0 THEN 1 ELSE 0 END FROM _user WHERE email = '${email_sql}' LIMIT 1;" \ + 2>/dev/null || true +} + +# ---- admin bootstrap (always evaluated, before the server starts) ---- +if [ -n "${TB_ADMIN_EMAIL}" ]; then + FIRST_BOOT=0 + [ -f "${DB_FILE}" ] || FIRST_BOOT=1 + + case "$(admin_flag "${TB_ADMIN_EMAIL}")" in + 1) + echo "[entrypoint] Admin '${TB_ADMIN_EMAIL}' already exists — skipping creation." + ;; + 0) + echo "[entrypoint] User '${TB_ADMIN_EMAIL}' exists but is not an admin — promoting." + trail admin promote "${TB_ADMIN_EMAIL}" + ;; + *) + if [ -z "${TB_ADMIN_PASSWORD}" ]; then + echo "[entrypoint] ERROR: admin '${TB_ADMIN_EMAIL}' does not exist and TB_ADMIN_PASSWORD is not set." >&2 + exit 1 + fi + + if [ "${FIRST_BOOT}" -eq 1 ]; then + # Initialise the fresh depot. TrailBase creates the default admin + # (admin@localhost, random password) here, which is then taken over: + # renamed to TB_ADMIN_EMAIL and given TB_ADMIN_PASSWORD. + echo "[entrypoint] Initialising new depot..." + trail admin list > /dev/null 2>&1 + fi + + if [ "${FIRST_BOOT}" -eq 1 ] && [ "$(admin_flag "${DEFAULT_ADMIN}")" = "1" ]; then + echo "[entrypoint] Creating admin '${TB_ADMIN_EMAIL}' from the default admin account..." + if [ "${TB_ADMIN_EMAIL}" != "${DEFAULT_ADMIN}" ]; then + trail user change-email "${DEFAULT_ADMIN}" "${TB_ADMIN_EMAIL}" + fi + trail user change-password "${TB_ADMIN_EMAIL}" "${TB_ADMIN_PASSWORD}" + else + echo "[entrypoint] Creating admin '${TB_ADMIN_EMAIL}'..." + if ! trail user add "${TB_ADMIN_EMAIL}" "${TB_ADMIN_PASSWORD}"; then + echo "[entrypoint] ERROR: 'trail user add' failed (it is broken in TrailBase 0.36.x)." >&2 + echo "[entrypoint] Create the user in the admin UI, then restart to promote it." >&2 + exit 1 + fi + trail admin promote "${TB_ADMIN_EMAIL}" + fi + ;; + esac +else + echo "[entrypoint] TB_ADMIN_EMAIL not set — skipping admin bootstrap." + echo "[entrypoint] On a fresh depot TrailBase creates '${DEFAULT_ADMIN}' and prints its password in the log." +fi + +# ---- server flags ---- +# --public-url is a global flag, so it must precede the `run` subcommand. +set -- +if [ -n "${TB_PUBLIC_URL}" ]; then + set -- --public-url="${TB_PUBLIC_URL}" +fi +set -- "$@" run --address="0.0.0.0:${TB_PORT}" + +echo "[entrypoint] Starting TrailBase on 0.0.0.0:${TB_PORT}" +exec /app/trail --depot="${DEPOT}" "$@" diff --git a/entrypoint.rootless.sh b/entrypoint.rootless.sh new file mode 100644 index 0000000..91cf10b --- /dev/null +++ b/entrypoint.rootless.sh @@ -0,0 +1,93 @@ +#!/bin/sh +# TrailBase container entrypoint. +# Runs on every start: conditionally bootstraps the admin user, then execs the +# server. POSIX sh (busybox ash on Alpine). +# +# Any arguments are forwarded to `trail` instead of starting the server, e.g. +# docker compose -f docker-compose.rootless.yml run --rm trailbase admin list +set -e + +DEPOT="/app/traildepot" +DB_FILE="${DEPOT}/data/main.db" +TB_PORT="${TB_PORT:-4000}" +# TrailBase auto-creates this admin (random password, printed to the log) on a fresh depot. +DEFAULT_ADMIN="admin@localhost" + +trail() { + /app/trail --depot="${DEPOT}" "$@" +} + +# ---- one-off CLI command instead of the server ---- +if [ "$#" -gt 0 ]; then + exec /app/trail --depot="${DEPOT}" "$@" +fi + +# ---- look up a user's admin flag: prints 1 (admin), 0 (user) or nothing (missing) ---- +admin_flag() { + [ -f "${DB_FILE}" ] || return 0 + # escape single quotes for the SQL string literal + email_sql=$(printf '%s' "$1" | sed "s/'/''/g") + sqlite3 "${DB_FILE}" \ + "SELECT CASE WHEN admin > 0 THEN 1 ELSE 0 END FROM _user WHERE email = '${email_sql}' LIMIT 1;" \ + 2>/dev/null || true +} + +# ---- admin bootstrap (always evaluated, before the server starts) ---- +if [ -n "${TB_ADMIN_EMAIL}" ]; then + FIRST_BOOT=0 + [ -f "${DB_FILE}" ] || FIRST_BOOT=1 + + case "$(admin_flag "${TB_ADMIN_EMAIL}")" in + 1) + echo "[entrypoint] Admin '${TB_ADMIN_EMAIL}' already exists — skipping creation." + ;; + 0) + echo "[entrypoint] User '${TB_ADMIN_EMAIL}' exists but is not an admin — promoting." + trail admin promote "${TB_ADMIN_EMAIL}" + ;; + *) + if [ -z "${TB_ADMIN_PASSWORD}" ]; then + echo "[entrypoint] ERROR: admin '${TB_ADMIN_EMAIL}' does not exist and TB_ADMIN_PASSWORD is not set." >&2 + exit 1 + fi + + if [ "${FIRST_BOOT}" -eq 1 ]; then + # Initialise the fresh depot. TrailBase creates the default admin + # (admin@localhost, random password) here, which is then taken over: + # renamed to TB_ADMIN_EMAIL and given TB_ADMIN_PASSWORD. + echo "[entrypoint] Initialising new depot..." + trail admin list > /dev/null 2>&1 + fi + + if [ "${FIRST_BOOT}" -eq 1 ] && [ "$(admin_flag "${DEFAULT_ADMIN}")" = "1" ]; then + echo "[entrypoint] Creating admin '${TB_ADMIN_EMAIL}' from the default admin account..." + if [ "${TB_ADMIN_EMAIL}" != "${DEFAULT_ADMIN}" ]; then + trail user change-email "${DEFAULT_ADMIN}" "${TB_ADMIN_EMAIL}" + fi + trail user change-password "${TB_ADMIN_EMAIL}" "${TB_ADMIN_PASSWORD}" + else + echo "[entrypoint] Creating admin '${TB_ADMIN_EMAIL}'..." + if ! trail user add "${TB_ADMIN_EMAIL}" "${TB_ADMIN_PASSWORD}"; then + echo "[entrypoint] ERROR: 'trail user add' failed (it is broken in TrailBase 0.36.x)." >&2 + echo "[entrypoint] Create the user in the admin UI, then restart to promote it." >&2 + exit 1 + fi + trail admin promote "${TB_ADMIN_EMAIL}" + fi + ;; + esac +else + echo "[entrypoint] TB_ADMIN_EMAIL not set — skipping admin bootstrap." + echo "[entrypoint] On a fresh depot TrailBase creates '${DEFAULT_ADMIN}' and prints its password in the log." +fi + +# ---- server flags ---- +# --public-url is a global flag, so it must precede the `run` subcommand. +set -- +if [ -n "${TB_PUBLIC_URL}" ]; then + set -- --public-url="${TB_PUBLIC_URL}" +fi +set -- "$@" run --address="0.0.0.0:${TB_PORT}" + +echo "[entrypoint] Starting TrailBase on 0.0.0.0:${TB_PORT}" +exec /app/trail --depot="${DEPOT}" "$@"