Skip to content

Commit da2d3ba

Browse files
Feature:3974 Add image tag docker setting
1 parent d0c608a commit da2d3ba

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

docs/book/how-to/containerization/containerization.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,19 @@ The repository name will be appended to the registry URI of your container regis
538538

539539
If you don't specify a target repository, the default repository name configured in your container registry stack component settings will be used.
540540

541+
### Specifying Image tags
542+
543+
You can control the tag of the generated Docker images using the image tag option:
544+
545+
```python
546+
from zenml.config import DockerSettings
547+
548+
docker_settings = DockerSettings(image_tag="1.0.0")
549+
```
550+
551+
Keep in mind that this will be applied to all images built using the DockerSettings object. If there are multiple
552+
such images, only one of them will keep the tag while the rest will be untagged.
553+
541554
### Decoupling Code from Builds
542555

543556
To reuse Docker builds while still using your latest code changes, you need to decouple your code from the build. There are two main approaches:

src/zenml/config/docker_settings.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ class DockerSettings(BaseSettings):
122122
this image.
123123
* If a custom `dockerfile` is specified for this settings
124124
object, this parent image will be ignored.
125+
image_tag: Docker image tag to use for the images that get built.
126+
127+
Additional notes:
128+
* This tag will be used for all images built for the pipeline or step
129+
(depending on where you define your DockerSettings). If there are multiple
130+
such images, only one of them will keep the tag while the rest will be untagged.
125131
dockerfile: Path to a custom Dockerfile that should be built. Depending
126132
on the other values you specify in this object, the resulting
127133
image will be used directly to run your pipeline or ZenML will use
@@ -207,6 +213,7 @@ class DockerSettings(BaseSettings):
207213
"""
208214

209215
parent_image: Optional[str] = None
216+
image_tag: Optional[str] = None
210217
dockerfile: Optional[str] = None
211218
build_context_root: Optional[str] = None
212219
parent_image_build_config: Optional[DockerBuildConfig] = None

src/zenml/pipelines/build_utils.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,14 @@ def create_pipeline_build(
374374
dockerfile = images[item_key].dockerfile
375375
requirements = images[item_key].requirements
376376
else:
377-
tag = snapshot.pipeline_configuration.name
378-
if build_config.step_name:
379-
tag += f"-{build_config.step_name}"
380-
tag += f"-{build_config.key}"
381-
tag = docker_utils.sanitize_tag(tag)
377+
if build_config.settings.image_tag:
378+
tag = build_config.settings.image_tag
379+
else:
380+
tag = snapshot.pipeline_configuration.name
381+
if build_config.step_name:
382+
tag += f"-{build_config.step_name}"
383+
tag += f"-{build_config.key}"
384+
tag = docker_utils.sanitize_tag(tag)
382385

383386
include_files = build_config.should_include_files(
384387
code_repository=code_repository,

tests/unit/pipelines/test_build_utils.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,12 @@ def test_build_uses_correct_settings(mocker, empty_pipeline): # noqa: F811
212212
def test_building_with_identical_keys_and_settings(mocker):
213213
"""Tests that two build configurations with identical keys and identical
214214
settings don't lead to two builds."""
215-
build_config_1 = BuildConfiguration(key="key", settings=DockerSettings())
216-
build_config_2 = BuildConfiguration(key="key", settings=DockerSettings())
215+
build_config_1 = BuildConfiguration(
216+
key="key", settings=DockerSettings(image_tag="v1")
217+
)
218+
build_config_2 = BuildConfiguration(
219+
key="key", settings=DockerSettings(image_tag="v1")
220+
)
217221

218222
mocker.patch.object(
219223
Stack,
@@ -240,6 +244,8 @@ def test_building_with_identical_keys_and_settings(mocker):
240244

241245
mock_build_docker_image.assert_called_once()
242246

247+
assert mock_build_docker_image.call_args[1]["tag"] == "v1"
248+
243249

244250
def test_building_with_identical_keys_and_different_settings(mocker):
245251
"""Tests that two build configurations with identical keys and different
@@ -301,8 +307,7 @@ def test_building_with_different_keys_and_identical_settings(mocker):
301307
assert len(build.images) == 2
302308
assert build.images["key1"].image == "image_name"
303309
assert build.images["key2"].image == "image_name"
304-
305-
mock_build_docker_image.assert_called_once()
310+
assert mock_build_docker_image.call_args[1]["tag"] == "pipeline-key1"
306311

307312

308313
def test_custom_build_verification(

0 commit comments

Comments
 (0)