File size: 2,087 Bytes
d488241
 
 
 
7295b0b
d488241
 
7295b0b
 
d488241
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7295b0b
 
 
 
 
 
d488241
7295b0b
d488241
 
 
 
 
 
 
7295b0b
 
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

# Set environment variables
ENV FLASK_APP=app.py
ENV FLASK_ENV=development
ENV PATH="/home/appuser/.local/bin:$PATH"
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV FLASK_DEBUG=1

# Install system dependencies for OpenCV and other requirements
RUN apt-get update && apt-get install -y \
    libgl1-mesa-glx \
    libglib2.0-0 \
    libsm6 \
    libxext6 \
    libxrender-dev \
    tesseract-ocr \
    wget \
    && rm -rf /var/lib/apt/lists/*

# Create a non-root user
RUN useradd -m appuser && mkdir -p /app /app/instance /app/models && chown -R appuser:appuser /app

# Set the working directory
WORKDIR /app

# Copy requirements and install dependencies
COPY --chown=appuser:appuser requirements.txt .

# Install dependencies with proper error handling
RUN pip install --no-cache-dir "numpy<2.0.0" && \
    pip install --no-cache-dir -r requirements.txt && \
    # Verify critical packages are installed
    python -c "import flask, flask_sqlalchemy, redis, celery; print('All critical dependencies installed successfully')"

# Download YOLO files
RUN wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights -O /app/models/yolov4.weights && \
    wget https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4.cfg -O /app/models/yolov4.cfg && \
    chown appuser:appuser /app/models/yolov4.*

# Create a health check script
RUN echo '#!/bin/sh\npython -c "import flask, flask_sqlalchemy, redis, celery; print(\"Dependencies OK\")"' > /app/healthcheck.sh

# Copy the application code
COPY --chown=appuser:appuser . .

# Create a debug script
RUN echo '#!/bin/bash\n\
echo "Starting app in debug mode..."\n\
python -m flask run --host=0.0.0.0 --port=5000 --debug\n\
' > /app/debug.sh && chmod +x /app/debug.sh

# Make scripts executable
RUN chmod +x /app/prestart.sh /app/healthcheck.sh /app/start.sh /app/debug.sh

# Switch to non-root user
USER appuser

# Expose the port
EXPOSE 5000

# Run the application in debug mode
CMD ["python", "-m", "flask", "run", "--host=0.0.0.0", "--port=5000", "--debug"]