Severian commited on
Commit
26f09f6
·
verified ·
1 Parent(s): 9b546e3

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +1 -99
Dockerfile CHANGED
@@ -1,99 +1 @@
1
- # Base Python image with correct version
2
- FROM python:3.12-slim-bookworm
3
-
4
- # Create non-root user early (HF requirement)
5
- RUN useradd -m -u 1000 user
6
-
7
- # Set up environment variables
8
- ENV PYTHONDONTWRITEBYTECODE=1 \
9
- POETRY_VERSION=1.8.4 \
10
- POETRY_HOME=/opt/poetry \
11
- POETRY_CACHE_DIR=/tmp/poetry_cache \
12
- POETRY_NO_INTERACTION=1 \
13
- POETRY_VIRTUALENVS_IN_PROJECT=true \
14
- POETRY_VIRTUALENVS_CREATE=true \
15
- POETRY_REQUESTS_TIMEOUT=15 \
16
- PYTHONPATH=/app
17
-
18
- WORKDIR /app
19
-
20
- # Install system dependencies
21
- RUN pip install --no-cache-dir "poetry==${POETRY_VERSION}" && \
22
- apt-get update && \
23
- apt-get install -y --no-install-recommends \
24
- gcc g++ libc-dev libffi-dev libgmp-dev libmpfr-dev libmpc-dev \
25
- postgresql postgresql-contrib postgresql-server-dev-all \
26
- curl git nodejs npm && \
27
- rm -rf /var/lib/apt/lists/*
28
-
29
- # Copy application code
30
- COPY . /app/api
31
- WORKDIR /app/api
32
-
33
- # Install Python dependencies
34
- RUN pip install --no-cache-dir flask==3.0.1 \
35
- gunicorn==22.0.0 \
36
- gevent==24.11.1 \
37
- celery==5.4.0 \
38
- redis==5.0.3 \
39
- psycopg2-binary==2.9.6 \
40
- sqlalchemy==2.0.29 \
41
- flask-migrate==4.0.5 \
42
- flask-sqlalchemy==3.1.1
43
-
44
- # Create and set up entrypoint script
45
- RUN echo '#!/bin/bash\n\
46
- set -e\n\
47
- \n\
48
- if [[ "${MIGRATION_ENABLED}" == "true" ]]; then\n\
49
- echo "Running migrations"\n\
50
- cd /app/api && flask db upgrade\n\
51
- fi\n\
52
- \n\
53
- if [[ "${DEBUG}" == "true" ]]; then\n\
54
- exec flask run --host=${DIFY_BIND_ADDRESS:-0.0.0.0} --port=7860 --debug\n\
55
- else\n\
56
- exec gunicorn \\\n\
57
- --bind "0.0.0.0:7860" \\\n\
58
- --workers ${SERVER_WORKER_AMOUNT:-1} \\\n\
59
- --worker-class ${SERVER_WORKER_CLASS:-gevent} \\\n\
60
- --timeout ${GUNICORN_TIMEOUT:-200} \\\n\
61
- --preload \\\n\
62
- app:app\n\
63
- fi' > /entrypoint.sh && \
64
- chmod +x /entrypoint.sh && \
65
- chown user:user /entrypoint.sh
66
-
67
- # Set up directories and permissions
68
- RUN mkdir -p /var/run/postgresql /var/lib/postgresql/data /data/storage && \
69
- chown -R postgres:postgres /var/run/postgresql /var/lib/postgresql/data && \
70
- chmod 2777 /var/run/postgresql && \
71
- chmod 700 /var/lib/postgresql/data && \
72
- chown -R user:user /app
73
-
74
- # Switch to user
75
- USER user
76
-
77
- # Set up user environment (HF requirement)
78
- ENV HOME=/home/user \
79
- PATH=/home/user/.local/bin:$PATH
80
-
81
- # Set required environment variables
82
- ENV FLASK_APP=/app/api/app.py \
83
- EDITION=SELF_HOSTED \
84
- DEPLOY_ENV=PRODUCTION \
85
- MODE=api \
86
- DB_USERNAME=postgres \
87
- DB_PASSWORD=difyai123456 \
88
- DB_HOST=localhost \
89
- DB_PORT=5432 \
90
- DB_DATABASE=dify \
91
- MIGRATION_ENABLED=true
92
-
93
- # Expose HF required port
94
- EXPOSE 7860
95
-
96
- WORKDIR /app/api
97
-
98
- ENTRYPOINT ["/bin/bash", "/entrypoint.sh"]
99
-
 
1
+ # Base Python image with correct versionFROM python:3.12-slim-bookworm AS base# Set shared environment variablesENV 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 users firstRUN useradd -m -u 1000 user# Install system dependencies and set up directoriesRUN apt-get update && apt-get install -y \ postgresql \ && rm -rf /var/lib/apt/lists/* \ && mkdir -p /var/run/postgresql /var/lib/postgresql/data \ && chown postgres:postgres /var/run/postgresql /var/lib/postgresql/data \ && chmod 2777 /var/run/postgresql \ && chmod 700 /var/lib/postgresql/data# Create application directoriesRUN mkdir -p /app/api /app/web /data/storage && \ chown -R user:user /app /data && \ chmod 777 /data /app && \ chown -R postgres:postgres /var/lib/postgresql/data && \ chmod 700 /var/lib/postgresql/data# Install remaining system dependenciesRUN 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}"# Initialize PostgreSQL database as postgres userUSER postgresRUN /usr/lib/postgresql/15/bin/initdb -D /var/lib/postgresql/data && \ echo "host all all 0.0.0.0/0 md5" >> /var/lib/postgresql/data/pg_hba.conf && \ echo "listen_addresses='*'" >> /var/lib/postgresql/data/postgresql.conf && \ echo "unix_socket_directories = '/var/run/postgresql'" >> /var/lib/postgresql/data/postgresql.conf# Switch to user for remaining operationsUSER user# Set environment for userENV HOME=/home/user \ PATH=/home/user/.local/bin:$PATH# Pull official imagesFROM langgenius/dify-web:latest AS webFROM langgenius/dify-api:latest AS api# Final stage (continuing from line 32)FROM base# Set up directory structureWORKDIR /appRUN mkdir -p api web /data/storage && \ chown -R user:user /app /data# Copy from official images with correct ownershipCOPY --from=web --chown=user:user /app/web /app/web/COPY --from=api --chown=user:user /app/api /app/api/# Install API dependencies using PoetryWORKDIR /app/apiCOPY --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 storageRUN ln -s /data/storage /app/api/storage# Set environment variablesENV 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=http://127.0.0.1:7860 \ CONSOLE_WEB_URL=http://127.0.0.1:3000 \ SERVICE_API_URL=http://127.0.0.1:7860 \ APP_WEB_URL=http://127.0.0.1:3000 \ DIFY_PORT=7860 \ DIFY_BIND_ADDRESS=0.0.0.0 \ DB_USERNAME=postgres \ DB_PASSWORD=difyai123456 \ DB_HOST=localhost \ DB_PORT=5432 \ DB_DATABASE=dify \ REDIS_HOST=localhost \ REDIS_PORT=6379 \ REDIS_PASSWORD=difyai123456 \ CELERY_BROKER_URL=amqp://guest:guest@localhost:5672// \ CELERY_RESULT_BACKEND=redis://localhost:6379/0 \ PYTHONPATH=/app/api \ STORAGE_PATH=/data/storageEXPOSE 7860 3000# Create startup script with connection retriesRUN echo '#!/bin/bash\n\echo "===== Application Startup at $(date "+%Y-%m-%d %H:%M:%S") ====="\n\\n\# Start PostgreSQL directly as postgres user\n\su postgres -c "/usr/lib/postgresql/15/bin/pg_ctl -D /var/lib/postgresql/data -l /var/log/postgresql/postgresql.log start"\n\\n\max_tries=30\n\count=0\n\echo "Checking database connection..."\n\until PGPASSWORD=$DB_PASSWORD psql -h localhost -p 5432 -U postgres -c "SELECT 1" > /dev/null 2>&1; do\n\ echo "Waiting for database connection..."\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 and user if they dont exist\n\PGPASSWORD=$DB_PASSWORD psql -h localhost -U postgres -c "CREATE DATABASE $DB_DATABASE;" || true\n\PGPASSWORD=$DB_PASSWORD psql -h localhost -U postgres -c "CREATE USER $DB_USERNAME WITH PASSWORD '\''$DB_PASSWORD'\'';" || true\n\PGPASSWORD=$DB_PASSWORD psql -h localhost -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE $DB_DATABASE TO $DB_USERNAME;" || true\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.shWORKDIR /appCMD ["./entrypoint.sh"]