leonsimon23 commited on
Commit
3d63de3
·
verified ·
1 Parent(s): d4fddc1

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +33 -46
Dockerfile CHANGED
@@ -1,72 +1,59 @@
1
  FROM python:3.9-slim
2
 
3
- # Install system dependencies
4
  RUN apt-get update && \
5
- apt-get install -y curl && \
6
  apt-get clean && \
7
  rm -rf /var/lib/apt/lists/*
8
 
9
  WORKDIR /app
10
 
11
- # Create necessary directories first
12
- RUN mkdir -p /app/templates /app/instance/sessions
 
 
13
 
14
- # Copy all files
15
- COPY . .
16
-
17
- # Debug: Show directory structure
18
- RUN echo "=== Directory Structure ===" && \
19
- ls -la /app
20
 
21
  # Install Python dependencies
22
  RUN pip install --no-cache-dir -r requirements.txt
23
 
24
- # Create gunicorn configuration
25
- RUN echo 'bind = "0.0.0.0:7860"' > /app/gunicorn.conf.py && \
26
- echo 'workers = 1' >> /app/gunicorn.conf.py && \
27
- echo 'worker_class = "sync"' >> /app/gunicorn.conf.py && \
28
- echo 'loglevel = "info"' >> /app/gunicorn.conf.py && \
29
- echo 'accesslog = "-"' >> /app/gunicorn.conf.py && \
30
- echo 'errorlog = "-"' >> /app/gunicorn.conf.py && \
31
- echo 'preload_app = True' >> /app/gunicorn.conf.py && \
32
- echo 'chdir = "/app"' >> /app/gunicorn.conf.py
33
-
34
- # Create start script
35
- RUN echo '#!/bin/bash' > /app/start.sh && \
36
- echo 'set -e' >> /app/start.sh && \
37
- echo 'cd /app' >> /app/start.sh && \
38
- echo 'export PYTHONPATH=/app:$PYTHONPATH' >> /app/start.sh && \
39
- echo 'echo "===== Application Startup at $(date) ====="' >> /app/start.sh && \
40
- echo 'echo "Current directory: $(pwd)"' >> /app/start.sh && \
41
- echo 'echo "Python path: $PYTHONPATH"' >> /app/start.sh && \
42
- echo 'echo "Directory contents:"' >> /app/start.sh && \
43
- echo 'ls -la' >> /app/start.sh && \
44
- echo 'echo "Running initialization script..."' >> /app/start.sh && \
45
- echo 'python3 init_script.py' >> /app/start.sh && \
46
- echo 'echo "Starting Gunicorn server..."' >> /app/start.sh && \
47
- echo 'exec gunicorn --config /app/gunicorn.conf.py app:app' >> /app/start.sh && \
48
  chmod +x /app/start.sh
49
 
50
- # Set environment variables
51
  ENV FLASK_APP=app.py
52
  ENV FLASK_ENV=production
53
  ENV HOST=0.0.0.0
54
  ENV PORT=7860
55
- ENV PYTHONPATH=/app
56
  ENV PYTHONUNBUFFERED=1
57
 
58
- # Dify API configuration
59
- ENV DIFY_API_KEY=${DIFY_API_KEY}
60
- ENV DIFY_API_ENDPOINT=${DIFY_API_ENDPOINT}
 
 
 
 
 
61
 
62
- # Set permissions
63
- RUN chmod -R 777 /app/instance && \
64
- chown -R nobody:nogroup /app && \
65
- chmod -R 755 /app
66
-
67
- # Switch to non-root user
68
  USER nobody
69
 
 
70
  EXPOSE 7860
71
-
72
  CMD ["/app/start.sh"]
 
1
  FROM python:3.9-slim
2
 
3
+ # Install git and clean up in the same layer to keep the image size down
4
  RUN apt-get update && \
5
+ apt-get install -y git curl && \
6
  apt-get clean && \
7
  rm -rf /var/lib/apt/lists/*
8
 
9
  WORKDIR /app
10
 
11
+ # Clone private repository using mounted secret
12
+ RUN --mount=type=secret,id=GITHUB_TOKEN,required=true \
13
+ git clone https://x-access-token:$(cat /run/secrets/GITHUB_TOKEN)@github.com/leoncool23/AIhfbackend.git . \
14
+ && rm -rf .git
15
 
16
+ # Create necessary directories and give them open permissions
17
+ # This is a robust way to handle permissions in containerized environments.
18
+ RUN mkdir -p /app/instance/sessions && \
19
+ chmod -R 777 /app/instance
 
 
20
 
21
  # Install Python dependencies
22
  RUN pip install --no-cache-dir -r requirements.txt
23
 
24
+ # Create a script to run the application
25
+ # Use 'set -e' to exit immediately if a command fails.
26
+ RUN echo '#!/bin/bash\n\
27
+ set -e\n\
28
+ echo "===== Application Startup at $(date) ====="\n\
29
+ echo "Initializing database..."\n\
30
+ python3 -c "from app import init_db; init_db()"\n\
31
+ echo "Database initialized successfully."\n\
32
+ echo "Starting Gunicorn server..."\n\
33
+ exec gunicorn \
34
+ --config /app/gunicorn.conf.py \
35
+ app:app' > /app/start.sh && \
 
 
 
 
 
 
 
 
 
 
 
 
36
  chmod +x /app/start.sh
37
 
38
+ # Environment variables
39
  ENV FLASK_APP=app.py
40
  ENV FLASK_ENV=production
41
  ENV HOST=0.0.0.0
42
  ENV PORT=7860
 
43
  ENV PYTHONUNBUFFERED=1
44
 
45
+ # Create gunicorn config file
46
+ RUN echo 'bind = "0.0.0.0:7860"\n\
47
+ workers = 1\n\
48
+ worker_class = "sync"\n\
49
+ loglevel = "info"\n\
50
+ accesslog = "-"\n\
51
+ errorlog = "-"\n\
52
+ preload_app = True' > /app/gunicorn.conf.py
53
 
54
+ # Switch to non-root user for security
 
 
 
 
 
55
  USER nobody
56
 
57
+ # Expose the port and start the application
58
  EXPOSE 7860
 
59
  CMD ["/app/start.sh"]