| # 1. Use the correct Python version as the base image | |
| # This is the most important fix to solve the TypeError | |
| FROM python:3.10-slim | |
| # 2. Add a non-root user for security | |
| # This is good practice. Your original file already did this. | |
| RUN useradd -m -u 1000 user | |
| USER user | |
| ENV PATH="/home/user/.local/bin:$PATH" | |
| # 3. Set the working directory | |
| WORKDIR /app | |
| # 4. Install system dependencies that TTS often needs | |
| # This should be done before switching to the non-root user, | |
| # but since we already did, we temporarily switch back to root. | |
| USER root | |
| RUN apt-get update && apt-get install -y --no-install-recommends \ | |
| ffmpeg \ | |
| espeak-ng \ | |
| && apt-get clean \ | |
| && rm -rf /var/lib/apt/lists/* | |
| USER user | |
| # 5. Copy and install Python requirements first | |
| # This is a key optimization. It caches the installed packages. | |
| # The slow `pip install` step will only re-run if you change requirements.txt. | |
| COPY --chown=user ./requirements.txt requirements.txt | |
| RUN pip install --no-cache-dir --upgrade -r requirements.txt | |
| # 6. Copy the rest of your application code | |
| # This is done *after* installing requirements. If you only change your | |
| # app.py, Docker can use the cached layer from the step above, making builds much faster. | |
| COPY --chown=user . /app | |
| # 7. Define the command to run your application | |
| CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"] |