Files
Zebratic 26fefd623a perf: pre-warm Gradle cache in Docker image
Run a dummy build during docker build to download all Gradle/Maven
dependencies. This cuts first user build from ~15 min to ~3-5 min.
Docker image is bigger but builds are dramatically faster.
2026-03-15 17:14:55 +00:00

115 lines
3.6 KiB
Docker

# ── Stage 1: Build Next.js app ──
FROM node:22-bookworm-slim AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci
FROM node:22-bookworm-slim AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ARG NEXT_PUBLIC_APP_URL
ENV NEXT_PUBLIC_APP_URL=$NEXT_PUBLIC_APP_URL
# Dummy values for build (real values injected at runtime)
ENV MONGODB_URI=mongodb://placeholder:27017/placeholder
ENV NEXTAUTH_SECRET=build-placeholder-secret-not-real
ENV LLM_API_KEY=build-placeholder
ENV LLM_API_URL=https://placeholder.com/v1
ENV LLM_MODEL=placeholder
RUN npm run build
# ── Stage 2: Final image with Next.js + Build Worker + Android SDK ──
FROM node:22-bookworm-slim AS runner
WORKDIR /app
ENV NODE_ENV=production
# Install Android SDK dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
openjdk-17-jdk-headless \
wget \
unzip \
supervisor \
&& rm -rf /var/lib/apt/lists/*
ENV JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
ENV ANDROID_HOME=/opt/android-sdk
ENV ANDROID_SDK_ROOT=/opt/android-sdk
ENV PATH="${ANDROID_HOME}/cmdline-tools/latest/bin:${ANDROID_HOME}/platform-tools:${PATH}"
# Install Android SDK
RUN mkdir -p ${ANDROID_HOME}/cmdline-tools && \
wget -q "https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip" -O /tmp/cmdline-tools.zip && \
unzip -q /tmp/cmdline-tools.zip -d ${ANDROID_HOME}/cmdline-tools && \
mv ${ANDROID_HOME}/cmdline-tools/cmdline-tools ${ANDROID_HOME}/cmdline-tools/latest && \
rm /tmp/cmdline-tools.zip && \
yes | sdkmanager --licenses > /dev/null 2>&1 && \
sdkmanager "platforms;android-34" "build-tools;34.0.0" "platform-tools"
# ── Next.js app ──
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
# Install ws for WebSocket support in the custom server
RUN cd /tmp && npm init -y > /dev/null 2>&1 && npm install ws && cp -r node_modules/ws /app/node_modules/ws
# Custom server with WebSocket support (replaces default server.js)
COPY server.custom.js /app/server.custom.js
# ── Build worker ──
COPY build-worker/server.js /worker/server.js
COPY build-worker/package.json /worker/package.json
COPY build-worker/template /worker/template
RUN cd /worker && npm install
# ── Pre-warm Gradle cache (saves ~10 min on first user build) ──
# Run a dummy build with the template to download all Gradle/Maven deps
RUN mkdir -p /tmp/warmup && cp -r /worker/template/* /tmp/warmup/ && \
cd /tmp/warmup && npm install --legacy-peer-deps && \
npx expo prebuild --platform android --no-install && \
cd /tmp/warmup/android && chmod +x gradlew && \
./gradlew assembleRelease --no-daemon \
-Dorg.gradle.jvmargs='-Xmx2g' || true && \
# Keep the Gradle cache, delete the project
rm -rf /tmp/warmup
# ── Supervisor config to run both processes ──
RUN mkdir -p /var/log/supervisor
COPY <<'EOF' /etc/supervisor/conf.d/app.conf
[supervisord]
nodaemon=true
logfile=/dev/null
logfile_maxbytes=0
[program:nextjs]
command=node /app/server.custom.js
directory=/app
autostart=true
autorestart=true
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
stderr_logfile=/dev/fd/2
stderr_logfile_maxbytes=0
[program:build-worker]
command=node /worker/server.js
directory=/worker
autostart=true
autorestart=true
environment=PORT=3001
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
stderr_logfile=/dev/fd/2
stderr_logfile_maxbytes=0
EOF
EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
ENV BUILD_WORKER_URL=http://localhost:3001
CMD ["supervisord", "-c", "/etc/supervisor/conf.d/app.conf"]