leonsimon23 commited on
Commit
64fa8c9
·
verified ·
1 Parent(s): f8c24e9

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +28 -50
Dockerfile CHANGED
@@ -13,69 +13,47 @@ 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 set permissions for the non-root user
17
- # These steps are run as root
18
- RUN mkdir -p /app/instance/sessions /app/static/images && \
19
- touch /app/instance/users.db && \
20
- chown -R nobody:nogroup /app/instance /app/static && \
21
- chmod -R 775 /app/instance /app/static
22
 
23
  # Install Python dependencies
24
- RUN pip install --no-cache-dir -r requirements.txt gunicorn
25
 
26
- # Set environment variables
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  ENV FLASK_APP=app.py
28
  ENV FLASK_ENV=production
29
  ENV HOST=0.0.0.0
30
  ENV PORT=7860
31
  ENV PYTHONUNBUFFERED=1
32
 
33
- # Create gunicorn config file with optimized settings
34
- RUN echo 'import multiprocessing\n\
35
- bind = "0.0.0.0:7860"\n\
36
  workers = 1\n\
37
  worker_class = "sync"\n\
38
- worker_connections = 1000\n\
39
- keepalive = 120\n\
40
- timeout = 300\n\
41
- graceful_timeout = 60\n\
42
- max_requests = 1000\n\
43
- max_requests_jitter = 50\n\
44
  accesslog = "-"\n\
45
  errorlog = "-"\n\
46
- loglevel = "info"\n\
47
- capture_output = True\n\
48
- enable_stdio_inheritance = True\n\
49
- preload_app = True\n\
50
- forwarded_allow_ips = "*"' > /app/gunicorn.conf.py
51
-
52
- # Expose the port Hugging Face Spaces expects
53
- EXPOSE 7860
54
-
55
- # Create a script to run the application
56
- # This is also run as root
57
- RUN echo '#!/bin/bash\n\
58
- echo "===== Application Startup at $(date) ====="\n\
59
- echo "Starting application initialization..."\n\
60
- export PYTHONPATH=/app\n\
61
- python3 -c "from app import init_db; init_db()" && \
62
- echo "Database initialized successfully"\n\
63
- echo "Starting Gunicorn server..."\n\
64
- exec gunicorn \
65
- --config /app/gunicorn.conf.py \
66
- --bind 0.0.0.0:7860 \
67
- --access-logfile - \
68
- --error-logfile - \
69
- --log-level info \
70
- app:app' > /app/start.sh && \
71
- chmod +x /app/start.sh
72
-
73
- # Health check
74
- HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
75
- CMD curl -f http://localhost:7860/health || exit 1
76
 
77
- # Switch to non-root user just before running the app
78
  USER nobody
79
 
80
- # Start the application
81
- CMD ["/app/start.sh"]
 
 
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"]