How to build a docker from within a kfp component using python docker lib

107 Views Asked by At

i have a kfp pipeline with a component from which i want to build an image and push it somewhere to be used in a later component:

kfp pipeline:

... --> [Build image comoponent2] -->

How can i write a kfp component to do that?

i have started some experiments but i'm not sure about it, in general the component looks like that:

@component(
packages_to_install=["google-cloud-aiplatform", "GitPython", 
"docker"],
base_image="SOME DIND BASE IMAGE WITH PYTHON",
)
def build_image_component(
project: str,
location: str,
repo_url: str,
branch_name: str,
commit_sha: str,
gitlab_access_token: str,
repo_name: str,
artifact_registry: str,
dockerfile_name: str = "Dockerfile",
):
# Create a client

import os
import git
import docker


url_with_access_token = f"https://__token__:{gitlab_access_token}@{repo_url}"
repo = git.Git().clone(url_with_access_token)
print(repo)

image_tag = f"{artifact_registry}/{project}/{repo_name}/{branch_name}:{commit_sha}"

project_dir = f"{os.getcwd()}/{repo_name}"
client = docker.from_env()


dockerfile_path = os.path.dirname(f"{project_dir}/Dockerfile")
print(dockerfile_path)

image, build_log = client.images.build(
     path=project_dir,
     dockerfile=dockerfile_name,
     tag=image_tag,
     buildargs={"PYPI_TOKEN": gitlab_access_token},
     rm=True,
 )
 print(f"IMAGE: {image}")
 for log in build_log:
     print(log)
 response = client.images.push(
     "us-central1-docker.pkg.dev/k-algo/inference-modules/199667345242/conversation_im/master:24394faff65e6d44594079ce129e8d9baf949b61"
 )
 print(response)
 print(image)
 for log in build_log:
     print(log)
0

There are 0 best solutions below