FROM tiangolo/uvicorn-gunicorn-fastapi:python3.10

WORKDIR /app/

# Install Poetry
RUN curl -sSL https://install.python-poetry.org | POETRY_HOME=/opt/poetry python && \
    cd /usr/local/bin && \
    ln -s /opt/poetry/bin/poetry && \
    poetry config virtualenvs.create false

# Copy poetry.lock* in case it doesn't exist in the repo
COPY ./pyproject.toml ./poetry.lock* /app/

# Allow installing dev dependencies to run tests
ARG INSTALL_DEV=false
RUN bash -c "if [ $INSTALL_DEV == 'true' ] ; then poetry install --no-root ; else poetry install --no-root --only main ; fi"

ENV PYTHONPATH=/app

COPY ./scripts/ /app/

COPY ./alembic.ini /app/

COPY ./prestart.sh /app/

COPY ./tests-start.sh /app/

COPY ./app /app/app

# Create a non-root user and group
RUN addgroup --system celery && adduser --system --ingroup celery celery

# Create necessary directories
RUN mkdir -p /app/.cache/huggingface /app/cache/ /app/upload-data/

# Set HuggingFace cache directory environment variable
ENV HF_HOME=/app/.cache/huggingface

# Set the ownership and permissions for the /app directory and its contents
RUN chown -R celery:celery /app
