-
Notifications
You must be signed in to change notification settings - Fork 755
/
containerize.py
366 lines (340 loc) · 12.3 KB
/
containerize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
from __future__ import annotations
import sys
import typing as t
import logging
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from click import Group
from bentoml._internal.bento import BentoStore
F = t.Callable[..., t.Any]
def containerize_transformer(
value: t.Iterable[str] | str | bool | None,
) -> t.Iterable[str] | str | bool | None:
if value is None:
return
if isinstance(value, tuple) and not value:
return
return value
def add_containerize_command(cli: Group) -> None:
import click
from simple_di import inject
from simple_di import Provide
from bentoml.bentos import FEATURES
from bentoml.bentos import containerize as containerize_bento
from bentoml_cli.utils import kwargs_transformers
from bentoml._internal.utils.docker import validate_tag
from bentoml._internal.configuration import get_debug_mode
from bentoml._internal.configuration.containers import BentoMLContainer
@cli.command()
@click.argument("bento_tag", type=click.STRING)
@click.option(
"-t",
"--docker-image-tag",
help="Name and optionally a tag (format: 'name:tag'), defaults to bento tag.",
required=False,
callback=validate_tag,
multiple=True,
)
@click.option(
"--add-host",
multiple=True,
help="Add a custom host-to-IP mapping (format: 'host:ip').",
)
@click.option(
"--allow",
multiple=True,
default=None,
help="Allow extra privileged entitlement (e.g., 'network.host', 'security.insecure').",
)
@click.option("--build-arg", multiple=True, help="Set build-time variables.")
@click.option(
"--build-context",
multiple=True,
help="Additional build contexts (e.g., name=path).",
)
@click.option(
"--builder",
type=click.STRING,
default=None,
help="Override the configured builder instance.",
)
@click.option(
"--cache-from",
multiple=True,
default=None,
help="External cache sources (e.g., 'user/app:cache', 'type=local,src=path/to/dir').",
)
@click.option(
"--cache-to",
multiple=True,
default=None,
help="Cache export destinations (e.g., 'user/app:cache', 'type=local,dest=path/to/dir').",
)
@click.option(
"--cgroup-parent",
type=click.STRING,
default=None,
help="Optional parent cgroup for the container.",
)
@click.option(
"--iidfile",
type=click.STRING,
default=None,
help="Write the image ID to the file.",
)
@click.option("--label", multiple=True, help="Set metadata for an image.")
@click.option(
"--load",
is_flag=True,
default=False,
help="Shorthand for '--output=type=docker'. Note that '--push' and '--load' are mutually exclusive.",
)
@click.option(
"--metadata-file",
type=click.STRING,
default=None,
help="Write build result metadata to the file.",
)
@click.option(
"--network",
type=click.STRING,
default=None,
help="Set the networking mode for the 'RUN' instructions during build (default 'default').",
)
@click.option(
"--no-cache",
is_flag=True,
default=False,
help="Do not use cache when building the image.",
)
@click.option(
"--no-cache-filter",
multiple=True,
help="Do not cache specified stages.",
)
@click.option(
"--output",
multiple=True,
default=None,
help="Output destination (format: 'type=local,dest=path').",
)
@click.option(
"--platform", default=None, multiple=True, help="Set target platform for build."
)
@click.option(
"--progress",
default="auto",
type=click.Choice(["auto", "tty", "plain"]),
help="Set type of progress output ('auto', 'plain', 'tty'). Use plain to show container output.",
)
@click.option(
"--pull",
is_flag=True,
default=False,
help="Always attempt to pull all referenced images.",
)
@click.option(
"--push",
is_flag=True,
default=False,
help="Shorthand for '--output=type=registry'. Note that '--push' and '--load' are mutually exclusive.",
)
@click.option(
"--secret",
multiple=True,
default=None,
help="Secret to expose to the build (format: 'id=mysecret[,src=/local/secret]').",
)
@click.option("--shm-size", default=None, help="Size of '/dev/shm'.")
@click.option(
"--ssh",
type=click.STRING,
default=None,
help="SSH agent socket or keys to expose to the build (format: 'default|<id>[=<socket>|<key>[,<key>]]').",
)
@click.option(
"--target",
type=click.STRING,
default=None,
help="Set the target build stage to build.",
)
@click.option(
"--ulimit", type=click.STRING, default=None, help="Ulimit options (default [])."
)
@click.option(
"--enable-features",
multiple=True,
nargs=1,
metavar="[features,]",
help=f"Enable additional BentoML features. Available features are: {', '.join(FEATURES)}.",
)
@kwargs_transformers(transformer=containerize_transformer)
@inject
def containerize( # type: ignore
bento_tag: str,
docker_image_tag: tuple[str],
add_host: tuple[str],
allow: tuple[str],
build_arg: tuple[str],
build_context: tuple[str],
builder: str,
cache_from: tuple[str],
cache_to: tuple[str],
cgroup_parent: str,
iidfile: str,
label: tuple[str],
load: bool,
network: str,
metadata_file: str,
no_cache: bool,
no_cache_filter: tuple[str],
output: tuple[str],
platform: tuple[str],
progress: t.Literal["auto", "tty", "plain"],
pull: bool,
push: bool,
secret: tuple[str],
shm_size: str,
ssh: str,
target: str,
ulimit: str,
enable_features: tuple[str],
_bento_store: BentoStore = Provide[BentoMLContainer.bento_store],
) -> None:
"""Containerizes given Bento into a ready-to-use Docker image.
\b
BENTO is the target BentoService to be containerized, referenced by its name
and version in format of name:version. For example: "iris_classifier:v1.2.0"
'bentoml containerize' command also supports the use of the 'latest' tag
which will automatically use the last built version of your Bento.
You can provide a tag for the image built by Bento using the
'--docker-image-tag' flag. Additionally, you can provide a '--push' flag,
which will push the built image to the Docker repository specified by the
image tag.
You can also prefixing the tag with a hostname for the repository you wish
to push to.
e.g. 'bentoml containerize IrisClassifier:latest --push --tag
repo-address.com:username/iris' would build a Docker image called
'username/iris:latest' and push that to docker repository at repo-address.com.
By default, the 'containerize' command will use the current credentials
provided by Docker daemon.
'bentoml containerize' also uses Docker Buildx as backend, in place for normal 'docker build'.
By doing so, BentoML will leverage Docker Buildx features such as multi-node
builds for cross-platform images, Full BuildKit capabilities with all of the
familiar UI from 'docker build'.
"""
from bentoml._internal.utils import buildx
logger = logging.getLogger("bentoml")
# run health check whether buildx is install locally
buildx.health()
add_hosts = {}
if add_host:
for host in add_host:
host_name, ip = host.split(":")
add_hosts[host_name] = ip
allow_ = []
if allow:
allow_ = list(allow)
build_args = {}
if build_arg:
for build_arg_str in build_arg:
key, value = build_arg_str.split("=")
build_args[key] = value
build_context_ = {}
if build_context:
for build_context_str in build_context:
key, value = build_context_str.split("=")
build_context_[key] = value
labels = {}
if label:
for label_str in label:
key, value = label_str.split("=")
labels[key] = value
output_: dict[str, t.Any] | None = None
if output:
output_ = {}
for arg in output:
if "," in arg:
for val in arg.split(","):
k, v = val.split("=")
output_[k] = v
key, value = arg.split("=")
output_[key] = value
load = True
if platform and len(platform) > 1:
if not push:
logger.warning(
"Multiple '--platform' arguments were found. Make sure to also use '--push' to push images to a repository or generated images will not be saved. For more information, see https://docs.docker.com/engine/reference/commandline/buildx_build/#load."
)
if push:
load = False
if get_debug_mode():
progress = "plain"
exit_code = not containerize_bento(
bento_tag,
docker_image_tag=docker_image_tag,
# containerize options
features=enable_features,
# docker options
add_host=add_hosts,
allow=allow_,
build_args=build_args,
build_context=build_context_,
builder=builder,
cache_from=cache_from,
cache_to=cache_to,
cgroup_parent=cgroup_parent,
iidfile=iidfile,
labels=labels,
load=load,
metadata_file=metadata_file,
network=network,
no_cache=no_cache,
no_cache_filter=no_cache_filter,
output=output_,
platform=platform,
progress=progress,
pull=pull,
push=push,
quiet=logger.getEffectiveLevel() == logging.ERROR,
secrets=secret,
shm_size=shm_size,
ssh=ssh,
target=target,
ulimit=ulimit,
)
if not exit_code:
# Note that we have to duplicate the logic here
# to ensure there is a docker image tag after a successful containerize.
if not docker_image_tag:
bento = _bento_store.get(bento_tag)
docker_image_tag = (str(bento.tag),)
# This section contains some duplicate logics from containerize_bento
# to process log message.
grpc_metrics_port = BentoMLContainer.grpc.metrics.port.get()
logger.info(
'Successfully built docker image for "%s" with tags "%s"',
str(bento_tag),
",".join(docker_image_tag),
)
docker_tag = ",".join(docker_image_tag)
if len(docker_image_tag) > 1:
instruction = 'To run your newly built Bento container, use one of the above tags, and pass it to "docker run". %s'
else:
instruction = f'To run your newly built Bento container, pass "{docker_tag}" to "docker run". %s'
instruction %= (
'For example: "docker run -it --rm -p 3000:3000 %s serve --production".'
% docker_tag
)
if enable_features is not None:
# enable_features could be a tuple of comma-separated string.
features = [
l for s in map(lambda x: x.split(","), enable_features) for l in s
]
if "grpc" in features:
instruction += (
'\nAdditionally, to run your Bento container as a gRPC server, do: "docker run -it --rm -p 3000:3000 -p %s:%s %s serve-grpc --production"'
% (grpc_metrics_port, grpc_metrics_port, docker_tag)
)
logger.info(instruction)
sys.exit(exit_code)