# Base image FROM python:3.12-slim-bookworm AS base # Set shared environment variables ENV POETRY_VERSION=1.8.4 \ POETRY_NO_INTERACTION=1 \ POETRY_VIRTUALENVS_CREATE=true \ POETRY_VIRTUALENVS_IN_PROJECT=true \ POETRY_CACHE_DIR=/tmp/poetry_cache \ PYTHONDONTWRITEBYTECODE=1 \ LANG=en_US.UTF-8 \ LANGUAGE=en_US:en \ LC_ALL=en_US.UTF-8 \ PORT=7860 \ NODE_ENV=production # Create user first (HF Spaces requirement) RUN useradd -m -u 1000 user # Install system dependencies and set up locales RUN apt-get update && apt-get install -y \ curl \ git \ gcc \ python3-dev \ libgmp-dev \ libmpfr-dev \ libmpc-dev \ nodejs \ npm \ postgresql \ postgresql-contrib \ locales \ nginx \ && rm -rf /var/lib/apt/lists/* \ && pip install --no-cache-dir "poetry==${POETRY_VERSION}" \ && sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen \ && locale-gen # Configure nginx RUN rm /etc/nginx/sites-enabled/default || true COPY < "$PGDATA/pg_hba.conf"\n\ echo "host all all 127.0.0.1/32 md5" >> "$PGDATA/pg_hba.conf"\n\ echo "host all all ::1/128 md5" >> "$PGDATA/pg_hba.conf"\n\ echo "host all all 0.0.0.0/0 md5" >> "$PGDATA/pg_hba.conf"\n\ \n\ echo "listen_addresses = '\''*'\''" >> "$PGDATA/postgresql.conf"\n\ echo "max_connections = 100" >> "$PGDATA/postgresql.conf"\n\ echo "shared_buffers = 128MB" >> "$PGDATA/postgresql.conf"\n\ fi\n\ \n\ # Start PostgreSQL with detailed logging\n\ echo "Starting PostgreSQL server..."\n\ pg_ctl start -D "$PGDATA" -l /var/log/postgresql/postgresql.log -o "-c logging_collector=on -c log_directory='\''/var/log/postgresql'\'' -c log_filename='\''postgresql-%Y-%m-%d_%H%M%S.log'\'' -c log_statement='\''all'\''" -w\n\ \n\ # Wait for PostgreSQL to start and show logs if there are issues\n\ max_tries=30\n\ count=0\n\ echo "Checking database connection..."\n\ until pg_isready -h localhost -p 5432; do\n\ if [ $count -eq 0 ]; then\n\ echo "PostgreSQL logs:"\n\ tail -n 50 /var/log/postgresql/postgresql.log\n\ fi\n\ echo "Waiting for database connection... (${count}/${max_tries})"\n\ sleep 2\n\ count=$((count+1))\n\ if [ $count -gt $max_tries ]; then\n\ echo "Failed to connect to database after ${max_tries} attempts"\n\ echo "Last 100 lines of PostgreSQL logs:"\n\ tail -n 100 /var/log/postgresql/postgresql.log\n\ exit 1\n\ fi\n\ done\n\ \n\ # Create database if it doesn'\''t exist\n\ if ! psql -lqt | cut -d \| -f 1 | grep -qw dify; then\n\ echo "Creating database dify..."\n\ createdb -U user dify\n\ fi\n\ \n\ echo "Database connection successful"\n\ \n\ # Start application services\n\ cd /app/api && poetry run python -m flask db upgrade\n\ \n\ # Start API server\n\ cd /app/api && poetry run python -m gunicorn app:app \\\n\ --bind ${DIFY_BIND_ADDRESS:-127.0.0.1}:${DIFY_PORT:-5001} \\\n\ --worker-class gevent \\\n\ --workers 1 \\\n\ --timeout 300 \\\n\ --preload &\n\ \n\ # Start frontend server\n\ cd /app/web && PORT=3000 node server.js &\n\ \n\ # Wait for services to be ready\n\ echo "Waiting for services to be ready..."\n\ sleep 5\n\ \n\ # Start nginx\n\ nginx -g "daemon off;" &\n\ \n\ wait' > /app/entrypoint.sh && \ chmod +x /app/entrypoint.sh WORKDIR /app CMD ["./entrypoint.sh"]