meisaicheck-api / Dockerfile
vumichien's picture
Refactor Dockerfile to streamline directory creation for appuser, removing unnecessary steps and ensuring proper permissions for AI model caching after git clone.
9002ce9
FROM python:3.9-slim
WORKDIR /app
# Cài đặt các dependencies cần thiết
RUN apt-get update && apt-get install -y \
build-essential \
git \
&& rm -rf /var/lib/apt/lists/*
# Tạo user appuser trước khi tạo thư mục
RUN useradd -m appuser
# Tạo các thư mục cần thiết (không bao gồm meisai-check-ai vì sẽ được tạo bởi git clone)
RUN mkdir -p /app/data /app/uploads /app/outputs /tmp/matplotlib-cache /tmp/huggingface-cache && \
chown -R appuser:appuser /app /tmp/matplotlib-cache /tmp/huggingface-cache
# Thiết lập biến môi trường cho Matplotlib và Hugging Face
ENV MPLCONFIGDIR=/tmp/matplotlib-cache
ENV TRANSFORMERS_CACHE=/tmp/huggingface-cache
ENV HF_HOME=/tmp/huggingface-cache
ENV HF_DATASETS_CACHE=/tmp/huggingface-cache
# Copy requirements từ project hiện tại
COPY requirements.txt .
# Clone repository meisai-check-ai từ Bitbucket sử dụng secret, checkout nhánh chien_develop và đảm bảo lấy code mới nhất
RUN --mount=type=secret,id=BITBUCKET_APP_PW,mode=0444,required=true \
git clone https://vumichien:$(cat /run/secrets/BITBUCKET_APP_PW)@bitbucket.org/dtm-partners/meisai-check-ai.git && \
cd meisai-check-ai && \
git checkout develop && \
git pull origin develop && \
cd ..
# Cài đặt dependencies
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install --no-cache-dir -r meisai-check-ai/requirements.txt
# Copy code của API vào container
COPY . .
# Sao chép các file dữ liệu mẫu vào thư mục data
RUN mkdir -p /app/data
COPY ./data/* /app/data/
# Ensure we have the correct abstractMapData.csv structure
# Copy the correct abstractMapData.csv file specifically to override any cached version
COPY ./data/abstractMapData.csv /app/data/abstractMapData.csv
# Debug: Check the abstractMapData.csv structure
RUN echo "=== Checking abstractMapData.csv structure ===" && \
head -3 /app/data/abstractMapData.csv && \
echo "=== End of abstractMapData.csv check ==="
# Thêm đường dẫn meisai-check-ai vào PYTHONPATH
ENV PYTHONPATH="${PYTHONPATH}:/app/meisai-check-ai"
# Tạo thư mục cache cho AI model sau khi git clone và cấp quyền đầy đủ cho appuser
RUN mkdir -p /app/meisai-check-ai/data && \
chown -R appuser:appuser /app && \
chmod -R 755 /app && \
chmod -R 777 /app/data /app/uploads /app/outputs /app/meisai-check-ai/data /tmp/matplotlib-cache /tmp/huggingface-cache
# Expose port
EXPOSE 7860
# Chuyển sang user appuser sau khi đã cấp quyền
USER appuser
# Chạy ứng dụng với Uvicorn
# Lưu ý: Hugging Face Spaces sử dụng port 7860
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]