Spaces:
Sleeping
Sleeping
Upload 2 files
Browse filesFiles changed (2)
- Dockerfile+19-12
- Dockerfile.2Added+217
1239 unmodified lines1314151617181921 unmodified lines414243444546474810 unmodified lines5960616263646566677 unmodified lines75767778798081825 unmodified lines8889909192939424 unmodified lines1191201211221231241251261271281291301311328 unmodified lines14114214314414514614714865 unmodified lines214215216217# Optimized for Hugging Face Spaces (Single-Stage, Non-Root, Persistent Services)# Database Configuration: PostgreSQL (ledgersg_dev) + Redis (localhost:6379)FROM python:3.13-trixie9 unmodified linesENV APP_PORT=7860ENV XDG_RUNTIME_DIR=/tmp/runtime-userENV PATH="/home/user/.local/bin:/usr/local/bin:${PATH}"ENV PGDATA=/var/lib/postgresql/dataENV PGHOST=/tmpENV PGPORT=543221 unmodified lines# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# 3. Toolchain Installation (UV, Bun)# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# SECURITY NOTE: Downloading binaries from raw GitHub URLs is a supply-chain risk.# Ensure you trust the source 'nordeim/HF-Space'.RUN cd /usr/bin && \wget -q https://github.com/nordeim/HF-Space/raw/refs/heads/main/bun && \wget -q https://github.com/nordeim/HF-Space/raw/refs/heads/main/uv && \10 unmodified linespip install fastapi uvicorn httpx pydantic python-multipart sqlalchemy alembic aiofiles jinja2# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# 5. Node.js Installation (Corrected to LTS 20.x)# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Node 24.x does not exist. Using 20.x LTS for stability with Next.js/Tooling.RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \apt-get install -y nodejs && \rm -rf /var/lib/apt/lists/* && \7 unmodified linesuseradd -m -u 1000 -g user -d /home/user user && \echo "user ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/user && \chmod 0440 /etc/sudoers.d/user && \mkdir -p ${XDG_RUNTIME_DIR} /data /app /var/run/postgresql && \chown -R user:user ${XDG_RUNTIME_DIR} /data /app /var/run/postgresql# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# 7. Global NPM & Playwright5 unmodified lines# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# 8. Database & Server Bootstrap Scripts (Embedded)# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Create a minimal FastAPI app to satisfy Healthcheck & Keep-AliveRUN mkdir -p /app/core && \chown -R user:user /app24 unmodified linesecho "=== LedgerSG Development Space Starting ==="# Ensure Postgres data directory exists and is owned by usermkdir -p \$PGDATAchown -R user:user \$PGDATAchown -R user:user /var/run/postgresql# Initialize Postgres if not exists (as user)if [ ! -f "\$PGDATA/PG_VERSION" ]; thenecho "Initializing PostgreSQL cluster..."su - user -c "initdb -D \$PGDATA"# Configure PostgreSQL for password authentication8 unmodified linesPGHBA# Configure listen addressessed -i "s/#listen_addresses = 'localhost'/listen_addresses = 'localhost'/" \$PGDATA/postgresql.confecho "listen_addresses = 'localhost'" >> \$PGDATA/postgresql.conffi# Start Redis in background65 unmodified lines# Use Entrypoint to start DBs, then run the boot.py serverENTRYPOINT ["/usr/local/bin/entrypoint.sh"]CMD ["python", "/app/core/boot.py"]12349 unmodified lines14151617181920212221 unmodified lines44454647484910 unmodified lines60616263646566677 unmodified lines75767778798081828384855 unmodified lines91929394959624 unmodified lines1211221231241251261271281291301311321331341351361371381398 unmodified lines14814915015115215315415565 unmodified lines221222223224# LedgerSG API Dockerfile# Optimized for Hugging Face Spaces (Single-Stage, Non-Root, Persistent Services)# Database Configuration: PostgreSQL (ledgersg_dev) + Redis (localhost:6379)FROM python:3.13-trixie9 unmodified linesENV APP_PORT=7860ENV XDG_RUNTIME_DIR=/tmp/runtime-userENV PATH="/home/user/.local/bin:/usr/local/bin:${PATH}"# CRITICAL: Use /data for PostgreSQL (persistent & user-writable)ENV PGDATA=/data/postgresqlENV PGHOST=/tmpENV PGPORT=543221 unmodified lines# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# 3. Toolchain Installation (UV, Bun)# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββRUN cd /usr/bin && \wget -q https://github.com/nordeim/HF-Space/raw/refs/heads/main/bun && \wget -q https://github.com/nordeim/HF-Space/raw/refs/heads/main/uv && \10 unmodified linespip install fastapi uvicorn httpx pydantic python-multipart sqlalchemy alembic aiofiles jinja2# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# 5. Node.js Installation (LTS 20.x)# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββRUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \apt-get install -y nodejs && \rm -rf /var/lib/apt/lists/* && \7 unmodified linesuseradd -m -u 1000 -g user -d /home/user user && \echo "user ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/user && \chmod 0440 /etc/sudoers.d/user && \# Create ALL required directoriesmkdir -p ${XDG_RUNTIME_DIR} /data /app /var/run/postgresql /data/postgresql && \# CRITICAL FIX: Own ALL postgres-related directorieschown -R user:user ${XDG_RUNTIME_DIR} /data /app /var/run/postgresql /var/lib/postgresql && \chmod 700 /data/postgresql# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# 7. Global NPM & Playwright5 unmodified lines# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# 8. Database & Server Bootstrap Scripts (Embedded)# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββRUN mkdir -p /app/core && \chown -R user:user /app24 unmodified linesecho "=== LedgerSG Development Space Starting ==="# Ensure PostgreSQL data directory exists and is owned by usermkdir -p \$PGDATAchown -R user:user \$PGDATAchmod 700 \$PGDATA# Ensure PostgreSQL socket directory is accessiblemkdir -p /var/run/postgresqlchown -R user:user /var/run/postgresqlchmod 777 /var/run/postgresql# Initialize Postgres if not exists (as user)if [ ! -f "\$PGDATA/PG_VERSION" ]; thenecho "Initializing PostgreSQL cluster at \$PGDATA..."su - user -c "initdb -D \$PGDATA"# Configure PostgreSQL for password authentication8 unmodified linesPGHBA# Configure listen addressesecho "listen_addresses = 'localhost'" >> \$PGDATA/postgresql.confecho "port = 5432" >> \$PGDATA/postgresql.conffi# Start Redis in background65 unmodified lines# Use Entrypoint to start DBs, then run the boot.py serverENTRYPOINT ["/usr/local/bin/entrypoint.sh"]CMD ["python", "/app/core/boot.py"]
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217# Optimized for Hugging Face Spaces (Single-Stage, Non-Root, Persistent Services)# Database Configuration: PostgreSQL (ledgersg_dev) + Redis (localhost:6379)FROM python:3.13-trixie
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# 1. Environment Configuration# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββENV PYTHONDONTWRITEBYTECODE=1ENV PYTHONUNBUFFERED=1ENV PIP_NO_CACHE_DIR=1ENV PIP_DISABLE_PIP_VERSION_CHECK=1ENV DEBIAN_FRONTEND=noninteractiveENV APP_PORT=7860ENV XDG_RUNTIME_DIR=/tmp/runtime-userENV PATH="/home/user/.local/bin:/usr/local/bin:${PATH}"ENV PGDATA=/var/lib/postgresql/dataENV PGHOST=/tmpENV PGPORT=5432
# Database Configuration (from .env)ENV DB_NAME=ledgersg_devENV DB_USER=ledgersgENV DB_PASSWORD=ledgersg_secret_to_changeENV DB_HOST=localhostENV DB_PORT=5432ENV REDIS_URL=redis://localhost:6379/0
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# 2. System Dependencies (Database + Dev Tools)# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββRUN apt-get update && apt-get install -y \ bash coreutils ca-certificates cron curl wget git less procps sudo vim tar zip unzip tmux openssh-client rsync \ build-essential gcc gnupg cmake pkg-config \ libpq-dev libjson-c-dev libssl-dev libwebsockets-dev \ libpango-1.0-0 libpangoft2-1.0-0 libharfbuzz0b libffi-dev libjpeg-dev libopenjp2-7-dev \ # Database Servers postgresql postgresql-contrib redis-server \ # Cleanup && apt-get clean && rm -rf /var/lib/apt/lists/*
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# 3. Toolchain Installation (UV, Bun)# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# SECURITY NOTE: Downloading binaries from raw GitHub URLs is a supply-chain risk.# Ensure you trust the source 'nordeim/HF-Space'.RUN cd /usr/bin && \ wget -q https://github.com/nordeim/HF-Space/raw/refs/heads/main/bun && \ wget -q https://github.com/nordeim/HF-Space/raw/refs/heads/main/uv && \ wget -q https://github.com/nordeim/HF-Space/raw/refs/heads/main/uvx && \ chmod a+x /usr/bin/bun /usr/bin/uv*
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# 4. Python Dependencies# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββRUN pip install --upgrade pip && \ pip install django-celery-beat && \ pip install -U django djangorestframework djangorestframework-simplejwt django-cors-headers django-filter && \ pip install psycopg[binary] celery[redis] redis py-moneyed pydantic weasyprint lxml python-decouple whitenoise gunicorn structlog sentry-sdk[django] pytest pytest-django pytest-cov pytest-xdist model-bakery factory-boy faker httpx ruff mypy django-stubs djangorestframework-stubs pre-commit ipython django-debug-toolbar django-extensions && \ pip install fastapi uvicorn httpx pydantic python-multipart sqlalchemy alembic aiofiles jinja2
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# 5. Node.js Installation (Corrected to LTS 20.x)# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Node 24.x does not exist. Using 20.x LTS for stability with Next.js/Tooling.RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \ apt-get install -y nodejs && \ rm -rf /var/lib/apt/lists/* && \ node --version && \ npm --version
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# 6. User & Permission Setup (Hugging Face Requirement)# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββRUN groupadd -g 1000 user && \ useradd -m -u 1000 -g user -d /home/user user && \ echo "user ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/user && \ chmod 0440 /etc/sudoers.d/user && \ mkdir -p ${XDG_RUNTIME_DIR} /data /app /var/run/postgresql && \ chown -R user:user ${XDG_RUNTIME_DIR} /data /app /var/run/postgresql
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# 7. Global NPM & Playwright# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββRUN npm install -g --omit=dev pnpm@latest vite@latest vitest@latest && \ npx playwright install chromium && \ npx playwright install-deps chromium
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# 8. Database & Server Bootstrap Scripts (Embedded)# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Create a minimal FastAPI app to satisfy Healthcheck & Keep-AliveRUN mkdir -p /app/core && \ chown -R user:user /app
COPY <<EOF /app/core/boot.pyfrom fastapi import FastAPIimport uvicornimport os
app = FastAPI()
@app.get("/")def read_root(): return {"status": "alive", "service": "LedgerSG Dev Space"}
@app.get("/health")def health(): return {"status": "healthy"}
if __name__ == "__main__": port = int(os.getenv("APP_PORT", 7860)) uvicorn.run(app, host="0.0.0.0", port=port)EOF
# Create Entrypoint Script to manage Postgres, Redis, and Web ServerCOPY <<EOF /usr/local/bin/entrypoint.sh#!/bin/bashset -e
echo "=== LedgerSG Development Space Starting ==="
# Ensure Postgres data directory exists and is owned by usermkdir -p \$PGDATAchown -R user:user \$PGDATAchown -R user:user /var/run/postgresql
# Initialize Postgres if not exists (as user)if [ ! -f "\$PGDATA/PG_VERSION" ]; then echo "Initializing PostgreSQL cluster..." su - user -c "initdb -D \$PGDATA" # Configure PostgreSQL for password authentication echo "Configuring PostgreSQL authentication..." # Allow local connections with password cat >> \$PGDATA/pg_hba.conf << 'PGHBA'# TYPE DATABASE USER ADDRESS METHODlocal all all trusthost all all 127.0.0.1/32 md5host all all ::1/128 md5PGHBA
# Configure listen addresses sed -i "s/#listen_addresses = 'localhost'/listen_addresses = 'localhost'/" \$PGDATA/postgresql.conf echo "listen_addresses = 'localhost'" >> \$PGDATA/postgresql.conffi
# Start Redis in backgroundecho "Starting Redis on port 6379..."redis-server --daemonize yes
# Start Postgres in background (as user)echo "Starting PostgreSQL on port 5432..."su - user -c "pg_ctl -D \$PGDATA -l /tmp/postgres.log start"
# Wait for Postgres to be readyecho "Waiting for PostgreSQL to accept connections..."for i in {1..30}; do if su - user -c "pg_isready -h localhost -p 5432" > /dev/null 2>&1; then echo "β PostgreSQL is ready" break fi if [ \$i -eq 30 ]; then echo "β PostgreSQL failed to start within 30 seconds" cat /tmp/postgres.log exit 1 fi sleep 1done
# Create database user and database if not existsecho "Setting up database user and database..."su - user -c "psql -h localhost -p 5432 -d postgres -c \"SELECT 1 FROM pg_roles WHERE rolname='\$DB_USER'\" | grep -q 1 || psql -h localhost -p 5432 -d postgres -c \"CREATE ROLE \$DB_USER WITH LOGIN PASSWORD '\$DB_PASSWORD';\""
su - user -c "psql -h localhost -p 5432 -d postgres -c \"SELECT 1 FROM pg_database WHERE datname='\$DB_NAME'\" | grep -q 1 || psql -h localhost -p 5432 -d postgres -c \"CREATE DATABASE \$DB_NAME OWNER \$DB_USER;\""
# Grant all privilegessu - user -c "psql -h localhost -p 5432 -d \$DB_NAME -c \"GRANT ALL PRIVILEGES ON DATABASE \$DB_NAME TO \$DB_USER;\""
echo "β Database setup complete: \$DB_NAME (user: \$DB_USER)"
# Verify Redis is runningecho "Verifying Redis connection..."if redis-cli ping > /dev/null 2>&1; then echo "β Redis is ready on localhost:6379"else echo "β Redis failed to start" exit 1fi
# Execute the main command (Web Server)echo "=== Starting Web Server on port \$APP_PORT ==="exec "\$@"EOF
RUN chmod +x /usr/local/bin/entrypoint.sh && \ chown -R user:user /app/core
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# 9. Finalization# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββUSER userWORKDIR /appCOPY --chown=user:user . /app
# Ensure .env file exists (create empty if not provided)RUN touch /app/.env && chown user:user /app/.env
EXPOSE ${APP_PORT}
# Healthcheck against the boot.py serverHEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \ CMD curl -f http://localhost:${APP_PORT}/health || exit 1
# Use Entrypoint to start DBs, then run the boot.py serverENTRYPOINT ["/usr/local/bin/entrypoint.sh"]CMD ["python", "/app/core/boot.py"]Dockerfile+19-12
Dockerfile.2Added+217