prefect.utilities.dockerutils
BuildError
Bases: Exception
Raised when a Docker build fails
Source code in src/prefect/utilities/dockerutils.py
129 130 |
|
ImageBuilder
An interface for preparing Docker build contexts and building images
Source code in src/prefect/utilities/dockerutils.py
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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
|
__init__(base_image, base_directory=None, platform=None, context=None)
Create an ImageBuilder
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_image
|
str
|
the base image to use |
required |
base_directory
|
Path
|
the starting point on your host for relative file locations, defaulting to the current directory |
None
|
context
|
Path
|
use this path as the build context (if not provided, will create a temporary directory for the context) |
None
|
Returns:
Type | Description |
---|---|
The image ID |
Source code in src/prefect/utilities/dockerutils.py
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 |
|
add_line(line)
Add a line to this image's Dockerfile
Source code in src/prefect/utilities/dockerutils.py
262 263 264 |
|
add_lines(lines)
Add lines to this image's Dockerfile
Source code in src/prefect/utilities/dockerutils.py
266 267 268 |
|
assert_has_file(source, container_path)
Asserts that the given file or directory will be copied into the container at the given path
Source code in src/prefect/utilities/dockerutils.py
392 393 394 395 396 397 398 |
|
assert_has_line(line)
Asserts that the given line is in the Dockerfile
Source code in src/prefect/utilities/dockerutils.py
333 334 335 336 337 338 339 340 341 |
|
assert_line_absent(line)
Asserts that the given line is absent from the Dockerfile
Source code in src/prefect/utilities/dockerutils.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
|
assert_line_after(second, first)
Asserts that the second line appears after the first line
Source code in src/prefect/utilities/dockerutils.py
388 389 390 |
|
assert_line_before(first, second)
Asserts that the first line appears before the second line
Source code in src/prefect/utilities/dockerutils.py
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
|
build(pull=False, stream_progress_to=None)
Build the Docker image from the current state of the ImageBuilder
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pull
|
bool
|
True to pull the base image during the build |
False
|
stream_progress_to
|
Optional[TextIO]
|
an optional stream (like sys.stdout, or an io.TextIO) that will collect the build output as it is reported by Docker |
None
|
Returns:
Type | Description |
---|---|
str
|
The image ID |
Source code in src/prefect/utilities/dockerutils.py
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 |
|
copy(source, destination)
Copy a file to this image
Source code in src/prefect/utilities/dockerutils.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
|
PushError
Bases: Exception
Raised when a Docker image push fails
Source code in src/prefect/utilities/dockerutils.py
401 402 |
|
build_image(context, dockerfile='Dockerfile', tag=None, pull=False, platform=None, stream_progress_to=None, **kwargs)
Builds a Docker image, returning the image ID
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
Path
|
the root directory for the Docker build context |
required |
dockerfile
|
str
|
the path to the Dockerfile, relative to the context |
'Dockerfile'
|
tag
|
Optional[str]
|
the tag to give this image |
None
|
pull
|
bool
|
True to pull the base image during the build |
False
|
stream_progress_to
|
Optional[TextIO]
|
an optional stream (like sys.stdout, or an io.TextIO) that will collect the build output as it is reported by Docker |
None
|
Returns:
Type | Description |
---|---|
str
|
The image ID |
Source code in src/prefect/utilities/dockerutils.py
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 |
|
docker_client()
Get the environmentally-configured Docker client
Source code in src/prefect/utilities/dockerutils.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
format_outlier_version_name(version)
Formats outlier docker version names to pass packaging.version.parse
validation
- Current cases are simple, but creates stub for more complicated formatting if eventually needed.
- Example outlier versions that throw a parsing exception:
- "20.10.0-ce" (variant of community edition label)
- "20.10.0-ee" (variant of enterprise edition label)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
version
|
str
|
raw docker version value |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
value that can pass |
Source code in src/prefect/utilities/dockerutils.py
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
|
generate_default_dockerfile(context=None)
Generates a default Dockerfile used for deploying flows. The Dockerfile is written to a temporary file and yielded. The temporary file is removed after the context manager exits.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
-
|
context
|
The context to use for the Dockerfile. Defaults to the current working directory. |
required |
Source code in src/prefect/utilities/dockerutils.py
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 |
|
get_prefect_image_name(prefect_version=None, python_version=None, flavor=None)
Get the Prefect image name matching the current Prefect and Python versions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefect_version
|
Optional[str]
|
An optional override for the Prefect version. |
None
|
python_version
|
Optional[str]
|
An optional override for the Python version; must be at the minor level e.g. '3.9'. |
None
|
flavor
|
Optional[str]
|
An optional alternative image flavor to build, like 'conda' |
None
|
Source code in src/prefect/utilities/dockerutils.py
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 |
|
parse_image_tag(name)
Parse Docker Image String
- If a tag exists, this function parses and returns the image registry and tag, separately as a tuple.
- Example 1: 'prefecthq/prefect:latest' -> ('prefecthq/prefect', 'latest')
- Example 2: 'hostname.io:5050/folder/subfolder:latest' -> ('hostname.io:5050/folder/subfolder', 'latest')
- Supports parsing Docker Image strings that follow Docker Image Specification v1.1.0
- Image building tools typically enforce this standard
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name of Docker Image |
required |
Return
Source code in src/prefect/utilities/dockerutils.py
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
|
push_image(image_id, registry_url, name, tag=None, stream_progress_to=None)
Pushes a local image to a Docker registry, returning the registry-qualified tag for that image
This assumes that the environment's Docker daemon is already authenticated to the given registry, and currently makes no attempt to authenticate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_id
|
str
|
a Docker image ID |
required |
registry_url
|
str
|
the URL of a Docker registry |
required |
name
|
str
|
the name of this image |
required |
tag
|
str
|
the tag to give this image (defaults to a short representation of the image's ID) |
None
|
stream_progress_to
|
Optional[TextIO]
|
an optional stream (like sys.stdout, or an io.TextIO) that will collect the build output as it is reported by Docker |
None
|
Returns:
Type | Description |
---|---|
str
|
A registry-qualified tag, like my-registry.example.com/my-image:abcdefg |
Source code in src/prefect/utilities/dockerutils.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
|
split_repository_path(repository_path)
Splits a Docker repository path into its namespace and repository components.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repository_path
|
str
|
The Docker repository path to split. |
required |
Returns:
Type | Description |
---|---|
Tuple[Optional[str], str]
|
Tuple[Optional[str], str]: A tuple containing the namespace and repository components. - namespace (Optional[str]): The Docker namespace, combining the registry and organization. None if not present. - repository (Optionals[str]): The repository name. |
Source code in src/prefect/utilities/dockerutils.py
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
|
to_run_command(command)
Convert a process-style list of command arguments to a single Dockerfile RUN instruction.
Source code in src/prefect/utilities/dockerutils.py
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
|