Severian commited on
Commit
08aeccf
·
verified ·
1 Parent(s): 26f09f6

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +159 -1
Dockerfile CHANGED
@@ -1 +1,159 @@
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"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Base image
2
+ FROM python:3.12-slim-bookworm AS base
3
+
4
+ # Set shared environment variables
5
+ ENV POETRY_VERSION=1.8.4 \
6
+ POETRY_NO_INTERACTION=1 \
7
+ POETRY_VIRTUALENVS_CREATE=true \
8
+ POETRY_VIRTUALENVS_IN_PROJECT=true \
9
+ POETRY_CACHE_DIR=/tmp/poetry_cache \
10
+ PYTHONDONTWRITEBYTECODE=1
11
+
12
+ # Create users first
13
+ RUN useradd -m -u 1000 user
14
+
15
+ # Install system dependencies and set up directories
16
+ RUN apt-get update && apt-get install -y \
17
+ postgresql \
18
+ && rm -rf /var/lib/apt/lists/* \
19
+ && mkdir -p /var/run/postgresql /var/lib/postgresql/data \
20
+ && chown postgres:postgres /var/run/postgresql /var/lib/postgresql/data \
21
+ && chmod 2777 /var/run/postgresql \
22
+ && chmod 700 /var/lib/postgresql/data
23
+
24
+ # Create application directories
25
+ RUN mkdir -p /app/api /app/web /data/storage && \
26
+ chown -R user:user /app /data && \
27
+ chmod 777 /data /app && \
28
+ chown -R postgres:postgres /var/lib/postgresql/data && \
29
+ chmod 700 /var/lib/postgresql/data
30
+
31
+ # Install remaining system dependencies
32
+ RUN apt-get update && apt-get install -y \
33
+ curl \
34
+ git \
35
+ gcc \
36
+ python3-dev \
37
+ libgmp-dev \
38
+ libmpfr-dev \
39
+ libmpc-dev \
40
+ nodejs \
41
+ npm \
42
+ postgresql \
43
+ postgresql-contrib \
44
+ && rm -rf /var/lib/apt/lists/* \
45
+ && pip install --no-cache-dir "poetry==${POETRY_VERSION}"
46
+
47
+ # Initialize PostgreSQL database as postgres user
48
+ USER postgres
49
+ RUN /usr/lib/postgresql/15/bin/initdb -D /var/lib/postgresql/data && \
50
+ echo "host all all 0.0.0.0/0 md5" >> /var/lib/postgresql/data/pg_hba.conf && \
51
+ echo "listen_addresses='*'" >> /var/lib/postgresql/data/postgresql.conf && \
52
+ echo "unix_socket_directories = '/var/run/postgresql'" >> /var/lib/postgresql/data/postgresql.conf
53
+
54
+ # Switch to user for remaining operations
55
+ USER user
56
+
57
+ # Set environment for user
58
+ ENV HOME=/home/user \
59
+ PATH=/home/user/.local/bin:$PATH
60
+
61
+ # Pull official images
62
+ FROM langgenius/dify-web:latest AS web
63
+ FROM langgenius/dify-api:latest AS api
64
+
65
+ # Final stage
66
+ FROM base
67
+
68
+ # Set up directory structure
69
+ WORKDIR /app
70
+ RUN mkdir -p api web /data/storage && \
71
+ chown -R user:user /app /data
72
+
73
+ # Copy from official images with correct ownership
74
+ COPY --from=web --chown=user:user /app/web /app/web/
75
+ COPY --from=api --chown=user:user /app/api /app/api/
76
+
77
+ # Install API dependencies using Poetry
78
+ WORKDIR /app/api
79
+ COPY --from=api --chown=user /app/api/pyproject.toml /app/api/poetry.lock /app/api/poetry.toml ./
80
+ RUN poetry install --no-root --no-dev
81
+
82
+ # Create symlink for persistent storage
83
+ RUN ln -s /data/storage /app/api/storage
84
+
85
+ # Set environment variables
86
+ ENV FLASK_APP=app.py \
87
+ EDITION=SELF_HOSTED \
88
+ DEPLOY_ENV=PRODUCTION \
89
+ MODE=api \
90
+ LOG_LEVEL=INFO \
91
+ DEBUG=false \
92
+ FLASK_DEBUG=false \
93
+ SECRET_KEY=sk-9f73s3ljTXVcMT3Blb3ljTqtsKiGHXVcMT3BlbkFJLK7U \
94
+ CONSOLE_API_URL=http://127.0.0.1:7860 \
95
+ CONSOLE_WEB_URL=http://127.0.0.1:3000 \
96
+ SERVICE_API_URL=http://127.0.0.1:7860 \
97
+ APP_WEB_URL=http://127.0.0.1:3000 \
98
+ DIFY_PORT=7860 \
99
+ DIFY_BIND_ADDRESS=0.0.0.0 \
100
+ DB_USERNAME=postgres \
101
+ DB_PASSWORD=difyai123456 \
102
+ DB_HOST=localhost \
103
+ DB_PORT=5432 \
104
+ DB_DATABASE=dify \
105
+ REDIS_HOST=localhost \
106
+ REDIS_PORT=6379 \
107
+ REDIS_PASSWORD=difyai123456 \
108
+ CELERY_BROKER_URL=amqp://guest:guest@localhost:5672// \
109
+ CELERY_RESULT_BACKEND=redis://localhost:6379/0 \
110
+ PYTHONPATH=/app/api \
111
+ STORAGE_PATH=/data/storage
112
+
113
+ EXPOSE 7860 3000
114
+
115
+ # Create startup script with connection retries
116
+ RUN echo '#!/bin/bash\n\
117
+ echo "===== Application Startup at $(date "+%Y-%m-%d %H:%M:%S") ====="\n\
118
+ \n\
119
+ # Start PostgreSQL directly as postgres user\n\
120
+ su postgres -c "/usr/lib/postgresql/15/bin/pg_ctl -D /var/lib/postgresql/data -l /var/log/postgresql/postgresql.log start"\n\
121
+ \n\
122
+ max_tries=30\n\
123
+ count=0\n\
124
+ echo "Checking database connection..."\n\
125
+ until PGPASSWORD=$DB_PASSWORD psql -h localhost -p 5432 -U postgres -c "SELECT 1" > /dev/null 2>&1; do\n\
126
+ echo "Waiting for database connection..."\n\
127
+ sleep 2\n\
128
+ count=$((count+1))\n\
129
+ if [ $count -gt $max_tries ]; then\n\
130
+ echo "Failed to connect to database after $max_tries attempts"\n\
131
+ exit 1\n\
132
+ fi\n\
133
+ done\n\
134
+ \n\
135
+ # Create database and user if they dont exist\n\
136
+ PGPASSWORD=$DB_PASSWORD psql -h localhost -U postgres -c "CREATE DATABASE $DB_DATABASE;" || true\n\
137
+ PGPASSWORD=$DB_PASSWORD psql -h localhost -U postgres -c "CREATE USER $DB_USERNAME WITH PASSWORD '\''$DB_PASSWORD'\'';" || true\n\
138
+ PGPASSWORD=$DB_PASSWORD psql -h localhost -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE $DB_DATABASE TO $DB_USERNAME;" || true\n\
139
+ \n\
140
+ echo "Database connection successful"\n\
141
+ \n\
142
+ # Start application services\n\
143
+ cd /app/api && poetry run python -m flask db upgrade\n\
144
+ \n\
145
+ cd /app/api && poetry run python -m gunicorn app:app \
146
+ --bind ${DIFY_BIND_ADDRESS:-0.0.0.0}:${DIFY_PORT:-7860} \
147
+ --worker-class gevent \
148
+ --workers 1 \
149
+ --timeout 300 \
150
+ --preload &\n\
151
+ \n\
152
+ cd /app/web && node server.js &\n\
153
+ \n\
154
+ wait' > /app/entrypoint.sh && \
155
+ chmod +x /app/entrypoint.sh
156
+
157
+ WORKDIR /app
158
+
159
+ CMD ["./entrypoint.sh"]