|
1 | | -# Base image with Python |
2 | | -FROM python:3.10-slim AS base |
| 1 | +# Build stage |
| 2 | +FROM python:3.10-slim AS builder |
3 | 3 |
|
4 | | -# Set working directory |
5 | 4 | WORKDIR /app |
6 | 5 |
|
7 | | -# Install system dependencies |
8 | | -RUN apt-get update && apt-get install -y \ |
9 | | - build-essential \ |
10 | | - curl \ |
11 | | - && rm -rf /var/lib/apt/lists/* |
| 6 | +# Install a specific version of Poetry that supports --no-dev |
| 7 | +RUN pip install poetry==2.1.1 |
12 | 8 |
|
13 | | -# Install Poetry |
14 | | -RUN curl -sSL https://install.python-poetry.org | python3 - && \ |
15 | | - ln -s /root/.local/bin/poetry /usr/local/bin/ |
| 9 | +# Copy project files |
| 10 | +COPY pyproject.toml poetry.lock* README.md ./ |
| 11 | +COPY babeltron ./babeltron |
16 | 12 |
|
17 | | -# Copy Poetry configuration files |
18 | | -COPY README.md pyproject.toml poetry.lock* ./ |
| 13 | +# Configure poetry to not use a virtual environment |
| 14 | +RUN poetry config virtualenvs.create false \ |
| 15 | + && poetry install --without dev --no-interaction --no-ansi |
19 | 16 |
|
20 | | -# Copy application code |
21 | | -COPY babeltron/ ./babeltron/ |
| 17 | +FROM python:3.10-slim |
22 | 18 |
|
23 | | -# Configure Poetry to not use virtualenvs in Docker |
24 | | -RUN poetry config virtualenvs.create false |
| 19 | +WORKDIR /app |
25 | 20 |
|
26 | | -# Install dependencies |
27 | | -RUN poetry install --without dev --no-interaction --no-ansi |
| 21 | +# Copy Python dependencies from builder stage |
| 22 | +COPY --from=builder /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages |
| 23 | +COPY --from=builder /usr/local/bin /usr/local/bin |
| 24 | + |
| 25 | +# Copy application code |
| 26 | +COPY --from=builder /app/babeltron ./babeltron |
28 | 27 |
|
29 | | -# Set environment variables |
30 | | -ENV MODEL_PATH=/models |
31 | 28 | ENV PYTHONPATH=/app |
32 | | -ENV WORKERS=1 |
33 | | -ENV HOST=0.0.0.0 |
34 | | -ENV PORT=8000 |
35 | | -ENV MODEL_COMPRESSION_ENABLED=true |
| 29 | +ENV MODEL_PATH=/models |
36 | 30 |
|
37 | | -# Expose the port the app runs on |
38 | | -EXPOSE 8000 |
| 31 | +# Create a non-root user |
| 32 | +RUN useradd -m appuser |
| 33 | +USER appuser |
39 | 34 |
|
40 | | -# Create a script to start the application with the specified number of workers |
41 | | -RUN echo '#!/bin/bash\n\ |
42 | | -echo "Starting with $WORKERS workers"\n\ |
43 | | -echo "Model compression: $MODEL_COMPRESSION_ENABLED"\n\ |
44 | | -if [ "$WORKERS" -eq "1" ]; then\n\ |
45 | | - # Single worker mode uses uvicorn directly\n\ |
46 | | - exec uvicorn babeltron.app.main:app --host $HOST --port $PORT\n\ |
47 | | -else\n\ |
48 | | - # Multi-worker mode uses gunicorn with uvicorn workers\n\ |
49 | | - exec gunicorn babeltron.app.main:app \\\n\ |
50 | | - --workers $WORKERS \\\n\ |
51 | | - --worker-class uvicorn.workers.UvicornWorker \\\n\ |
52 | | - --bind $HOST:$PORT\n\ |
53 | | -fi' > /app/start.sh && chmod +x /app/start.sh |
54 | | - |
55 | | -# Command to run the application |
56 | | -CMD ["/app/start.sh"] |
| 35 | +EXPOSE 8000 |
| 36 | +CMD ["uvicorn", "babeltron.app.main:app", "--host", "0.0.0.0", "--port", "8000"] |
0 commit comments