# Note that base image tag should not be 'latest' as it might cause false positive image cache hit.
FROM ${base_image} as build

COPY ${model_env_folder}/conda.yml conda.yml
COPY ${model_env_folder}/requirements.txt requirements.txt
COPY ${inference_server_dir} ./${inference_server_dir}
COPY ${entrypoint_script} ./${entrypoint_script}

USER root
RUN if id mambauser >/dev/null 2>&1; then \
        echo "mambauser already exists."; \
    else \
        # Set environment variables
        export USER=mambauser && \
        export UID=1000 && \
        export HOME=/home/$USER && \
        echo "Creating $USER user..." && \
        adduser --disabled-password \
            --gecos "A non-root user for running inference server" \
            --uid $UID \
            --home $HOME \
            $USER; \
    fi

RUN chmod +rx conda.yml
RUN chmod +rx requirements.txt
RUN chmod +x ./${entrypoint_script}

USER mambauser

# Set MAMBA_DOCKERFILE_ACTIVATE=1 to activate the conda environment during build time.
ARG MAMBA_DOCKERFILE_ACTIVATE=1

# Bitsandbytes uses this ENVVAR to determine CUDA library location
ENV CONDA_PREFIX=/opt/conda

# The micromamba image comes with an empty environment named base.
# CONDA_OVERRIDE_CUDA ref https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-virtual.html
RUN --mount=type=cache,target=/opt/conda/pkgs CONDA_OVERRIDE_CUDA="${cuda_override_env}" \
    micromamba install -y -n base -f conda.yml && \
	python -m pip install "uvicorn[standard]" gunicorn starlette==0.30.0 && \
	python -m pip install -r requirements.txt && \
    micromamba clean -afy

${copy_model_statement}

${extra_env_statement}

# Expose the port on which the Starlette app will run.
EXPOSE 5000

CMD ["./${entrypoint_script}"]
