FROM $base_image as build

COPY $model_env_folder/conda.yaml conda.yaml
COPY $model_env_folder/requirements.txt requirements.txt

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

# The micromamba image comes with an empty environment named base.
RUN --mount=type=cache,target=/opt/conda/pkgs micromamba install -y -n base -f conda.yaml && \
	python -m pip install "uvicorn[standard]" gunicorn starlette==0.30.0 && \
	python -m pip install -r requirements.txt

FROM debian:buster-slim AS runtime

ENV USER nonrootuser
ENV UID 1000
ENV HOME /home/$USER
RUN adduser --disabled-password \
	--gecos "A non-root user for running inference server" \
	--uid $UID \
	--home $HOME \
	$USER

COPY $inference_server_dir ./$inference_server_dir
COPY $entrypoint_script ./$entrypoint_script
RUN chmod +x /$entrypoint_script

# The mamba root prefix by default is set to /opt/conda, in which the base conda environment is built at.
COPY --from=build /opt/conda /opt/conda

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

USER nonrootuser

CMD ["/$entrypoint_script"]
