FROM amd64/ubuntu:24.04

ENV DEBIAN_FRONTEND noninteractive
ENV LC_ALL C.UTF-8
ENV LANG C.UTF-8

# pre-requisites
# libgomp1 is required by lightgmb to open a shared object file for parallel computation
RUN apt-get update && apt-get install --no-install-recommends -y --upgrade \
    python3 python3-pip git curl gunicorn coinor-cbc postgresql-client libgomp1 && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

WORKDIR /app
# requirements - doing this earlier, so we don't install them each time. Use --no-cache to refresh them.
COPY requirements /app/requirements

# py dev tooling
# NB --break-system-packages, as Python >=3.11 separates system & external libs, see also https://github.com/FlexMeasures/flexmeasures/pull/1723#pullrequestreview-3271273745
RUN python3 -m pip install --no-cache-dir --break-system-packages highspy && \
    PYV=$(python3 -c "import sys;t='{v[0]}.{v[1]}'.format(v=list(sys.version_info[:2]));sys.stdout.write(t)") && \
    pip install --no-cache-dir --break-system-packages -r requirements/$PYV/app.txt -r requirements/$PYV/dev.txt -r requirements/$PYV/test.txt

# Copy code and meta/config data
COPY setup.* pyproject.toml .flaskenv wsgi.py /app/
COPY flexmeasures/ /app/flexmeasures
RUN find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf
COPY .git/ /app/.git

RUN pip3 install --no-cache-dir --break-system-packages .

EXPOSE 5000

CMD [ \
    "gunicorn", \
    "--bind", "0.0.0.0:5000", \
    # This is set to /tmp by default, but this is part of the Docker overlay filesystem, and can cause stalls.
    # http://docs.gunicorn.org/en/latest/faq.html#how-do-i-avoid-gunicorn-excessively-blocking-in-os-fchmod
    "--worker-tmp-dir", "/dev/shm", \
    # Ideally you'd want one worker per container, but we don't want to risk the health check timing out because
    # another request is taking a long time to complete.
    "--workers", "2", "--threads", "4", \
    "wsgi:application" \
]
