From 31f78b15c6db2679fb7f01c002e2b6f3ed60d984 Mon Sep 17 00:00:00 2001 From: Joshua Higgins Date: Thu, 8 Oct 2026 17:02:51 -0400 Subject: [PATCH] perf(docker): reduce runtime image size --- Dockerfile | 133 +++++++++++++++++++++++++++++++++++++------ docker/entrypoint.sh | 2 +- 2 files changed, 117 insertions(+), 18 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5b6818c..4a0fd4c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,5 @@ +# syntax=docker/dockerfile:1 + # ============================================ # Stage 1: Dependencies Installation Stage # ============================================ @@ -11,6 +13,11 @@ ARG NODE_VERSION=24.13.0-slim # https://github.com/get-convex/convex-backend/pkgs/container/convex-backend ARG CONVEX_BACKEND_IMAGE=ghcr.io/get-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 @@ -28,7 +35,12 @@ RUN printf '%s\n' \ 'packages:' \ ' - "apps/web"' \ ' - "packages/convex"' \ - > pnpm-workspace.yaml + >> 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 && \ @@ -38,21 +50,20 @@ RUN --mount=type=cache,target=/root/.local/share/pnpm/store \ # Stage 2: Build Next.js application in standalone mode # ============================================ -# Reuse the installed workspace — no need to re-copy node_modules from a blank image. +# 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 -# 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 +COPY apps/web /tmp/src/apps/web +COPY packages/convex /tmp/src/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 +# 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 @@ -75,7 +86,12 @@ COPY packages/convex/package.json ./packages/convex/ RUN printf '%s\n' \ 'packages:' \ ' - "packages/convex"' \ - > pnpm-workspace.yaml + > 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 && \ @@ -83,19 +99,100 @@ RUN --mount=type=cache,target=/root/.local/share/pnpm/store \ 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: Run Convex and Next.js together +# Stage 4: Audio-only ffmpeg # ============================================ -FROM ${CONVEX_BACKEND_IMAGE} AS runner +FROM ubuntu:24.04 AS ffmpeg + +ARG FFMPEG_VERSION +ARG FFMPEG_SHA256 RUN apt-get update && \ - apt-get install -y --no-install-recommends ffmpeg tini && \ + 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/* -RUN useradd --system --user-group --home-dir /app --shell /usr/sbin/nologin otunes +WORKDIR /src -COPY --from=node /usr/local/bin/node /opt/node/bin/node +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 @@ -109,6 +206,8 @@ COPY --from=builder --chown=otunes:otunes /app/apps/web/.next/static ./apps/web/ 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 \ diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 21c5c7a..c179357 100644 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -8,7 +8,7 @@ # the bundled functions, and never exported to the web server. set -euo pipefail -NODE=/opt/node/bin/node +NODE=/usr/local/bin/node CONVEX_DATA_DIR=/convex/data FUNCTIONS_DIR=/opt/otunes/convex/packages/convex BACKEND_URL=http://127.0.0.1:3210