142 lines
4.1 KiB
Docker
142 lines
4.1 KiB
Docker
# Stage 1: Build Next.js frontend
|
|
FROM node:20-alpine AS frontend-builder
|
|
WORKDIR /app
|
|
|
|
# Install dependencies
|
|
COPY package.json package-lock.json* ./
|
|
RUN npm ci
|
|
|
|
# Copy source and build
|
|
COPY . .
|
|
RUN npm run build
|
|
|
|
# Stage 2: Build Python backend
|
|
FROM python:3.11-slim AS backend-builder
|
|
WORKDIR /backend
|
|
|
|
# Install dependencies
|
|
COPY backend/requirements.txt ./
|
|
RUN pip install --no-cache-dir -r requirements.txt
|
|
|
|
# Copy backend source
|
|
COPY backend/ ./
|
|
|
|
# Stage 3: Production image with nginx + supervisor
|
|
FROM python:3.11-slim AS runner
|
|
WORKDIR /app
|
|
|
|
# Install Node.js, nginx, and supervisor
|
|
RUN apt-get update && apt-get install -y \
|
|
curl \
|
|
nginx \
|
|
supervisor \
|
|
&& curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
|
|
&& apt-get install -y nodejs \
|
|
&& apt-get clean \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
# Create non-root user
|
|
RUN groupadd --system --gid 1001 appgroup \
|
|
&& useradd --system --uid 1001 --gid appgroup appuser
|
|
|
|
# Copy Next.js standalone build
|
|
COPY --from=frontend-builder /app/public ./public
|
|
COPY --from=frontend-builder /app/.next/standalone ./
|
|
COPY --from=frontend-builder /app/.next/static ./.next/static
|
|
|
|
# Copy Python backend
|
|
COPY --from=backend-builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
|
|
COPY --from=backend-builder /backend ./backend
|
|
|
|
# Copy data directory for prompts
|
|
COPY --from=frontend-builder /app/data ./data
|
|
|
|
# Create nginx config - reverse proxy /api to FastAPI
|
|
RUN cat > /etc/nginx/sites-available/default <<'EOF'
|
|
server {
|
|
listen 80;
|
|
server_name _;
|
|
|
|
# Proxy /api/* to FastAPI backend (strip /api prefix)
|
|
location /api/ {
|
|
proxy_pass http://127.0.0.1:8000/;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
proxy_set_header Connection 'upgrade';
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_cache_bypass $http_upgrade;
|
|
proxy_read_timeout 300s;
|
|
proxy_connect_timeout 75s;
|
|
}
|
|
|
|
# Proxy everything else to Next.js
|
|
location / {
|
|
proxy_pass http://127.0.0.1:3000;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
proxy_set_header Connection 'upgrade';
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_cache_bypass $http_upgrade;
|
|
}
|
|
}
|
|
EOF
|
|
|
|
# Create supervisor config
|
|
RUN mkdir -p /var/log/supervisor
|
|
COPY <<EOF /etc/supervisor/conf.d/supervisord.conf
|
|
[supervisord]
|
|
nodaemon=true
|
|
user=root
|
|
logfile=/var/log/supervisor/supervisord.log
|
|
pidfile=/var/run/supervisord.pid
|
|
|
|
[program:nginx]
|
|
command=/usr/sbin/nginx -g "daemon off;"
|
|
autostart=true
|
|
autorestart=true
|
|
stdout_logfile=/dev/stdout
|
|
stdout_logfile_maxbytes=0
|
|
stderr_logfile=/dev/stderr
|
|
stderr_logfile_maxbytes=0
|
|
|
|
[program:nextjs]
|
|
command=node /app/server.js
|
|
directory=/app
|
|
autostart=true
|
|
autorestart=true
|
|
stdout_logfile=/dev/stdout
|
|
stdout_logfile_maxbytes=0
|
|
stderr_logfile=/dev/stderr
|
|
stderr_logfile_maxbytes=0
|
|
environment=NODE_ENV=production,PORT=3000,HOSTNAME=0.0.0.0
|
|
|
|
[program:fastapi]
|
|
command=python -m uvicorn main:app --host 0.0.0.0 --port 8000
|
|
directory=/app/backend
|
|
autostart=true
|
|
autorestart=true
|
|
stdout_logfile=/dev/stdout
|
|
stdout_logfile_maxbytes=0
|
|
stderr_logfile=/dev/stderr
|
|
stderr_logfile_maxbytes=0
|
|
environment=API_ROOT_PATH="/api"
|
|
EOF
|
|
|
|
# Set permissions
|
|
RUN chown -R appuser:appgroup /app /var/log/supervisor
|
|
|
|
# Expose only port 80 (nginx) - internal ports 3000 and 8000 not needed externally
|
|
EXPOSE 80
|
|
|
|
# Health check via nginx
|
|
HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
|
|
CMD curl -f http://localhost/api/health || exit 1
|
|
|
|
# Run supervisor
|
|
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
|