File size: 1,828 Bytes
f216f9c
497203c
3d63de3
e63e363
3d63de3
e63e363
 
 
497203c
 
3d63de3
 
 
 
cec55e8
3d63de3
 
 
 
3e58f7b
9eb6664
21aa862
5d54d11
3d63de3
 
 
 
 
 
 
 
 
 
 
 
e63e363
9eb6664
3d63de3
497203c
 
e63e363
 
5d54d11
b2bd677
 
 
cc354fb
3d63de3
 
 
fb5feff
 
 
3d63de3
 
 
 
e63e363
3d63de3
1dd1595
 
3d63de3
64fa8c9
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
FROM python:3.9-slim

# Install git and clean up in the same layer to keep the image size down
RUN apt-get update && \
    apt-get install -y git curl && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Clone private repository using mounted secret
RUN --mount=type=secret,id=GITHUB_TOKEN,required=true \
    git clone https://x-access-token:$(cat /run/secrets/GITHUB_TOKEN)@github.com/leoncool23/AIhfbackend.git . \
    && rm -rf .git

# Create necessary directories and give them open permissions
# This is a robust way to handle permissions in containerized environments.
RUN mkdir -p /app/instance/sessions && \
    chmod -R 777 /app/instance

# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Create a script to run the application
# Use 'set -e' to exit immediately if a command fails.
RUN echo '#!/bin/bash\n\
set -e\n\
echo "===== Application Startup at $(date) ====="\n\
echo "Initializing database..."\n\
python3 -c "from app import init_db; init_db()"\n\
echo "Database initialized successfully."\n\
echo "Starting Gunicorn server..."\n\
exec gunicorn \
    --config /app/gunicorn.conf.py \
    app:app' > /app/start.sh && \
    chmod +x /app/start.sh

# Environment variables
ENV FLASK_APP=app.py
ENV FLASK_ENV=production
ENV HOST=0.0.0.0
ENV PORT=7860
ENV PYTHONUNBUFFERED=1
ENV DIFY_API_KEY=${DIFY_API_KEY}
ENV DIFY_API_URL=${DIFY_API_URL}
ENV DIFY_APP_ID=${DIFY_APP_ID}

# Create gunicorn config file
RUN echo 'bind = "0.0.0.0:7860"\n\
workers = 1\n\
#worker_class = "gevent"\n\
worker_class = "gthread"\n\
threads = 4\n\
loglevel = "info"\n\
accesslog = "-"\n\
errorlog = "-"\n\
preload_app = True' > /app/gunicorn.conf.py

# Switch to non-root user for security
USER nobody

# Expose the port and start the application
EXPOSE 7860
CMD ["/app/start.sh"]