{% extends "base.Dockerfile.jinja" %}

{% block base_image_patch %}
# If user base image is supplied in config, apply build commands from truss base image
{% if config.base_image %}
ENV PYTHONUNBUFFERED True
ENV DEBIAN_FRONTEND=noninteractive

RUN apt update && \
    apt install -y bash \
                build-essential \
                git \
                curl \
                ca-certificates \
                software-properties-common \
    && apt-get autoremove -y \
    && apt-get clean -y \
    && rm -rf /var/lib/apt/lists/*

COPY ./base_server_requirements.txt base_server_requirements.txt
RUN pip install -r base_server_requirements.txt --no-cache-dir && rm -rf /root/.cache/pip

# This is a hack, kfserving uses table_logger, which doesn't work well with
# numpy 1.24 onwards, where np.float and np.int have been remove.
# https://github.com/AleksTk/table-logger/blob/v0.3.6/table_logger/table_logger.py#L80
# Monkey patch table_logger here. Ultimately we should move away from kfserving,
# perhaps to kserve.
RUN find /usr/local/lib/ -name table_logger.py -exec sed -i '/np\.int:/d;/np\.float:/d' {} \;

{% if live_reload %}
COPY ./control /control
RUN python3 -m venv /control/.env \
    && /control/.env/bin/pip3 install -r /control/requirements.txt
{% endif %}
{% endif %}

{% endblock %}

{% block install_requirements %}
    {%- if should_install_server_requirements %}
COPY ./server_requirements.txt server_requirements.txt
RUN pip install -r server_requirements.txt --no-cache-dir && rm -rf /root/.cache/pip
    {%- endif %}
{{ super() }}
{% endblock %}

{% block b10cp %}
RUN mkdir -p /app/bin \
    && curl https://baseten-public.s3.us-west-2.amazonaws.com/bin/b10cp-0.0.2-linux-amd64 -o /app/bin/b10cp \
    && chmod +x /app/bin/b10cp
{% endblock %}

{% block app_copy %}
COPY ./server /app
COPY ./{{ config.model_module_dir }} /app/model
COPY ./config.yaml /app/config.yaml
    {%- if data_dir_exists %}
COPY ./{{config.data_dir}} /app/data
    {%- endif %}
{% endblock %}


{% block run %}
    {%- if config.live_reload %}
ENV HASH_TRUSS {{truss_hash}}
ENV CONTROL_SERVER_PORT 8080
ENV INFERENCE_SERVER_PORT 8090
CMD exec /control/.env/bin/python3 /control/control/server.py
    {%- else %}
ENV INFERENCE_SERVER_PORT 8080
CMD exec python3 /app/inference_server.py
    {%- endif %}
{% endblock %}
