Dockerfile.fastapi•1.33 kB
# Use official Python 3.10 slim image
FROM python:3.10-slim
# Set metadata
LABEL maintainer="XuMing <xuming624@qq.com>"
LABEL description="FastAPI server for Python code execution"
LABEL version="0.0.3"
# Set working directory
WORKDIR /app
# Set environment variables to prevent Python buffering
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV PATH="/home/appuser/.local/bin:$PATH"
# Create non-root user
RUN groupadd -r appuser && useradd -r -g appuser appuser
COPY requirements.txt .
# Install Python dependencies including build dependencies
RUN pip3 install --user -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip3 install pandas scipy openpyxl -i https://pypi.tuna.tsinghua.edu.cn/simple
COPY . .
# Install the package in development mode
RUN pip install -e .
# Create temporary directory for code execution
RUN mkdir -p /tmp/code_execution && \
chown -R appuser:appuser /tmp/code_execution && \
chmod 755 /tmp/code_execution
# Switch to non-root user
USER appuser
# Expose port for FastAPI
EXPOSE 8083
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD python3 -c "import requests; requests.get('http://localhost:8000/health')" || exit 1
# Command to run FastAPI server
CMD ["python3", "run_python_code/fastapi_server.py"]