aki-008
commited on
Commit
Β·
531c962
1
Parent(s):
f6e5ff7
refactor: migrate to external render postgres database
Browse files- Dockerfile +4 -9
- start.sh +9 -61
Dockerfile
CHANGED
|
@@ -9,19 +9,15 @@ RUN npm run build
|
|
| 9 |
# --- Stage 2: Runtime ---
|
| 10 |
FROM python:3.12-slim
|
| 11 |
|
| 12 |
-
# Install system dependencies
|
|
|
|
| 13 |
RUN apt-get update && apt-get install -y \
|
| 14 |
-
nginx
|
| 15 |
build-essential portaudio19-dev libpq-dev git \
|
| 16 |
&& rm -rf /var/lib/apt/lists/*
|
| 17 |
|
| 18 |
-
# Add PostgreSQL bin directory to PATH for non-root user (adjust version if necessary)
|
| 19 |
-
ENV PATH="/usr/lib/postgresql/15/bin:$PATH"
|
| 20 |
-
|
| 21 |
# Setup non-root user for HF Spaces (UID 1000)
|
| 22 |
RUN useradd -m -u 1000 user
|
| 23 |
-
# Create a data directory for PostgreSQL owned by the non-root user
|
| 24 |
-
RUN mkdir -p /home/user/postgres_data && chown -R user:user /home/user/postgres_data
|
| 25 |
WORKDIR /home/user/app
|
| 26 |
|
| 27 |
# Copy Backend and install requirements
|
|
@@ -34,7 +30,6 @@ COPY --chown=user Backend/ ./Backend/
|
|
| 34 |
COPY --chown=user --from=build-stage /app/frontend/dist /usr/share/nginx/html
|
| 35 |
|
| 36 |
# Copy Configs
|
| 37 |
-
# FIX: Removed citation tags and extra slashes below
|
| 38 |
COPY --chown=user nginx.conf /etc/nginx/sites-available/default
|
| 39 |
COPY --chown=user start.sh ./start.sh
|
| 40 |
RUN chmod +x ./start.sh
|
|
@@ -43,7 +38,7 @@ RUN chmod +x ./start.sh
|
|
| 43 |
ENV PORT=7860
|
| 44 |
EXPOSE 7860
|
| 45 |
|
| 46 |
-
# Set the non-root user
|
| 47 |
USER user
|
| 48 |
|
| 49 |
# Run the startup script
|
|
|
|
| 9 |
# --- Stage 2: Runtime ---
|
| 10 |
FROM python:3.12-slim
|
| 11 |
|
| 12 |
+
# Install system dependencies
|
| 13 |
+
# Note: We keep libpq-dev for Python database libraries
|
| 14 |
RUN apt-get update && apt-get install -y \
|
| 15 |
+
nginx \
|
| 16 |
build-essential portaudio19-dev libpq-dev git \
|
| 17 |
&& rm -rf /var/lib/apt/lists/*
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
# Setup non-root user for HF Spaces (UID 1000)
|
| 20 |
RUN useradd -m -u 1000 user
|
|
|
|
|
|
|
| 21 |
WORKDIR /home/user/app
|
| 22 |
|
| 23 |
# Copy Backend and install requirements
|
|
|
|
| 30 |
COPY --chown=user --from=build-stage /app/frontend/dist /usr/share/nginx/html
|
| 31 |
|
| 32 |
# Copy Configs
|
|
|
|
| 33 |
COPY --chown=user nginx.conf /etc/nginx/sites-available/default
|
| 34 |
COPY --chown=user start.sh ./start.sh
|
| 35 |
RUN chmod +x ./start.sh
|
|
|
|
| 38 |
ENV PORT=7860
|
| 39 |
EXPOSE 7860
|
| 40 |
|
| 41 |
+
# Set the non-root user
|
| 42 |
USER user
|
| 43 |
|
| 44 |
# Run the startup script
|
start.sh
CHANGED
|
@@ -1,74 +1,20 @@
|
|
| 1 |
#!/bin/bash
|
|
|
|
| 2 |
|
| 3 |
-
# --- 1.
|
| 4 |
-
PG_BIN_DIR=$(find /usr/lib/postgresql -name pg_ctl | head -n 1 | xargs dirname)
|
| 5 |
-
if [ -z "$PG_BIN_DIR" ]; then
|
| 6 |
-
echo "β Error: Could not find PostgreSQL binaries."
|
| 7 |
-
exit 1
|
| 8 |
-
fi
|
| 9 |
-
echo "β
Found PostgreSQL binaries at $PG_BIN_DIR"
|
| 10 |
-
export PATH="$PG_BIN_DIR:$PATH"
|
| 11 |
-
|
| 12 |
-
# --- 2. Set Critical Environment Variables ---
|
| 13 |
-
export DATABASE_URL="postgresql+asyncpg://prepuser:password@127.0.0.1:5432/studentdb"
|
| 14 |
-
export PGDATA=/home/user/postgres_data
|
| 15 |
export HOME=/home/user
|
|
|
|
|
|
|
| 16 |
export chroma_host="127.0.0.1"
|
| 17 |
export chroma_port="8080"
|
| 18 |
export chroma_collection="prepai_collection"
|
| 19 |
|
| 20 |
-
# ---
|
| 21 |
-
if [ -d "$PGDATA" ] && [ ! -f "$PGDATA/PG_VERSION" ]; then
|
| 22 |
-
echo "β οΈ $PGDATA exists but is not a valid cluster. Wiping..."
|
| 23 |
-
rm -rf "$PGDATA"
|
| 24 |
-
fi
|
| 25 |
-
|
| 26 |
-
if [ ! -d "$PGDATA" ]; then
|
| 27 |
-
echo "βοΈ Initializing database..."
|
| 28 |
-
initdb -D "$PGDATA" --auth-local=trust --no-locale --encoding=UTF8
|
| 29 |
-
fi
|
| 30 |
-
|
| 31 |
-
# --- 4. Configure & Start PostgreSQL ---
|
| 32 |
-
# FIX: Explicitly set the socket directory in the config file to avoid /var/run permissions issues
|
| 33 |
-
if ! grep -q "unix_socket_directories" "$PGDATA/postgresql.conf"; then
|
| 34 |
-
echo "unix_socket_directories = '/tmp'" >> "$PGDATA/postgresql.conf"
|
| 35 |
-
fi
|
| 36 |
-
|
| 37 |
-
# Remove any stale lock files from previous crashes
|
| 38 |
-
rm -f "$PGDATA/postmaster.pid"
|
| 39 |
-
|
| 40 |
-
echo "π Starting PostgreSQL..."
|
| 41 |
-
# Try to start. If it fails, we continue (no set -e) so we can print logs.
|
| 42 |
-
pg_ctl -D "$PGDATA" -l /home/user/postgres.log start
|
| 43 |
-
|
| 44 |
-
# Wait a moment for startup...
|
| 45 |
-
sleep 3
|
| 46 |
-
|
| 47 |
-
# Check if it is actually running
|
| 48 |
-
if ! pg_isready -h 127.0.0.1 -p 5432; then
|
| 49 |
-
echo "β PostgreSQL failed to start. Printing contents of postgres.log:"
|
| 50 |
-
echo "----------------------------------------------------------------"
|
| 51 |
-
cat /home/user/postgres.log
|
| 52 |
-
echo "----------------------------------------------------------------"
|
| 53 |
-
exit 1
|
| 54 |
-
fi
|
| 55 |
-
|
| 56 |
-
echo "β
PostgreSQL is up and accepting connections!"
|
| 57 |
-
|
| 58 |
-
# --- 5. User & DB Setup ---
|
| 59 |
-
echo "π οΈ Configuring Database..."
|
| 60 |
-
psql -h 127.0.0.1 -d postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='prepuser'" | grep -q 1 || \
|
| 61 |
-
psql -h 127.0.0.1 -d postgres -c "CREATE USER prepuser WITH PASSWORD 'password';"
|
| 62 |
-
|
| 63 |
-
psql -h 127.0.0.1 -d postgres -tAc "SELECT 1 FROM pg_database WHERE datname='studentdb'" | grep -q 1 || \
|
| 64 |
-
createdb -h 127.0.0.1 -O prepuser studentdb
|
| 65 |
-
|
| 66 |
-
# --- 6. ChromaDB Setup ---
|
| 67 |
echo "π¨ Setting up ChromaDB..."
|
| 68 |
mkdir -p ./chroma_store
|
| 69 |
chroma run --host 0.0.0.0 --port 8080 --path ./chroma_store &
|
| 70 |
|
| 71 |
-
# ---
|
| 72 |
echo "π Starting Nginx..."
|
| 73 |
mkdir -p /tmp/nginx/body /tmp/nginx/proxy /tmp/nginx/fastcgi /tmp/nginx/uwsgi /tmp/nginx/scgi
|
| 74 |
|
|
@@ -107,7 +53,9 @@ EOF
|
|
| 107 |
|
| 108 |
nginx -c /tmp/nginx.conf &
|
| 109 |
|
| 110 |
-
# ---
|
| 111 |
echo "π Starting Backend..."
|
| 112 |
cd Backend
|
|
|
|
|
|
|
| 113 |
python run.py
|
|
|
|
| 1 |
#!/bin/bash
|
| 2 |
+
set -e
|
| 3 |
|
| 4 |
+
# --- 1. Set Environment Variables ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
export HOME=/home/user
|
| 6 |
+
# Use the DATABASE_URL from Hugging Face Secrets
|
| 7 |
+
# ChromaDB settings
|
| 8 |
export chroma_host="127.0.0.1"
|
| 9 |
export chroma_port="8080"
|
| 10 |
export chroma_collection="prepai_collection"
|
| 11 |
|
| 12 |
+
# --- 2. Start ChromaDB ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
echo "π¨ Setting up ChromaDB..."
|
| 14 |
mkdir -p ./chroma_store
|
| 15 |
chroma run --host 0.0.0.0 --port 8080 --path ./chroma_store &
|
| 16 |
|
| 17 |
+
# --- 3. Start Nginx (Non-root Mode) ---
|
| 18 |
echo "π Starting Nginx..."
|
| 19 |
mkdir -p /tmp/nginx/body /tmp/nginx/proxy /tmp/nginx/fastcgi /tmp/nginx/uwsgi /tmp/nginx/scgi
|
| 20 |
|
|
|
|
| 53 |
|
| 54 |
nginx -c /tmp/nginx.conf &
|
| 55 |
|
| 56 |
+
# --- 4. Start Backend ---
|
| 57 |
echo "π Starting Backend..."
|
| 58 |
cd Backend
|
| 59 |
+
# Ensure tables are created in the new remote DB
|
| 60 |
+
# (Your main.py lifespan event handles this via Base.metadata.create_all)
|
| 61 |
python run.py
|