Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ scripts/
*.pyc
__pycache__/
.env

# Ingore volumes created at run
postgres_data/
12 changes: 11 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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"]
75 changes: 75 additions & 0 deletions backend/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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 <<EOF
DO \$\$
BEGIN
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '$DB_USER') THEN
EXECUTE format('CREATE ROLE %I WITH LOGIN PASSWORD %L', '$DB_USER', '$DB_PASSWORD');
END IF;
END
\$\$;
EOF
su postgres -c "psql -f /tmp/create_user.sql"
rm /tmp/create_user.sql
}

set_db_ownership_and_privileges() {
su postgres -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE \"$DB_NAME\" TO \"$DB_USER\";\""
su postgres -c "psql -d \"$DB_NAME\" -c 'ALTER DATABASE \"$DB_NAME\" OWNER TO \"$DB_USER\";'"
su postgres -c "psql -d \"$DB_NAME\" -c 'ALTER SCHEMA public OWNER TO \"$DB_USER\";'"
su postgres -c "psql -d \"$DB_NAME\" -c 'GRANT ALL ON SCHEMA public TO \"$DB_USER\";'"
}

# --- Main Script ---

if [ "$USE_INTERNAL_DB" = "true" ]; then
init_embedded_postgres
create_db_if_needed
create_user_if_needed
set_db_ownership_and_privileges
fi

python /app/migrations/apply_migrations.py

if [ "$USE_INTERNAL_DB" != "true" ]; then
python /app/fix_permissions.py
else
echo "[Entrypoint] Skipping fix_permissions.py for embedded Postgres."
fi

cd /app && python -c "import subprocess; subprocess.run([\"pybabel\", \"compile\", \"-d\", \"locales\"], check=False)"
exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
14 changes: 10 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3'

services:
warracker:
build: .
Expand All @@ -8,13 +6,18 @@ services:
volumes:
- ./uploads:/data/uploads
- ./backend/migrations:/app/migrations
# Uncomment the next line to persist embedded Postgres data (when using USE_INTERNAL_DB=true)
# - ./postgres_data:/app/postgres_data
environment:
- DB_HOST=warrackerdb
- DB_NAME=warranty_test
- DB_USER=warranty_user
- DB_PASSWORD=${DB_PASSWORD:-warranty_password}
- DB_ADMIN_USER=warracker_admin
- DB_ADMIN_PASSWORD=${DB_ADMIN_PASSWORD:-change_this_password_in_production}
# To use embedded Postgres, uncomment the next line:
# - USE_INTERNAL_DB=true
# When using embedded Postgres, DB_HOST/DB_PORT are ignored and set automatically
- SMTP_HOST=${SMTP_HOST:-localhost}
- SMTP_PORT=${SMTP_PORT:-1025}
- SMTP_USERNAME=${SMTP_USERNAME:-notifications@warracker.com}
Expand All @@ -40,6 +43,7 @@ services:
- WARRACKER_MEMORY_MODE=${WARRACKER_MEMORY_MODE:-optimized} # Options: optimized (default), ultra-light, performance
- MAX_UPLOAD_MB=${MAX_UPLOAD_MB:-16} # Reduced from 32MB default for memory efficiency
- NGINX_MAX_BODY_SIZE_VALUE=${NGINX_MAX_BODY_SIZE_VALUE:-16M} # Match upload limit
# If using external DB, keep depends_on
depends_on:
warrackerdb:
condition: service_healthy
Expand All @@ -48,12 +52,14 @@ services:
bash -c "
exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
"

# 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/init.sql:/docker-entrypoint-initdb.d/init.sql
- ./backend/migrations:/docker-entrypoint-initdb.d
environment:
- POSTGRES_DB=warranty_test
- POSTGRES_USER=warranty_user
Expand Down