-
Notifications
You must be signed in to change notification settings - Fork 788
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Frost Ming <[email protected]>
- Loading branch information
Showing
7 changed files
with
233 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from __future__ import annotations | ||
|
||
import importlib | ||
import os | ||
import shlex | ||
import typing as t | ||
|
||
import attrs | ||
from fs.base import FS | ||
from jinja2 import Environment | ||
from jinja2 import FileSystemLoader | ||
|
||
from bentoml._internal.bento.bento import ImageInfo | ||
from bentoml._internal.container.generate import DEFAULT_BENTO_ENVS | ||
from bentoml._internal.container.generate import PREHEAT_PIP_PACKAGES | ||
from bentoml._internal.container.generate import expands_bento_path | ||
from bentoml._internal.container.generate import resolve_package_versions | ||
from bentoml._internal.container.generate import to_bento_field | ||
from bentoml._internal.container.generate import to_options_field | ||
|
||
|
||
def get_templates_variables( | ||
image: ImageInfo, bento_fs: FS, **bento_envs: t.Any | ||
) -> dict[str, t.Any]: | ||
from bentoml._internal.configuration.containers import BentoMLContainer | ||
|
||
bento_envs = {**DEFAULT_BENTO_ENVS, **bento_envs} | ||
options = attrs.asdict(image) | ||
requirement_file = bento_fs.getsyspath("env/python/requirements.txt") | ||
if os.path.exists(requirement_file): | ||
python_packages = resolve_package_versions(requirement_file) | ||
else: | ||
python_packages = {} | ||
pip_preheat_packages = [ | ||
python_packages[k] for k in PREHEAT_PIP_PACKAGES if k in python_packages | ||
] | ||
return { | ||
**{to_options_field(k): v for k, v in options.items()}, | ||
**{to_bento_field(k): v for k, v in bento_envs.items()}, | ||
"__prometheus_port__": BentoMLContainer.grpc.metrics.port.get(), | ||
"__pip_preheat_packages__": pip_preheat_packages, | ||
} | ||
|
||
|
||
def generate_dockerfile( | ||
image: ImageInfo, bento_fs: FS, *, frontend: str = "dockerfile", **bento_envs: t.Any | ||
) -> str: | ||
templates_path = importlib.import_module( | ||
f"bentoml._internal.container.frontend.{frontend}.templates" | ||
).__path__ | ||
environment = Environment( | ||
extensions=["jinja2.ext.do", "jinja2.ext.loopcontrols", "jinja2.ext.debug"], | ||
trim_blocks=True, | ||
lstrip_blocks=True, | ||
loader=FileSystemLoader(templates_path, followlinks=True), | ||
) | ||
environment.filters["bash_quote"] = shlex.quote | ||
environment.globals["expands_bento_path"] = expands_bento_path | ||
template = environment.get_template("base_v2.j2") | ||
return template.render(**get_templates_variables(image, bento_fs, **bento_envs)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/bentoml/_internal/container/frontend/dockerfile/templates/base_v2.j2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
{# BENTOML INTERNAL #} | ||
{# users can use these values #} | ||
{% import '_macros.j2' as common %} | ||
{% set bento__entrypoint = expands_bento_path('env', 'docker', 'entrypoint.sh', bento_path=bento__path) %} | ||
{% set __enable_buildkit__ = bento__enable_buildkit | default(False) -%} | ||
{% if __enable_buildkit__ %} | ||
# 1.2.1 is the current docker frontend that both buildkitd and kaniko supports. | ||
# syntax = {{ bento__buildkit_frontend }} | ||
# | ||
{% endif %} | ||
{% if bento__add_header %} | ||
# =========================================== | ||
# | ||
# THIS IS A GENERATED DOCKERFILE. DO NOT EDIT | ||
# | ||
# =========================================== | ||
{% endif %} | ||
|
||
# Block SETUP_BENTO_BASE_IMAGE | ||
{% block SETUP_BENTO_BASE_IMAGE %} | ||
FROM {{ __options__base_image }} AS base-container | ||
|
||
ENV LANG=C.UTF-8 | ||
|
||
ENV LC_ALL=C.UTF-8 | ||
|
||
ENV PYTHONIOENCODING=UTF-8 | ||
|
||
ENV PYTHONUNBUFFERED=1 | ||
{% endblock %} | ||
|
||
{% block SETUP_BENTO_USER %} | ||
ARG BENTO_USER={{ bento__user }} | ||
ARG BENTO_USER_UID={{ bento__uid_gid }} | ||
ARG BENTO_USER_GID={{ bento__uid_gid }} | ||
RUN groupadd -g $BENTO_USER_GID -o $BENTO_USER && useradd -m -u $BENTO_USER_UID -g $BENTO_USER_GID -o -r $BENTO_USER | ||
{% endblock %} | ||
|
||
{% block SETUP_BENTO_ENVARS %} | ||
|
||
ARG BENTO_PATH={{ bento__path }} | ||
ENV BENTO_PATH=$BENTO_PATH | ||
ENV BENTOML_HOME={{ bento__home }} | ||
ENV BENTOML_HF_CACHE_DIR={{ bento__path }}/hf-models | ||
|
||
RUN mkdir $BENTO_PATH && chown {{ bento__user }}:{{ bento__user }} $BENTO_PATH -R | ||
WORKDIR $BENTO_PATH | ||
{% endblock %} | ||
|
||
{% block SETUP_BENTO_COMPONENTS %} | ||
{% set required_packages = "curl git" %} | ||
RUN command -v apt-get && apt-get update && apt-get -y install {{ required_packages }} \ | ||
|| command -v apk && apk update && apk add {{ required_packages }} \ | ||
|| yum update && yum install -y {{ required_packages }} \ | ||
|| true | ||
|
||
{% for command in __options__commands %} | ||
RUN {{ command }} | ||
{% endfor %} | ||
|
||
RUN curl -LO https://astral.sh/uv/install.sh && \ | ||
sh install.sh && rm install.sh && mv $HOME/.cargo/bin/uv /usr/local/bin/ && uv venv --python {{ __options__python_version }} | ||
{% set __pip_cache__ = common.mount_cache("/root/.cache/") %} | ||
{% if __pip_preheat_packages__ %} | ||
{% for value in __pip_preheat_packages__ -%} | ||
{% call common.RUN(__enable_buildkit__) -%} {{ __pip_cache__ }} {% endcall -%} uv pip install {{ value|bash_quote }} ; exit 0 | ||
{% endfor -%} | ||
{% endif -%} | ||
|
||
COPY --chown={{ bento__user }}:{{ bento__user }} ./env/python ./env/python/ | ||
# install python packages | ||
{% call common.RUN(__enable_buildkit__) -%} {{ __pip_cache__ }} {% endcall -%} uv pip install -r ./env/python/requirements.txt | ||
COPY --chown={{ bento__user }}:{{ bento__user }} . ./ | ||
{% endblock %} | ||
|
||
# Block SETUP_BENTO_ENTRYPOINT | ||
{% block SETUP_BENTO_ENTRYPOINT %} | ||
# Default port for BentoServer | ||
EXPOSE 3000 | ||
|
||
# Expose Prometheus port | ||
EXPOSE {{ __prometheus_port__ }} | ||
|
||
RUN chmod +x {{ bento__entrypoint }} | ||
|
||
USER bentoml | ||
|
||
ENTRYPOINT [ "{{ bento__entrypoint }}" ] | ||
|
||
{% endblock %} |
Oops, something went wrong.