image
ImageConstructor
ImageConstructor(config: BuildConfig)
Constructs a Dockerfile based on a provided build configuration.
This class generates Dockerfile content by appending commands derived from the build configuration and provides methods to save the generated Dockerfile.
Initializes the ImageConstructor with the given build configuration.
Parameters:
-
config(BuildConfig) –The build configuration settings.
Examples:
>>> from ures.docker.conf import BuildConfig
>>> config = BuildConfig(base_image="python:3.10-slim", user="appuser")
>>> constructor = ImageConstructor(config)
Source code in ures/docker/image.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
home_dir
property
home_dir: Path
Returns the home directory path based on the configured user.
Returns:
-
Path(Path) –The home directory path (e.g. /home/{user} if user is specified, else /root).
Examples:
>>> from ures.docker.conf import BuildConfig
>>> config = BuildConfig(user="appuser")
>>> constructor = ImageConstructor(config)
>>> constructor.home_dir
PosixPath('/home/appuser')
content
property
content: List[str]
save
save(dest: Union[str, Path]) -> Path
Saves the generated Dockerfile to the specified destination.
If the destination is a directory, the Dockerfile will be named using the configuration's docker_filename and placed inside that directory.
Parameters:
Returns:
-
Path(Path) –The full path where the Dockerfile was saved.
Examples:
>>> constructor = ImageConstructor(BuildConfig(docker_filename="Dockerfile"))
>>> saved_path = constructor.save("/tmp")
>>> saved_path.name # Should be 'Dockerfile'
Source code in ures/docker/image.py
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 | |
Image
Image(image_name: str, tag: Optional[str] = None, client: DockerClient = None)
Represents and manages a Docker image.
Attributes:
-
_image_name(str) –The name of the Docker image.
-
_tag(str) –The tag of the Docker image (default is "latest").
-
_client(DockerClient) –The Docker client instance.
-
_image(Optional[Image]) –The Docker image object if available.
Initializes an Image instance.
Parameters:
-
image_name(str) –The name of the Docker image.
-
tag(Optional[str], default:None) –The image tag. Defaults to "latest" if not provided.
-
client(Optional[DockerClient], default:None) –The Docker client to use. Defaults to docker.from_env().
Examples:
>>> img = Image("myapp", tag="v1")
Source code in ures/docker/image.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
name
property
name: str
Gets the name of the image.
Returns:
-
str(str) –The image name.
Examples:
>>> img = Image("myapp")
>>> img.name
'myapp'
tag
property
tag: str
Gets the tag of the image.
Returns:
-
str(str) –The image tag.
Examples:
>>> img = Image("myapp", tag="v1")
>>> img.tag
'v1'
exist
property
exist: bool
Checks if the image exists locally.
Returns:
-
bool(bool) –True if the image exists, False otherwise.
Examples:
>>> img = Image("myapp")
>>> img.exist # Depends on local Docker images
image
property
image: Optional[Image]
Gets the Docker image object.
Returns:
-
Optional[Image]–Optional[DockerImage]: The Docker image object if found, otherwise None.
Examples:
>>> img = Image("myapp")
>>> img.image # Might return a DockerImage object if available
id
property
id: str
Gets the unique ID of the Docker image.
Returns:
-
str(str) –The Docker image ID.
Examples:
>>> img = Image("myapp")
>>> img.id
'sha256:...'
architecture
property
architecture: str
Gets the architecture of the Docker image.
Returns:
-
str(str) –The image architecture (e.g., 'amd64').
Examples:
>>> img = Image("myapp")
>>> img.architecture
'amd64'
image_size
property
image_size: int
Gets the size of the Docker image in bytes.
Returns:
-
int(int) –The size of the image in bytes.
Examples:
>>> img = Image("myapp")
>>> img.image_size
12345678
labels
property
labels: dict
Gets the labels of the Docker image.
Returns:
-
dict(dict) –A dictionary of image labels.
Examples:
>>> img = Image("myapp")
>>> img.labels
{'version': '1.0'}
get_fullname
get_fullname(tag: Optional[str] = None) -> str
Constructs the full image name including the tag.
Parameters:
Returns:
-
str(str) –The full image name in the format "name:tag".
Examples:
>>> img = Image("myapp", tag="v1")
>>> img.get_fullname()
'myapp:v1'
Source code in ures/docker/image.py
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 | |
get_image
get_image(tag: Optional[str] = None) -> Optional[DockerImage]
Retrieves the Docker image from the local repository.
Parameters:
-
tag(Optional[str], default:None) –The tag to use when retrieving the image. Defaults to the instance's tag.
Returns:
-
Optional[Image]–Optional[DockerImage]: The Docker image if found; otherwise, None.
Examples:
>>> img = Image("myapp")
>>> image_obj = img.get_image()
>>> image_obj is not None # Depends on local Docker images
Source code in ures/docker/image.py
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 | |
pull_image
pull_image(tag: Optional[str] = None) -> Optional[DockerImage]
Pulls the Docker image from a remote repository.
Parameters:
-
tag(Optional[str], default:None) –The tag to pull; defaults to the instance's tag if not provided.
Returns:
-
Optional[Image]–Optional[DockerImage]: The pulled Docker image if successful; otherwise, None.
Examples:
>>> img = Image("myapp")
>>> pulled = img.pull_image()
>>> pulled is not None
Source code in ures/docker/image.py
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 | |
build_image
build_image(build_config: BuildConfig, dest: Union[str, Path], build_context: Optional[Union[str, Path]] = None) -> DockerImage
Builds a Docker image using the specified build configuration.
Parameters:
-
build_config(BuildConfig) –The configuration for building the image.
-
dest(Union[str, Path]) –The destination path where the Dockerfile will be saved.
-
build_context(Optional[Union[str, Path]], default:None) –The build context directory. Defaults to build_config.context_dir.
Returns:
-
DockerImage(Image) –The built Docker image.
Examples:
>>> build_config = BuildConfig()
>>> img = Image("myapp")
>>> built_img = img.build_image(build_config, "/tmp/dockerfile_dir")
>>> built_img is not None
True
Source code in ures/docker/image.py
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 | |
remove
remove(tag: Optional[str] = None, force: bool = False, noprune: bool = False)
Removes the Docker image from the local repository.
Parameters:
-
tag(Optional[str], default:None) –The tag to remove. Defaults to the instance's tag.
-
force(bool, default:False) –Force removal. Defaults to False.
-
noprune(bool, default:False) –Do not remove untagged parent images. Defaults to False.
Examples:
>>> img = Image("myapp")
>>> img.remove()
Source code in ures/docker/image.py
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 | |
info
info()
Prints detailed information about the Docker image.
Examples:
>>> img = Image("myapp")
>>> img.info()
Source code in ures/docker/image.py
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 | |
ImageOrchestrator
ImageOrchestrator(client: Optional[DockerClient] = None)
Orchestrates the building of multiple Docker images considering their dependencies.
Attributes:
-
_client(DockerClient) –The Docker client instance.
-
_images(dict) –A dictionary holding images and their build configuration and status.
Initializes the ImageOrchestrator.
Parameters:
-
client(Optional[DockerClient], default:None) –The Docker client instance. Defaults to docker.from_env().
Examples:
>>> orchestrator = ImageOrchestrator()
Source code in ures/docker/image.py
638 639 640 641 642 643 644 645 646 647 648 649 650 651 | |
images
property
images: Dict[str, Dict[str, Union[Optional[Image], BuildConfig, str]]]
add_image
add_image(image: Image, config: BuildConfig, base: Image = None) -> bool
Adds an image and its build configuration to the orchestrator.
Parameters:
-
image(Image) –The Image instance to add.
-
config(BuildConfig) –The build configuration for the image.
-
base(Image, default:None) –The base image that this image depends on, if any.
Returns:
-
bool–True if the image was registered in the orchestrator.
Examples:
>>> orch = ImageOrchestrator()
>>> img = Image("myapp")
>>> config = BuildConfig()
>>> orch.add_image(img, config)
True
Source code in ures/docker/image.py
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 | |
build_all
build_all()
Builds all registered images in the correct order based on dependencies.
Examples:
>>> orchestrator.build_all()
Source code in ures/docker/image.py
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 | |