fix(docker): run the full stack in compose — gitgram backend+web healthy

The whole stack now comes up healthy under one `docker compose` run (gitgram
backend+web + Postgres/Redis + aitm-api/mcp/website + gitnexus); only the
optional tools-profile context7 remains (pre-existing workspace-build issue).

gitgram/backend/Dockerfile: move to a glibc base (node:20-bookworm-slim). Alpine
musl lacks the glibc loader that @tensorflow/tfjs-node's native libtensorflow
needs (ERR_DLOPEN_FAILED), so NSFW detection now runs for real (no stubbing).
Full install WITH lifecycle scripts in the builder (downloads libtensorflow),
node_modules reused in the runtime stage so devDeps like swagger-ui-express are
present for the NODE_ENV=development compose run; openssl + ffmpeg + libgomp1
runtime libs; create writable /app/logs + /app/uploads for the non-root user.
prisma schema: binaryTargets -> debian/arm64 openssl-3 for the glibc runtime.
nsfwDetectionService: lazy-load tf + nsfwjs so a genuinely-unavailable native
lib degrades gracefully instead of crashing at import (still uses real tfjs).

gitgram/web/Dockerfile: npm ci -> npm install (workspace member's standalone
lockfile drifts after dependency additions).

Verified end-to-end through containers: register/login/post/feed 201/200,
web homepage + GitHub->Gitgram /caelum0x both 200.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
caelum0x 2026-06-29 04:04:38 +03:00
parent 3408f0c873
commit 2bbcceb77b
4 changed files with 87 additions and 49 deletions

View File

@ -1,61 +1,64 @@
# Build stage
FROM node:20-alpine AS builder
# syntax=docker/dockerfile:1
#
# gitgram backend — production image on a glibc (Debian) base.
#
# A glibc base (not Alpine/musl) is required because @tensorflow/tfjs-node ships
# prebuilt native binaries that link the glibc dynamic loader (ld-linux) and
# libtensorflow; on Alpine they fail to dlopen. node:20-bookworm-slim provides
# the loader + shared libs, so NSFW detection runs for real (no stubbing).
# ── Build stage ──────────────────────────────────────────────────────────────
FROM node:20-bookworm-slim AS builder
WORKDIR /app
# Copy package files
COPY package*.json ./
COPY tsconfig.json ./
# Toolchain for native modules + Prisma/openssl + tfjs postinstall (curl/ca-certs
# to download libtensorflow, python3/make/g++ for node-gyp builds).
RUN apt-get update && apt-get install -y --no-install-recommends \
ca-certificates curl openssl python3 make g++ \
&& rm -rf /var/lib/apt/lists/*
# Install dependencies
RUN npm ci
COPY package.json package-lock.json* tsconfig.json ./
# Full install WITH lifecycle scripts so @tensorflow/tfjs-node downloads its
# native libtensorflow binary for this platform.
RUN npm install --no-audit --no-fund
# Copy source code
COPY . .
# Generate Prisma client
RUN npx prisma generate
# Build TypeScript
RUN npm run build
# Production stage
FROM node:20-alpine
# ── Production stage ─────────────────────────────────────────────────────────
FROM node:20-bookworm-slim AS runner
WORKDIR /app
# Install ffmpeg (provides the ffmpeg + ffprobe binaries) so videoService can
# extract real video frames for thumbnails and transcode media. Without it,
# thumbnail generation degrades to the branded placeholder fallback. Installing
# it here makes the real-frame path the norm in production.
RUN apk add --no-cache ffmpeg
# Runtime libs: ffmpeg/ffprobe for video thumbnails, openssl for Prisma,
# libgomp1 for tfjs-node's OpenMP runtime.
RUN apt-get update && apt-get install -y --no-install-recommends \
ca-certificates ffmpeg openssl libgomp1 \
&& rm -rf /var/lib/apt/lists/*
# Copy package files
COPY package*.json ./
ENV NODE_ENV=production
ENV PORT=4000
# Install production dependencies only
RUN npm ci --only=production
# Copy built files from builder
# Reuse the builder's fully-installed node_modules (tfjs native binary + devDeps
# such as swagger-ui-express that the NODE_ENV=development compose run enables),
# plus the compiled output and Prisma client/schema.
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/prisma ./prisma
COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma
# Create non-root user
RUN addgroup -g 1001 -S nodejs && \
adduser -S nodejs -u 1001
# Change ownership
RUN chown -R nodejs:nodejs /app
COPY --from=builder /app/package.json ./package.json
# Non-root user + writable runtime dirs (winston file transport writes /app/logs).
RUN groupadd -g 1001 nodejs && useradd -u 1001 -g nodejs -m nodejs \
&& mkdir -p /app/logs /app/uploads && chown -R nodejs:nodejs /app
USER nodejs
ENV PORT=4000
EXPOSE 4000
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
HEALTHCHECK --interval=30s --timeout=3s --start-period=15s --retries=3 \
CMD node -e "require('http').get('http://localhost:' + (process.env.PORT || 4000) + '/health', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})"
CMD ["node", "dist/index.js"]

View File

@ -1,7 +1,10 @@
// NEW SCHEMA - Repository-as-Account Architecture
generator client {
provider = "prisma-client-js"
provider = "prisma-client-js"
// "native" covers local dev; the debian/arm64 openssl-3 targets cover the
// node:20-bookworm-slim (glibc) Docker runtime on x64 and arm64 respectively.
binaryTargets = ["native", "debian-openssl-3.0.x", "linux-arm64-openssl-3.0.x"]
}
datasource db {

View File

@ -1,24 +1,53 @@
import * as tf from '@tensorflow/tfjs-node';
import * as nsfwjs from 'nsfwjs';
import logger from '../utils/logger';
let model: any = null;
// @tensorflow/tfjs-node ships prebuilt NATIVE (glibc) binaries that are not
// available on every runtime (e.g. Alpine/musl images or some arm64 containers).
// Importing it at module load therefore crashes the whole server on those hosts.
// Load tf + nsfwjs LAZILY so an unavailable native lib degrades NSFW detection to
// a safe no-op (treat as not-NSFW) instead of taking the process down.
type TfModule = typeof import('@tensorflow/tfjs-node');
type NsfwModel = Awaited<ReturnType<typeof import('nsfwjs')['load']>>;
export const loadModel = async () => {
if (!model) {
let tf: TfModule | null = null;
let model: NsfwModel | null = null;
let unavailable = false;
const ensureEngine = async (): Promise<{ tf: TfModule; model: NsfwModel } | null> => {
if (unavailable) return null;
if (tf && model) return { tf, model };
try {
tf = await import('@tensorflow/tfjs-node');
const nsfwjs = await import('nsfwjs');
model = await nsfwjs.load();
return { tf, model };
} catch (error) {
unavailable = true;
logger.warn('NSFW detection disabled — @tensorflow/tfjs-node unavailable on this runtime', {
error: error instanceof Error ? error.message : String(error),
});
return null;
}
return model;
};
export const detectNSFW = async (imageBuffer: Buffer): Promise<{ isNSFW: boolean; predictions: any[] }> => {
export const loadModel = async (): Promise<NsfwModel | null> => {
const engine = await ensureEngine();
return engine?.model ?? null;
};
export const detectNSFW = async (
imageBuffer: Buffer,
): Promise<{ isNSFW: boolean; predictions: Array<{ className: string; probability: number }> }> => {
try {
const nsfwModel = await loadModel();
const image = tf.node.decodeImage(imageBuffer, 3);
const predictions = await nsfwModel.classify(image);
const engine = await ensureEngine();
if (!engine) {
return { isNSFW: false, predictions: [] };
}
const image = engine.tf.node.decodeImage(imageBuffer, 3);
const predictions = await engine.model.classify(image);
image.dispose();
const nsfwScore = predictions.find((p: any) => ['Porn', 'Hentai', 'Sexy'].includes(p.className))?.probability || 0;
const nsfwScore =
predictions.find((p) => ['Porn', 'Hentai', 'Sexy'].includes(p.className))?.probability || 0;
const isNSFW = nsfwScore > 0.6;
return { isNSFW, predictions };

View File

@ -4,7 +4,10 @@ FROM node:20-alpine AS base
FROM base AS deps
WORKDIR /app
COPY package.json package-lock.json* ./
RUN npm ci --ignore-scripts
# Use `npm install` (not `npm ci`) — gitgram/web is an npm-workspace member, so its
# standalone lockfile can drift from package.json; install tolerates that and resolves
# the full dependency tree for the isolated image build.
RUN npm install --ignore-scripts --no-audit --no-fund
# --- Builder ---
FROM base AS builder