From cc4b341b9e638a33bae332fb29227a2d342d4547 Mon Sep 17 00:00:00 2001 From: HamletDuFromage <61667930+HamletDuFromage@users.noreply.github.com> Date: Wed, 23 Jul 2025 13:30:05 +0200 Subject: [PATCH 1/2] add support for internal database --- Dockerfile | 12 ++++++++- backend/entrypoint.sh | 61 +++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 48 +++++++++++++++++++--------------- 3 files changed, 99 insertions(+), 22 deletions(-) create mode 100644 backend/entrypoint.sh 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..c4ae4bc --- /dev/null +++ b/backend/entrypoint.sh @@ -0,0 +1,61 @@ +#!/bin/bash +set -e + +if [ "$USE_INTERNAL_DB" = "true" ]; then + 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 not exist + 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 not exist (write SQL to temp file with variables expanded) + cat > /tmp/create_user.sql < bash -c " exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf " - - warrackerdb: - image: "postgres:15-alpine" - volumes: - - postgres_data:/var/lib/postgresql/data - - ./backend/init.sql:/docker-entrypoint-initdb.d/init.sql - environment: - - POSTGRES_DB=warranty_test - - POSTGRES_USER=warranty_user - - POSTGRES_PASSWORD=${DB_PASSWORD:-warranty_password} - restart: unless-stopped - healthcheck: - test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"] - interval: 10s - timeout: 5s - retries: 5 + # If using embedded DB, you can remove depends_on and command overrides + + # External Postgres service (optional, not needed if using embedded DB) +# warrackerdb: +# image: "postgres:15-alpine" +# volumes: +# - postgres_data:/var/lib/postgresql/data +# - ./backend/migrations:/docker-entrypoint-initdb.d +# environment: +# - POSTGRES_DB=warranty_test +# - POSTGRES_USER=warranty_user +# - POSTGRES_PASSWORD=${DB_PASSWORD:-warranty_password} +# restart: unless-stopped +# healthcheck: +# test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"] +# interval: 10s +# timeout: 5s +# retries: 5 volumes: postgres_data: From 3bc46468c629cc1a18279d49c1469b8d8cf96a60 Mon Sep 17 00:00:00 2001 From: HamletDuFromage <61667930+HamletDuFromage@users.noreply.github.com> Date: Wed, 23 Jul 2025 13:45:55 +0200 Subject: [PATCH 2/2] make external db the default --- .dockerignore | 3 +++ backend/entrypoint.sh | 30 ++++++++++++++++++++++-------- docker-compose.yml | 40 ++++++++++++++++++++-------------------- 3 files changed, 45 insertions(+), 28 deletions(-) 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/backend/entrypoint.sh b/backend/entrypoint.sh index c4ae4bc..5cd0c7a 100644 --- a/backend/entrypoint.sh +++ b/backend/entrypoint.sh @@ -1,20 +1,23 @@ #!/bin/bash set -e -if [ "$USE_INTERNAL_DB" = "true" ]; then +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 @@ -24,9 +27,13 @@ if [ "$USE_INTERNAL_DB" = "true" ]; then echo "[Entrypoint] Waiting for Postgres to be ready... ($i)" sleep 1 done - # Create DB if not exist +} + +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 not exist (write SQL to temp file with variables expanded) +} + +create_user_if_needed() { cat > /tmp/create_user.sql < bash -c " @@ -55,21 +55,21 @@ services: # If using embedded DB, you can remove depends_on and command overrides # External Postgres service (optional, not needed if using embedded DB) -# warrackerdb: -# image: "postgres:15-alpine" -# volumes: -# - postgres_data:/var/lib/postgresql/data -# - ./backend/migrations:/docker-entrypoint-initdb.d -# environment: -# - POSTGRES_DB=warranty_test -# - POSTGRES_USER=warranty_user -# - POSTGRES_PASSWORD=${DB_PASSWORD:-warranty_password} -# restart: unless-stopped -# healthcheck: -# test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"] -# interval: 10s -# timeout: 5s -# retries: 5 + warrackerdb: + image: "postgres:15-alpine" + volumes: + - postgres_data:/var/lib/postgresql/data + - ./backend/migrations:/docker-entrypoint-initdb.d + environment: + - POSTGRES_DB=warranty_test + - POSTGRES_USER=warranty_user + - POSTGRES_PASSWORD=${DB_PASSWORD:-warranty_password} + restart: unless-stopped + healthcheck: + test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"] + interval: 10s + timeout: 5s + retries: 5 volumes: postgres_data: