Files
otunes/Dockerfile

221 lines
7.6 KiB
Docker

# syntax=docker/dockerfile:1
# ============================================
# 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/convex/convex-backend:5c7cb5bc7db457290f1769f95f1d1340912f7fd7
# Audio-only static ffmpeg. The distro package links video filters that pull
# in LLVM and Mesa (~420MB) for codecs this app never uses.
ARG FFMPEG_VERSION=7.1.5
ARG FFMPEG_SHA256=de668509caf9e35e3cd162473441fdb29538c6d96ed080292b3cf9e6fc5d558f
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 ./
# pnpm validates patches even when their packages are outside the install filter.
COPY patches ./patches
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 \
&& printf '\n%s\n' \
'supportedArchitectures.os=current' \
'supportedArchitectures.cpu=current' \
'supportedArchitectures.libc=current' \
>> .npmrc
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. Copying node_modules into a new layer retars
# hundreds of thousands of files on every source change.
FROM dependencies AS builder
WORKDIR /app
COPY apps/web /tmp/src/apps/web
COPY packages/convex /tmp/src/packages/convex
# Merge sources onto the install. cp does not remove the existing
# node_modules tree, and the build context already dockerignores it.
RUN cp -a /tmp/src/apps/web/. ./apps/web/ \
&& cp -a /tmp/src/packages/convex/. ./packages/convex/ \
&& rm -rf /tmp/src
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 patches ./patches
COPY packages/convex/package.json ./packages/convex/
RUN printf '%s\n' \
'packages:' \
' - "packages/convex"' \
> pnpm-workspace.yaml \
&& printf '\n%s\n' \
'supportedArchitectures.os=current' \
'supportedArchitectures.cpu=current' \
'supportedArchitectures.libc=current' \
>> .npmrc
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
# Source maps and the Convex package's TypeScript sources are not used at runtime.
RUN find /app -type f -name '*.map' -delete \
&& rm -rf /app/node_modules/.pnpm/convex@*/node_modules/convex/src
# ============================================
# Stage 4: Audio-only ffmpeg
# ============================================
FROM ubuntu:24.04 AS ffmpeg
ARG FFMPEG_VERSION
ARG FFMPEG_SHA256
RUN apt-get update && \
apt-get install -y --no-install-recommends \
build-essential ca-certificates curl nasm pkg-config xz-utils zlib1g-dev && \
rm -rf /var/lib/apt/lists/*
WORKDIR /src
RUN curl -fsSL -o ffmpeg.tar.xz "https://ffmpeg.org/releases/ffmpeg-${FFMPEG_VERSION}.tar.xz" \
&& echo "${FFMPEG_SHA256} ffmpeg.tar.xz" | sha256sum -c - \
&& tar -xJf ffmpeg.tar.xz --strip-components=1 \
&& rm ffmpeg.tar.xz
# Decoders cover the library formats (FLAC, ALAC/AAC in m4a, MP3, Opus,
# Vorbis, WavPack, WMA, E-AC-3/TrueHD for Atmos). Encoders are only the two
# the app writes: AAC (HLS and ADTS) and FLAC.
RUN ./configure \
--prefix=/opt/ffmpeg \
--disable-shared \
--enable-static \
--disable-debug \
--disable-doc \
--disable-ffplay \
--disable-ffprobe \
--disable-network \
--disable-autodetect \
--disable-everything \
--enable-ffmpeg \
--enable-protocol=file,pipe \
--enable-demuxer=aac,ac3,aiff,ape,asf,au,caf,dsf,flac,matroska,mov,mp3,ogg,wav,wv \
--enable-decoder=aac,aac_latm,ac3,alac,ape,dsd_lsbf,dsd_msbf,eac3,flac,mlp,mp3,opus,pcm_f32le,pcm_s16be,pcm_s16le,pcm_s24be,pcm_s24le,pcm_s32be,pcm_s32le,pcm_u8,truehd,vorbis,wavpack,wmav1,wmav2 \
--enable-encoder=aac,flac \
--enable-muxer=adts,flac,hls,mpegts \
--enable-parser=aac,aac_latm,ac3,flac,mpegaudio,opus,vorbis \
--enable-bsf=aac_adtstoasc \
--enable-filter=aresample,aformat \
--enable-zlib \
--extra-cflags="-Os" \
--extra-ldflags="-static" \
--pkg-config-flags="--static" \
&& make -j"$(nproc)" \
&& make install \
&& strip /opt/ffmpeg/bin/ffmpeg
# ============================================
# Stage 5: Run Convex and Next.js together
# ============================================
FROM ${CONVEX_BACKEND_IMAGE} AS convex-backend
# Fresh Ubuntu instead of the Convex image. That image installs libclang,
# which pulls LLVM (~200MB) even though the backend only links libstdc++.
FROM ubuntu:24.04 AS runner
RUN apt-get update && \
apt-get install -y --no-install-recommends ca-certificates curl openssl tini && \
rm -rf /var/lib/apt/lists/*
WORKDIR /convex
COPY --from=convex-backend --chmod=755 \
/convex/convex-local-backend \
/convex/generate_key \
/convex/run_backend.sh \
/convex/generate_admin_key.sh \
/convex/read_credentials.sh \
./
COPY --from=node --chmod=755 /usr/local/bin/node /usr/local/bin/node
COPY --from=ffmpeg --chmod=755 /opt/ffmpeg/bin/ffmpeg /usr/local/bin/ffmpeg
# Fail the build if a binary is missing a shared library.
RUN node --version \
&& ffmpeg -hide_banner -version \
&& openssl version \
&& command -v tini \
&& command -v curl \
&& ldd /convex/convex-local-backend \
&& ldd /convex/generate_key \
&& ! ldd /convex/convex-local-backend /convex/generate_key | grep -q 'not found'
RUN useradd --system --user-group --home-dir /app --shell /usr/sbin/nologin otunes
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
ENV PATH="/usr/local/bin:${PATH}"
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 []