Bundle FastAPI backend into Next.js Docker container
The Next.js app was proxying /api/* to the old Flask/FastAPI server at legal-ai.nautilus.marcusgroup.org. When that server went down, the Next.js app's API calls failed with 503. Now both services run in the same container: - FastAPI (uvicorn) on :8000 — the API backend - Next.js (node) on :3000 — proxies /api/* to localhost:8000 Changes: - Dockerfile: multi-stage build with Python 3.12 + Node.js - next.config.ts: default proxy target is now 127.0.0.1:8000 - start.sh: launches uvicorn in background + node in foreground - pyproject.toml: add fastapi + uvicorn as explicit deps Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -4,3 +4,12 @@ mcp-server/.venv/
|
|||||||
**/__pycache__/
|
**/__pycache__/
|
||||||
*.pyc
|
*.pyc
|
||||||
.git/
|
.git/
|
||||||
|
.taskmaster/
|
||||||
|
web/static/
|
||||||
|
web/__pycache__/
|
||||||
|
scripts/
|
||||||
|
skills/
|
||||||
|
docs/
|
||||||
|
legacy/
|
||||||
|
node_modules/
|
||||||
|
.next/
|
||||||
|
|||||||
47
Dockerfile
47
Dockerfile
@@ -1,21 +1,20 @@
|
|||||||
# ══════════════════════════════════════════════════════════════
|
# ══════════════════════════════════════════════════════════════
|
||||||
# Dockerfile — Next.js 16 web-ui (ui-rewrite branch only)
|
# Dockerfile — Next.js frontend + FastAPI backend (single container)
|
||||||
#
|
#
|
||||||
# This file REPLACES the FastAPI Dockerfile on this branch so that
|
# The container runs both:
|
||||||
# Coolify's default /Dockerfile lookup builds the new Next.js staging
|
# - FastAPI (uvicorn) on :8000 — the API backend
|
||||||
# UI. The FastAPI Dockerfile lives on `main` and is unaffected.
|
# - Next.js (node) on :3000 — the frontend (proxies /api/* to :8000)
|
||||||
#
|
#
|
||||||
# When the rewrite is merged to main, decide between:
|
# start.sh launches both processes.
|
||||||
# (a) keeping both via separate Dockerfiles + dockerfile_location config, or
|
|
||||||
# (b) a multi-stage Dockerfile that serves both, or
|
|
||||||
# (c) fully replacing FastAPI's StaticFiles with this Next.js front end.
|
|
||||||
# ══════════════════════════════════════════════════════════════
|
# ══════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
|
# ── Stage 1: Node deps ────────────────────────────────────────
|
||||||
FROM node:20-alpine AS deps
|
FROM node:20-alpine AS deps
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY web-ui/package.json web-ui/package-lock.json ./
|
COPY web-ui/package.json web-ui/package-lock.json ./
|
||||||
RUN npm ci --no-audit --no-fund
|
RUN npm ci --no-audit --no-fund
|
||||||
|
|
||||||
|
# ── Stage 2: Build Next.js ────────────────────────────────────
|
||||||
FROM node:20-alpine AS builder
|
FROM node:20-alpine AS builder
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY --from=deps /app/node_modules ./node_modules
|
COPY --from=deps /app/node_modules ./node_modules
|
||||||
@@ -23,18 +22,44 @@ COPY web-ui/ ./
|
|||||||
ENV NEXT_TELEMETRY_DISABLED=1
|
ENV NEXT_TELEMETRY_DISABLED=1
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
|
||||||
FROM node:20-alpine AS runner
|
# ── Stage 3: Install Python deps ─────────────────────────────
|
||||||
|
FROM python:3.12-alpine AS pydeps
|
||||||
|
WORKDIR /opt/api
|
||||||
|
COPY mcp-server/ ./mcp-server/
|
||||||
|
RUN pip install --no-cache-dir ./mcp-server
|
||||||
|
|
||||||
|
# ── Stage 4: Runner ───────────────────────────────────────────
|
||||||
|
FROM python:3.12-alpine AS runner
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Install Node.js (needed for Next.js standalone server)
|
||||||
|
RUN apk add --no-cache nodejs
|
||||||
|
|
||||||
ENV NODE_ENV=production
|
ENV NODE_ENV=production
|
||||||
ENV NEXT_TELEMETRY_DISABLED=1
|
ENV NEXT_TELEMETRY_DISABLED=1
|
||||||
ENV PORT=3000
|
ENV PORT=3000
|
||||||
ENV HOSTNAME=0.0.0.0
|
ENV HOSTNAME=0.0.0.0
|
||||||
|
|
||||||
# next.config.ts uses output: 'standalone', so we copy only the minimal runtime
|
# Copy Python packages from pydeps stage
|
||||||
|
COPY --from=pydeps /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
|
||||||
|
COPY --from=pydeps /usr/local/bin/uvicorn /usr/local/bin/uvicorn
|
||||||
|
|
||||||
|
# Copy Next.js standalone build
|
||||||
COPY --from=builder /app/public ./public
|
COPY --from=builder /app/public ./public
|
||||||
COPY --from=builder /app/.next/standalone ./
|
COPY --from=builder /app/.next/standalone ./
|
||||||
COPY --from=builder /app/.next/static ./.next/static
|
COPY --from=builder /app/.next/static ./.next/static
|
||||||
|
|
||||||
|
# Copy FastAPI backend code
|
||||||
|
COPY web/ ./web/
|
||||||
|
COPY mcp-server/src/ ./mcp-server/src/
|
||||||
|
|
||||||
|
# Make mcp-server source available to web/app.py (it does sys.path.insert for legal_mcp)
|
||||||
|
ENV PYTHONPATH=/app/mcp-server/src
|
||||||
|
|
||||||
|
# Copy startup script
|
||||||
|
COPY start.sh ./start.sh
|
||||||
|
RUN chmod +x ./start.sh
|
||||||
|
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
|
||||||
CMD ["node", "server.js"]
|
CMD ["./start.sh"]
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ dependencies = [
|
|||||||
"rq>=1.16.0",
|
"rq>=1.16.0",
|
||||||
"pillow>=10.0.0",
|
"pillow>=10.0.0",
|
||||||
"google-cloud-vision>=3.7.0",
|
"google-cloud-vision>=3.7.0",
|
||||||
|
"fastapi>=0.115.0",
|
||||||
|
"uvicorn[standard]>=0.30.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[build-system]
|
[build-system]
|
||||||
|
|||||||
13
start.sh
Executable file
13
start.sh
Executable file
@@ -0,0 +1,13 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Start FastAPI backend + Next.js frontend in the same container.
|
||||||
|
# Uvicorn runs in the background; Node.js runs in the foreground
|
||||||
|
# so that Docker gets its exit signal from the primary process.
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "Starting FastAPI backend on :8000 ..."
|
||||||
|
uvicorn web.app:app --host 127.0.0.1 --port 8000 --workers 1 &
|
||||||
|
UVICORN_PID=$!
|
||||||
|
|
||||||
|
echo "Starting Next.js frontend on :3000 ..."
|
||||||
|
exec node server.js
|
||||||
@@ -1,17 +1,14 @@
|
|||||||
import type { NextConfig } from "next";
|
import type { NextConfig } from "next";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Staging config — proxies /api/* and /openapi.json to the production FastAPI
|
* Proxies /api/* and /openapi.json to the FastAPI backend.
|
||||||
* at legal-ai.nautilus.marcusgroup.org. This lets the new Next.js UI call the
|
* In Docker both processes run in the same container, so the default
|
||||||
* existing backend without CORS and without running a second FastAPI instance.
|
* target is http://127.0.0.1:8000. Override with NEXT_PUBLIC_API_ORIGIN
|
||||||
*
|
* if the backend lives elsewhere (e.g. during local dev).
|
||||||
* When the rewrite branch is cut over to production, set NEXT_PUBLIC_API_BASE_URL
|
|
||||||
* and/or move the FastAPI in front of this app via traefik routing.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const API_ORIGIN =
|
const API_ORIGIN =
|
||||||
process.env.NEXT_PUBLIC_API_ORIGIN ??
|
process.env.NEXT_PUBLIC_API_ORIGIN ?? "http://127.0.0.1:8000";
|
||||||
"https://legal-ai.nautilus.marcusgroup.org";
|
|
||||||
|
|
||||||
const nextConfig: NextConfig = {
|
const nextConfig: NextConfig = {
|
||||||
output: "standalone",
|
output: "standalone",
|
||||||
|
|||||||
Reference in New Issue
Block a user