Spaces:
Build error
Build error
Youngger9765 Claude Happy commited on
Commit ·
bda4756
1
Parent(s): b830e05
fix: optimize Docker build to reduce storage usage on HuggingFace
Browse files- Use multi-stage build with alpine images where possible
- Implement aggressive cleanup after each build stage
- Use Next.js standalone output mode
- Exclude unnecessary files via comprehensive .dockerignore
- Copy only runtime files (no tests, docs, or dev files)
- Clean all package manager caches
- Fixes 'storage limit exceeded (50G)' error
Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)
Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
- .dockerignore +90 -10
- Dockerfile +58 -27
.dockerignore
CHANGED
|
@@ -1,14 +1,94 @@
|
|
| 1 |
-
|
| 2 |
-
.next
|
| 3 |
.git
|
| 4 |
.gitignore
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
README.md
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
*.log
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
.
|
| 13 |
-
.
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Version control
|
|
|
|
| 2 |
.git
|
| 3 |
.gitignore
|
| 4 |
+
|
| 5 |
+
# Python
|
| 6 |
+
__pycache__
|
| 7 |
+
*.py[cod]
|
| 8 |
+
*$py.class
|
| 9 |
+
*.so
|
| 10 |
+
.Python
|
| 11 |
+
env/
|
| 12 |
+
venv/
|
| 13 |
+
ENV/
|
| 14 |
+
.venv
|
| 15 |
+
.pytest_cache/
|
| 16 |
+
.coverage
|
| 17 |
+
htmlcov/
|
| 18 |
+
.tox/
|
| 19 |
+
.hypothesis/
|
| 20 |
+
*.egg-info/
|
| 21 |
+
dist/
|
| 22 |
+
build/
|
| 23 |
+
|
| 24 |
+
# Node
|
| 25 |
+
node_modules/
|
| 26 |
+
npm-debug.log*
|
| 27 |
+
yarn-debug.log*
|
| 28 |
+
yarn-error.log*
|
| 29 |
+
|
| 30 |
+
# Next.js
|
| 31 |
+
.next/
|
| 32 |
+
out/
|
| 33 |
+
frontend/.next/
|
| 34 |
+
frontend/out/
|
| 35 |
+
|
| 36 |
+
# IDE
|
| 37 |
+
.vscode/
|
| 38 |
+
.idea/
|
| 39 |
+
*.swp
|
| 40 |
+
*.swo
|
| 41 |
+
.DS_Store
|
| 42 |
+
|
| 43 |
+
# Environment files
|
| 44 |
+
.env
|
| 45 |
+
.env.*
|
| 46 |
+
*.local
|
| 47 |
+
|
| 48 |
+
# Documentation
|
| 49 |
README.md
|
| 50 |
+
*.md
|
| 51 |
+
docs/
|
| 52 |
+
|
| 53 |
+
# Docker
|
| 54 |
+
Dockerfile*
|
| 55 |
+
docker-compose*
|
| 56 |
+
|
| 57 |
+
# Tests
|
| 58 |
+
tests/
|
| 59 |
+
backend/tests/
|
| 60 |
+
*.test.*
|
| 61 |
+
*.spec.*
|
| 62 |
+
|
| 63 |
+
# Logs
|
| 64 |
*.log
|
| 65 |
+
logs/
|
| 66 |
+
|
| 67 |
+
# Temporary files
|
| 68 |
+
*.tmp
|
| 69 |
+
*.temp
|
| 70 |
+
tmp/
|
| 71 |
+
temp/
|
| 72 |
+
|
| 73 |
+
# Database
|
| 74 |
+
*.db
|
| 75 |
+
*.sqlite
|
| 76 |
+
*.sqlite3
|
| 77 |
+
|
| 78 |
+
# Large files
|
| 79 |
+
*.pdf
|
| 80 |
+
*.zip
|
| 81 |
+
*.tar.gz
|
| 82 |
+
*.rar
|
| 83 |
+
|
| 84 |
+
# Development files
|
| 85 |
+
Makefile
|
| 86 |
+
pyproject.toml
|
| 87 |
+
.pre-commit-config.yaml
|
| 88 |
+
alembic.ini
|
| 89 |
+
migrations/
|
| 90 |
+
|
| 91 |
+
# Cache
|
| 92 |
+
.cache/
|
| 93 |
+
.npm/
|
| 94 |
+
.yarn/
|
Dockerfile
CHANGED
|
@@ -1,56 +1,87 @@
|
|
| 1 |
# Multi-stage Docker build for Next.js + FastAPI on Hugging Face Spaces
|
| 2 |
|
| 3 |
# Stage 1: Build Next.js frontend
|
| 4 |
-
FROM node:18-
|
| 5 |
|
| 6 |
WORKDIR /frontend
|
| 7 |
|
| 8 |
-
# Copy package files
|
| 9 |
-
COPY frontend/package
|
| 10 |
|
| 11 |
-
# Install dependencies
|
| 12 |
-
RUN npm
|
|
|
|
| 13 |
|
| 14 |
# Copy frontend source
|
| 15 |
COPY frontend/ ./
|
| 16 |
|
| 17 |
-
# Build Next.js app
|
| 18 |
-
RUN npm run build
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
FROM python:3.9-slim
|
| 22 |
|
| 23 |
# Create non-root user (required by Hugging Face)
|
| 24 |
-
RUN useradd -m -u 1000 user
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
USER user
|
| 26 |
-
ENV PATH="/home/user/.local/bin:$PATH"
|
| 27 |
-
|
| 28 |
WORKDIR /app
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
COPY --chown=user
|
| 32 |
-
|
| 33 |
|
| 34 |
-
# Copy
|
| 35 |
-
COPY --chown=user backend/ ./backend/
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
# Copy
|
| 38 |
-
COPY --from=frontend-builder --chown=user /frontend/.next ./frontend/
|
| 39 |
-
COPY --from=frontend-builder --chown=user /frontend/
|
| 40 |
-
COPY --from=frontend-builder --chown=user /frontend/node_modules ./frontend/node_modules
|
| 41 |
COPY --from=frontend-builder --chown=user /frontend/public ./frontend/public
|
| 42 |
-
|
| 43 |
-
# Install Node.js in final image for Next.js runtime
|
| 44 |
-
USER root
|
| 45 |
-
RUN apt-get update && apt-get install -y nodejs npm && rm -rf /var/lib/apt/lists/*
|
| 46 |
-
USER user
|
| 47 |
|
| 48 |
# Copy startup script
|
| 49 |
COPY --chown=user start.sh .
|
| 50 |
RUN chmod +x start.sh
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# Expose port 7860 (required by Hugging Face Spaces)
|
| 53 |
EXPOSE 7860
|
| 54 |
|
| 55 |
# Start both services
|
| 56 |
-
CMD ["./start.sh"]
|
|
|
|
| 1 |
# Multi-stage Docker build for Next.js + FastAPI on Hugging Face Spaces
|
| 2 |
|
| 3 |
# Stage 1: Build Next.js frontend
|
| 4 |
+
FROM node:18-alpine AS frontend-builder
|
| 5 |
|
| 6 |
WORKDIR /frontend
|
| 7 |
|
| 8 |
+
# Copy only package files first (better caching)
|
| 9 |
+
COPY frontend/package.json frontend/package-lock.json* ./
|
| 10 |
|
| 11 |
+
# Install dependencies with clean install and cache clean
|
| 12 |
+
RUN npm ci --only=production && \
|
| 13 |
+
npm cache clean --force
|
| 14 |
|
| 15 |
# Copy frontend source
|
| 16 |
COPY frontend/ ./
|
| 17 |
|
| 18 |
+
# Build Next.js app with standalone output
|
| 19 |
+
RUN npm run build && \
|
| 20 |
+
# Remove source files after build
|
| 21 |
+
rm -rf src/ components/ pages/ styles/ && \
|
| 22 |
+
# Clean npm cache
|
| 23 |
+
npm cache clean --force
|
| 24 |
+
|
| 25 |
+
# Stage 2: Python dependencies
|
| 26 |
+
FROM python:3.9-slim AS python-deps
|
| 27 |
+
|
| 28 |
+
# Install build dependencies
|
| 29 |
+
RUN apt-get update && \
|
| 30 |
+
apt-get install -y --no-install-recommends \
|
| 31 |
+
gcc \
|
| 32 |
+
g++ \
|
| 33 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 34 |
+
|
| 35 |
+
# Copy requirements and install Python packages
|
| 36 |
+
COPY requirements.txt .
|
| 37 |
+
RUN pip install --no-cache-dir --user -r requirements.txt && \
|
| 38 |
+
# Clean pip cache
|
| 39 |
+
rm -rf ~/.cache/pip
|
| 40 |
+
|
| 41 |
+
# Stage 3: Final runtime image (minimal)
|
| 42 |
FROM python:3.9-slim
|
| 43 |
|
| 44 |
# Create non-root user (required by Hugging Face)
|
| 45 |
+
RUN useradd -m -u 1000 user && \
|
| 46 |
+
# Install only runtime dependencies
|
| 47 |
+
apt-get update && \
|
| 48 |
+
apt-get install -y --no-install-recommends \
|
| 49 |
+
nodejs \
|
| 50 |
+
npm \
|
| 51 |
+
&& rm -rf /var/lib/apt/lists/* && \
|
| 52 |
+
apt-get clean
|
| 53 |
+
|
| 54 |
+
# Switch to user
|
| 55 |
USER user
|
|
|
|
|
|
|
| 56 |
WORKDIR /app
|
| 57 |
|
| 58 |
+
# Copy Python packages from deps stage
|
| 59 |
+
COPY --from=python-deps --chown=user /root/.local /home/user/.local
|
| 60 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 61 |
|
| 62 |
+
# Copy only necessary backend files (no tests, no migrations)
|
| 63 |
+
COPY --chown=user backend/api ./backend/api
|
| 64 |
+
COPY --chown=user backend/models ./backend/models
|
| 65 |
+
COPY --chown=user backend/services ./backend/services
|
| 66 |
+
COPY --chown=user backend/*.py ./backend/
|
| 67 |
|
| 68 |
+
# Copy minimal Next.js build from builder
|
| 69 |
+
COPY --from=frontend-builder --chown=user /frontend/.next/standalone ./frontend/
|
| 70 |
+
COPY --from=frontend-builder --chown=user /frontend/.next/static ./frontend/.next/static
|
|
|
|
| 71 |
COPY --from=frontend-builder --chown=user /frontend/public ./frontend/public
|
| 72 |
+
COPY --from=frontend-builder --chown=user /frontend/package.json ./frontend/
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
# Copy startup script
|
| 75 |
COPY --chown=user start.sh .
|
| 76 |
RUN chmod +x start.sh
|
| 77 |
|
| 78 |
+
# Set environment to reduce memory usage
|
| 79 |
+
ENV NODE_ENV=production
|
| 80 |
+
ENV PYTHONUNBUFFERED=1
|
| 81 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
| 82 |
+
|
| 83 |
# Expose port 7860 (required by Hugging Face Spaces)
|
| 84 |
EXPOSE 7860
|
| 85 |
|
| 86 |
# Start both services
|
| 87 |
+
CMD ["./start.sh"]
|