title: How to build a docker from within a kfp component using python docker lib
body: <p>i have a <code>kfp</code> pipeline with a component from which i want to build an image and push it somewhere to be used in a later component:</p>
  <p>kfp pipeline:</p>
  <p>... --&gt; [Build image comoponent2] --&gt;</p>
  <p>How can i write a kfp component to do that?</p>
  <p>i have started some experiments but i'm not sure about it,
  in general the component looks like that:</p>
  <pre><code>@component(
  packages_to_install=[&quot;google-cloud-aiplatform&quot;, &quot;GitPython&quot;, 
  &quot;docker&quot;],
  base_image=&quot;SOME DIND BASE IMAGE WITH PYTHON&quot;,
  )
  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 = &quot;Dockerfile&quot;,
  ):
  # Create a client
  
  import os
  import git
  import docker
  
  
  url_with_access_token = f&quot;https://__token__:{gitlab_access_token}@{repo_url}&quot;
  repo = git.Git().clone(url_with_access_token)
  print(repo)
  
  image_tag = f&quot;{artifact_registry}/{project}/{repo_name}/{branch_name}:{commit_sha}&quot;
  
  project_dir = f&quot;{os.getcwd()}/{repo_name}&quot;
  client = docker.from_env()
  
  
  dockerfile_path = os.path.dirname(f&quot;{project_dir}/Dockerfile&quot;)
  print(dockerfile_path)
  
  image, build_log = client.images.build(
       path=project_dir,
       dockerfile=dockerfile_name,
       tag=image_tag,
       buildargs={&quot;PYPI_TOKEN&quot;: gitlab_access_token},
       rm=True,
   )
   print(f&quot;IMAGE: {image}&quot;)
   for log in build_log:
       print(log)
   response = client.images.push(
       &quot;us-central1-docker.pkg.dev/k-algo/inference-modules/199667345242/conversation_im/master:24394faff65e6d44594079ce129e8d9baf949b61&quot;
   )
   print(response)
   print(image)
   for log in build_log:
       print(log)
  </code></pre>