Files
otunes/Dockerfile
T
joshuafhiggins Verified 1cea1fe745 feat(docker): bundle Convex backend with oTunes
- Run the web app and Convex backend in one container with generated per-install keys
- Derive Convex URLs from the server address and update self-hosting docs
2026-10-08 15:24:00 -04:00

119 lines
4.2 KiB
Docker

# ============================================
# Stage 1: Dependencies Installation Stage
# ============================================
# IMPORTANT: Node.js Version Maintenance
# This Dockerfile uses Node.js 24.13.0-slim, which was the latest LTS version at the time of writing.
# To ensure security and compatibility, regularly update the NODE_VERSION ARG to the latest LTS version.
ARG NODE_VERSION=24.13.0-slim
# The Convex backend runs inside the oTunes image. Pin to a commit tag from
# https://github.com/get-convex/convex-backend/pkgs/container/convex-backend
ARG CONVEX_BACKEND_IMAGE=ghcr.io/get-convex/convex-backend:5c7cb5bc7db457290f1769f95f1d1340912f7fd7
FROM node:${NODE_VERSION} AS node
FROM node:${NODE_VERSION} AS dependencies
WORKDIR /app
# Only manifests needed for @otunes/web and its workspace dependency (@otunes/convex).
# Mobile is intentionally omitted so its deps are never installed.
COPY package.json pnpm-lock.yaml .npmrc ./
COPY apps/web/package.json ./apps/web/
COPY packages/convex/package.json ./packages/convex/
# Explicit workspace — do not use apps/* so mobile cannot be discovered.
RUN printf '%s\n' \
'packages:' \
' - "apps/web"' \
' - "packages/convex"' \
> pnpm-workspace.yaml
RUN --mount=type=cache,target=/root/.local/share/pnpm/store \
corepack enable pnpm && \
pnpm install --frozen-lockfile --filter @otunes/web...
# ============================================
# Stage 2: Build Next.js application in standalone mode
# ============================================
# Reuse the installed workspace — no need to re-copy node_modules from a blank image.
FROM dependencies AS builder
WORKDIR /app
# Overlay sources; nested node_modules must be dockerignored so they do not
# replace the install from the dependencies stage with host pnpm symlinks.
COPY apps/web ./apps/web
COPY packages/convex ./packages/convex
# Re-apply the install tree after source COPY so module resolution cannot be
# clobbered even if a nested node_modules slips into the build context.
COPY --from=dependencies /app/node_modules ./node_modules
COPY --from=dependencies /app/apps/web/node_modules ./apps/web/node_modules
COPY --from=dependencies /app/packages/convex/node_modules ./packages/convex/node_modules
ENV NODE_ENV=production
WORKDIR /app/apps/web
RUN --mount=type=cache,target=/app/apps/web/.next/cache \
corepack enable pnpm && pnpm build
# ============================================
# Stage 3: Convex functions, deployed to the bundled backend on boot
# ============================================
FROM node:${NODE_VERSION} AS convex-functions
WORKDIR /app
COPY package.json pnpm-lock.yaml .npmrc ./
COPY packages/convex/package.json ./packages/convex/
RUN printf '%s\n' \
'packages:' \
' - "packages/convex"' \
> pnpm-workspace.yaml
RUN --mount=type=cache,target=/root/.local/share/pnpm/store \
corepack enable pnpm && \
pnpm install --frozen-lockfile --prod --filter @otunes/convex...
COPY packages/convex ./packages/convex
# ============================================
# Stage 4: Run Convex and Next.js together
# ============================================
FROM ${CONVEX_BACKEND_IMAGE} AS runner
RUN apt-get update && \
apt-get install -y --no-install-recommends ffmpeg tini && \
rm -rf /var/lib/apt/lists/*
RUN useradd --system --user-group --home-dir /app --shell /usr/sbin/nologin otunes
COPY --from=node /usr/local/bin/node /opt/node/bin/node
COPY --from=convex-functions /app /opt/otunes/convex
COPY docker/generate-auth-keys.mjs /opt/otunes/generate-auth-keys.mjs
COPY --chmod=755 docker/entrypoint.sh /usr/local/bin/otunes-entrypoint
WORKDIR /app
COPY --from=builder --chown=otunes:otunes /app/apps/web/public ./apps/web/public
COPY --from=builder --chown=otunes:otunes /app/apps/web/.next/standalone ./
COPY --from=builder --chown=otunes:otunes /app/apps/web/.next/static ./apps/web/.next/static
RUN mkdir -p apps/web/.next /data && chown -R otunes:otunes apps/web/.next
EXPOSE 3000 3210
HEALTHCHECK --interval=10s --timeout=5s --start-period=60s \
CMD curl -fsS http://127.0.0.1:3210/version && curl -fsS -o /dev/null http://127.0.0.1:3000/signin
ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/otunes-entrypoint"]
CMD []