diff --git a/.dockerignore b/.dockerignore index 274ea4d..371b5e6 100644 --- a/.dockerignore +++ b/.dockerignore @@ -11,3 +11,6 @@ scripts/ *.pyc __pycache__/ .env + +# Ingore volumes created at run +postgres_data/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 242c1aa..f52141b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,8 @@ RUN apt-get update && \ libpq-dev \ nginx \ curl \ - postgresql-client \ + postgresql-15 \ + postgresql-client-15 \ supervisor \ gettext-base \ libcurl4-openssl-dev \ @@ -195,3 +196,12 @@ EOF # Start supervisor which will manage the setup, nginx, and gunicorn processes CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"] + +# Copy entrypoint script +COPY backend/entrypoint.sh /app/entrypoint.sh +RUN chmod +x /app/entrypoint.sh + +# Expose Postgres data directory as a volume for persistence +VOLUME ["/app/postgres_data"] + +ENTRYPOINT ["/app/entrypoint.sh"] diff --git a/backend/entrypoint.sh b/backend/entrypoint.sh new file mode 100644 index 0000000..5cd0c7a --- /dev/null +++ b/backend/entrypoint.sh @@ -0,0 +1,75 @@ +#!/bin/bash +set -e + +init_embedded_postgres() { + echo "[Entrypoint] Starting embedded Postgres..." + export PGDATA="/app/postgres_data" + export PGPORT=5432 + export DB_HOST=localhost + export DB_PORT=5432 + + if [ ! -f "$PGDATA/PG_VERSION" ]; then + echo "[Entrypoint] Initializing Postgres data directory at $PGDATA..." + mkdir -p "$PGDATA" + chown -R postgres:postgres "$PGDATA" + su postgres -c "/usr/lib/postgresql/15/bin/initdb -D $PGDATA" + fi + + echo "[Entrypoint] Launching Postgres server..." + su postgres -c "/usr/lib/postgresql/15/bin/pg_ctl -D $PGDATA -l $PGDATA/postgres.log start" + + # Wait for Postgres to be ready + for i in {1..20}; do + if pg_isready -h localhost -p 5432; then + echo "[Entrypoint] Postgres is ready!" + break + fi + echo "[Entrypoint] Waiting for Postgres to be ready... ($i)" + sleep 1 + done +} + +create_db_if_needed() { + su postgres -c "psql -tAc \"SELECT 1 FROM pg_database WHERE datname = '$DB_NAME'\" | grep -q 1 || psql -c 'CREATE DATABASE \"$DB_NAME\";'" +} + +create_user_if_needed() { + cat > /tmp/create_user.sql <