File size: 4,044 Bytes
08aeccf e7a94f7 08aeccf e7a94f7 08aeccf e7a94f7 08aeccf e7a94f7 08aeccf e7a94f7 08aeccf e7a94f7 08aeccf f308308 08aeccf e7a94f7 08aeccf e7a94f7 08aeccf e7a94f7 08aeccf e7a94f7 08aeccf e7a94f7 f0c6f87 08aeccf e7a94f7 f308308 08aeccf f308308 08aeccf e7a94f7 08aeccf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# 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
# Create user first (HF Spaces requirement)
RUN useradd -m -u 1000 user
# Install system dependencies
RUN apt-get update && apt-get install -y \
curl \
git \
gcc \
python3-dev \
libgmp-dev \
libmpfr-dev \
libmpc-dev \
nodejs \
npm \
postgresql \
postgresql-contrib \
&& rm -rf /var/lib/apt/lists/* \
&& pip install --no-cache-dir "poetry==${POETRY_VERSION}"
# Set up PostgreSQL directories and permissions
RUN mkdir -p /var/run/postgresql /var/lib/postgresql/data /var/log/postgresql && \
chown -R user:user /var/run/postgresql /var/lib/postgresql/data /var/log/postgresql && \
chmod 700 /var/lib/postgresql/data
# Create application directories
RUN mkdir -p /app/api /app/web /data/storage && \
chown -R user:user /app /data && \
chmod 777 /data /app
# Switch to user for remaining operations
USER user
# Set environment for user
ENV HOME=/home/user \
PATH=/home/user/.local/bin:$PATH
# Pull official images
FROM langgenius/dify-web:latest AS web
FROM langgenius/dify-api:latest AS api
# Final stage
FROM base
# Set up directory structure
WORKDIR /app
RUN mkdir -p api web /data/storage
# Copy from official images
COPY --from=web --chown=user:user /app/web /app/web/
COPY --from=api --chown=user:user /app/api /app/api/
# Install API dependencies using Poetry
WORKDIR /app/api
COPY --from=api --chown=user /app/api/pyproject.toml /app/api/poetry.lock /app/api/poetry.toml ./
RUN poetry install --no-root --no-dev
# Create symlink for persistent storage
RUN ln -s /data/storage /app/api/storage
# Set environment variables
ENV FLASK_APP=app.py \
EDITION=SELF_HOSTED \
DEPLOY_ENV=PRODUCTION \
MODE=api \
LOG_LEVEL=INFO \
DEBUG=false \
FLASK_DEBUG=false \
SECRET_KEY=sk-9f73s3ljTXVcMT3Blb3ljTqtsKiGHXVcMT3BlbkFJLK7U \
CONSOLE_API_URL=https://${SPACE_ID}.hf.space \
CONSOLE_WEB_URL=https://${SPACE_ID}.hf.space \
SERVICE_API_URL=https://${SPACE_ID}.hf.space \
APP_WEB_URL=https://${SPACE_ID}.hf.space \
DIFY_PORT=7860 \
DIFY_BIND_ADDRESS=0.0.0.0 \
DB_USERNAME=user \
DB_PASSWORD=difyai123456 \
DB_HOST=localhost \
DB_PORT=5432 \
DB_DATABASE=dify \
PYTHONPATH=/app/api \
STORAGE_PATH=/data/storage
EXPOSE 7860
# Create startup script
RUN echo '#!/bin/bash\n\
echo "===== Application Startup at $(date "+%Y-%m-%d %H:%M:%S") ====="\n\
\n\
# Initialize PostgreSQL database\n\
initdb -D $HOME/postgresql/data\n\
\n\
# Configure PostgreSQL\n\
echo "host all all 0.0.0.0/0 md5" >> $HOME/postgresql/data/pg_hba.conf\n\
echo "listen_addresses='\''*'\''" >> $HOME/postgresql/data/postgresql.conf\n\
\n\
# Start PostgreSQL\n\
pg_ctl -D $HOME/postgresql/data -l $HOME/postgresql/logfile start\n\
\n\
# Wait for PostgreSQL to start\n\
max_tries=30\n\
count=0\n\
echo "Checking database connection..."\n\
until psql -h localhost -p 5432 -U user -d postgres -c "SELECT 1" > /dev/null 2>&1; do\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\
exit 1\n\
fi\n\
done\n\
\n\
# Create database\n\
createdb ${DB_DATABASE}\n\
\n\
echo "Database connection successful"\n\
\n\
# Start application services\n\
cd /app/api && poetry run python -m flask db upgrade\n\
\n\
cd /app/api && poetry run python -m gunicorn app:app \
--bind ${DIFY_BIND_ADDRESS:-0.0.0.0}:${DIFY_PORT:-7860} \
--worker-class gevent \
--workers 1 \
--timeout 300 \
--preload &\n\
\n\
cd /app/web && node server.js &\n\
\n\
wait' > /app/entrypoint.sh && \
chmod +x /app/entrypoint.sh
WORKDIR /app
CMD ["./entrypoint.sh"] |