# Dockerfile for building and serving a Python FastAPI application using Poetry # This Dockerfile uses a multi-stage build to keep the final image size small. # It builds the application in a Python environment and serves it using Uvicorn. # The User ID can be passed as a build argument to run the container as a non-root user. ARG USER_ID=$(id -u) # The Python version can be passed as a build argument. # Default is set to Python 3.13. ARG PYTHON_VERSION=3.13 # Builder stage # This stage is used to install dependencies using Poetry and export them to a requirements.txt file. FROM python:${PYTHON_VERSION}-slim as builder # Set the User ID for non-root user USER ${USER_ID} # Install Poetry in the builder stage RUN pip install --no-cache-dir poetry # Set working directory WORKDIR /app # Copy the Poetry configuration files COPY pyproject.toml poetry.lock ./ # Add the poetry plugin for exporting dependencies RUN poetry self add poetry-plugin-export # Force the requirements.txt to be generated without cache ADD "https://www.random.org/cgi-bin/randbyte?nbytes=10&format=h" skipcache # Export dependencies to requirements.txt # This will create a requirements.txt file with the dependencies listed in pyproject.toml RUN poetry export -f requirements.txt --output requirements.txt --without-hashes # Final stage # Use a fresh Python image to keep the final image small FROM python:${PYTHON_VERSION}-slim # Set the User ID for non-root user USER ${USER_ID} # Set working directory WORKDIR /app # Copy the requirements.txt generated in the builder stage and install dependencies COPY --from=builder /app/requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Copy the project source code COPY src/ src/ # Copy the Alembic configuration and migrations COPY alembic.ini alembic.ini COPY migrations/ migrations/ COPY .env .env # Start fastapi application using uvicorn EXPOSE 8000 ENV PYTHONPATH=/app/src CMD ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]