Severian commited on
Commit
e7a94f7
·
verified ·
1 Parent(s): 8d3cf66

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +34 -57
Dockerfile CHANGED
@@ -9,29 +9,10 @@ ENV POETRY_VERSION=1.8.4 \
9
  POETRY_CACHE_DIR=/tmp/poetry_cache \
10
  PYTHONDONTWRITEBYTECODE=1
11
 
12
- # Install PostgreSQL first to ensure postgres group exists
13
- RUN apt-get update && apt-get install -y \
14
- postgresql \
15
- postgresql-contrib \
16
- && rm -rf /var/lib/apt/lists/*
17
-
18
- # Create users with specific GID/UID for HF Spaces compatibility
19
- RUN groupadd -g 1000 user && \
20
- useradd -m -u 1000 -g user user && \
21
- usermod -aG postgres user
22
-
23
- # Set up directories with correct permissions
24
- RUN mkdir -p /var/run/postgresql /var/lib/postgresql/data /var/log/postgresql && \
25
- chown -R postgres:postgres /var/run/postgresql /var/lib/postgresql/data /var/log/postgresql && \
26
- chmod 2777 /var/run/postgresql && \
27
- chmod 700 /var/lib/postgresql/data
28
-
29
- # Create application directories
30
- RUN mkdir -p /app/api /app/web /data/storage && \
31
- chown -R user:user /app /data && \
32
- chmod 777 /data /app
33
 
34
- # Install remaining system dependencies
35
  RUN apt-get update && apt-get install -y \
36
  curl \
37
  git \
@@ -42,15 +23,20 @@ RUN apt-get update && apt-get install -y \
42
  libmpc-dev \
43
  nodejs \
44
  npm \
 
 
45
  && rm -rf /var/lib/apt/lists/* \
46
  && pip install --no-cache-dir "poetry==${POETRY_VERSION}"
47
 
48
- # Initialize PostgreSQL database as postgres user
49
- USER postgres
50
- RUN /usr/lib/postgresql/15/bin/initdb -D /var/lib/postgresql/data && \
51
- echo "host all all 0.0.0.0/0 md5" >> /var/lib/postgresql/data/pg_hba.conf && \
52
- echo "listen_addresses='*'" >> /var/lib/postgresql/data/postgresql.conf && \
53
- echo "unix_socket_directories = '/var/run/postgresql'" >> /var/lib/postgresql/data/postgresql.conf
 
 
 
54
 
55
  # Switch to user for remaining operations
56
  USER user
@@ -68,10 +54,9 @@ FROM base
68
 
69
  # Set up directory structure
70
  WORKDIR /app
71
- RUN mkdir -p api web /data/storage && \
72
- chown -R user:user /app /data
73
 
74
- # Copy from official images with correct ownership
75
  COPY --from=web --chown=user:user /app/web /app/web/
76
  COPY --from=api --chown=user:user /app/api /app/api/
77
 
@@ -98,40 +83,35 @@ ENV FLASK_APP=app.py \
98
  APP_WEB_URL=https://${SPACE_ID}.hf.space \
99
  DIFY_PORT=7860 \
100
  DIFY_BIND_ADDRESS=0.0.0.0 \
101
- DB_USERNAME=${POSTGRES_USER:-postgres} \
102
- DB_PASSWORD=${POSTGRES_PASSWORD:-difyai123456} \
103
- DB_HOST=${POSTGRES_HOST:-localhost} \
104
- DB_PORT=${POSTGRES_PORT:-5432} \
105
- DB_DATABASE=${POSTGRES_DB:-dify} \
106
- REDIS_HOST=localhost \
107
- REDIS_PORT=6379 \
108
- REDIS_PASSWORD=difyai123456 \
109
- CELERY_BROKER_URL=amqp://guest:guest@localhost:5672// \
110
- CELERY_RESULT_BACKEND=redis://localhost:6379/0 \
111
  PYTHONPATH=/app/api \
112
  STORAGE_PATH=/data/storage
113
 
114
- EXPOSE 7860 3000
115
 
116
- # Create startup script with connection retries
117
  RUN echo '#!/bin/bash\n\
118
  echo "===== Application Startup at $(date "+%Y-%m-%d %H:%M:%S") ====="\n\
119
  \n\
120
- # Ensure PostgreSQL directories have correct permissions\n\
121
- mkdir -p /var/log/postgresql /var/run/postgresql\n\
122
- chown -R postgres:postgres /var/log/postgresql /var/run/postgresql /var/lib/postgresql/data\n\
123
- chmod 2777 /var/run/postgresql\n\
124
- chmod 700 /var/lib/postgresql/data\n\
 
125
  \n\
126
- # Start PostgreSQL as postgres user without using su command\n\
127
- runuser -u postgres -- /usr/lib/postgresql/15/bin/pg_ctl -D /var/lib/postgresql/data \
128
- -l /var/log/postgresql/postgresql.log start\n\
129
  \n\
130
  # Wait for PostgreSQL to start\n\
131
  max_tries=30\n\
132
  count=0\n\
133
  echo "Checking database connection..."\n\
134
- until PGPASSWORD="${DB_PASSWORD}" psql -h localhost -p 5432 -U postgres -c "SELECT 1" > /dev/null 2>&1; do\n\
135
  echo "Waiting for database connection... (${count}/${max_tries})"\n\
136
  sleep 2\n\
137
  count=$((count+1))\n\
@@ -141,11 +121,8 @@ until PGPASSWORD="${DB_PASSWORD}" psql -h localhost -p 5432 -U postgres -c "SELE
141
  fi\n\
142
  done\n\
143
  \n\
144
- # Create database and set up permissions\n\
145
- PGPASSWORD="${DB_PASSWORD}" psql -h localhost -p 5432 -U postgres <<-EOSQL\n\
146
- CREATE DATABASE ${DB_DATABASE} WITH OWNER ${DB_USERNAME};\n\
147
- ALTER USER ${DB_USERNAME} WITH PASSWORD '\''${DB_PASSWORD}'\'';\n\
148
- EOSQL\n\
149
  \n\
150
  echo "Database connection successful"\n\
151
  \n\
 
9
  POETRY_CACHE_DIR=/tmp/poetry_cache \
10
  PYTHONDONTWRITEBYTECODE=1
11
 
12
+ # Create user first (HF Spaces requirement)
13
+ RUN useradd -m -u 1000 user
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ # Install system dependencies
16
  RUN apt-get update && apt-get install -y \
17
  curl \
18
  git \
 
23
  libmpc-dev \
24
  nodejs \
25
  npm \
26
+ postgresql \
27
+ postgresql-contrib \
28
  && rm -rf /var/lib/apt/lists/* \
29
  && pip install --no-cache-dir "poetry==${POETRY_VERSION}"
30
 
31
+ # Set up PostgreSQL directories and permissions
32
+ RUN mkdir -p /var/run/postgresql /var/lib/postgresql/data /var/log/postgresql && \
33
+ chown -R user:user /var/run/postgresql /var/lib/postgresql/data /var/log/postgresql && \
34
+ chmod 700 /var/lib/postgresql/data
35
+
36
+ # Create application directories
37
+ RUN mkdir -p /app/api /app/web /data/storage && \
38
+ chown -R user:user /app /data && \
39
+ chmod 777 /data /app
40
 
41
  # Switch to user for remaining operations
42
  USER user
 
54
 
55
  # Set up directory structure
56
  WORKDIR /app
57
+ RUN mkdir -p api web /data/storage
 
58
 
59
+ # Copy from official images
60
  COPY --from=web --chown=user:user /app/web /app/web/
61
  COPY --from=api --chown=user:user /app/api /app/api/
62
 
 
83
  APP_WEB_URL=https://${SPACE_ID}.hf.space \
84
  DIFY_PORT=7860 \
85
  DIFY_BIND_ADDRESS=0.0.0.0 \
86
+ DB_USERNAME=user \
87
+ DB_PASSWORD=difyai123456 \
88
+ DB_HOST=localhost \
89
+ DB_PORT=5432 \
90
+ DB_DATABASE=dify \
 
 
 
 
 
91
  PYTHONPATH=/app/api \
92
  STORAGE_PATH=/data/storage
93
 
94
+ EXPOSE 7860
95
 
96
+ # Create startup script
97
  RUN echo '#!/bin/bash\n\
98
  echo "===== Application Startup at $(date "+%Y-%m-%d %H:%M:%S") ====="\n\
99
  \n\
100
+ # Initialize PostgreSQL database\n\
101
+ initdb -D $HOME/postgresql/data\n\
102
+ \n\
103
+ # Configure PostgreSQL\n\
104
+ echo "host all all 0.0.0.0/0 md5" >> $HOME/postgresql/data/pg_hba.conf\n\
105
+ echo "listen_addresses='\''*'\''" >> $HOME/postgresql/data/postgresql.conf\n\
106
  \n\
107
+ # Start PostgreSQL\n\
108
+ pg_ctl -D $HOME/postgresql/data -l $HOME/postgresql/logfile start\n\
 
109
  \n\
110
  # Wait for PostgreSQL to start\n\
111
  max_tries=30\n\
112
  count=0\n\
113
  echo "Checking database connection..."\n\
114
+ until psql -h localhost -p 5432 -U user -d postgres -c "SELECT 1" > /dev/null 2>&1; do\n\
115
  echo "Waiting for database connection... (${count}/${max_tries})"\n\
116
  sleep 2\n\
117
  count=$((count+1))\n\
 
121
  fi\n\
122
  done\n\
123
  \n\
124
+ # Create database\n\
125
+ createdb ${DB_DATABASE}\n\
 
 
 
126
  \n\
127
  echo "Database connection successful"\n\
128
  \n\