gpt4 book ai didi

python - 从云函数内部构建容器镜像

转载 作者:行者123 更新时间:2023-12-05 04:39:34 26 4
gpt4 key购买 nike

上下文:我在 Google Vertex AI 中为每个 bigquery 数据集训练一个非常相似的模型,但我想为每个现有数据集(在 Google BigQuery 中)都有一个自定义训练图像。从这个意义上讲,我需要按需在容器注册表中以编程方式构建自定义 Docker 镜像。我的想法是让 Google Cloud Function 执行此操作,由 PubSub 主题触发,其中包含有关我要为其构建训练容器的数据集的信息。因此,该函数自然会将 Dockerfile 和相关脚本写入 Cloud Functions 中的/tmp 文件夹(据我所知,这是唯一可写的地方)。但是,当我尝试在此脚本中实际构建容器时,显然它找不到/tmp 文件夹或其内容,即使它们在那里(通过日志记录操作检查)。

到目前为止令人不安的代码:

def build_container(dataset=str):

with open('container_template/Dockerfile','r') as f:
dockerfile = f.read()
dockerfile = dockerfile.replace('@dataset',dataset)
f.close()

os.makedirs(os.path.dirname('/tmp/script-location'), exist_ok=True)

with open('/tmp/Dockerfile','w') as docker_config:
docker_config.write(dockerfile)

docker_config.close()

shutil.copy('container_template/script-location/script.py','/tmp/script-location/script.py')

build_client = cloudbuild_v1.CloudBuildClient()

build = cloudbuild_v1.Build()

build.steps = [{'name':'gcr.io/cloud-builders/docker',
'args':['build', '-t', 'us-central1-docker.pkg.dev/myproject/myrepo/imagename:latest','/tmp']},
{'name':'gcr.io/cloud-builders/docker',
'args':['push', 'us-central1-docker.pkg.dev/myproject/myrepo/imagename:latest']}]

build_operation = build_client.create_build(project_id=myprojectid,build=build)

build_result = build_operation.result()

logger.info('Build Result: {}'.format(build_result.status))

当我检查云构建日志时,我得到:步骤#0:无法准备上下文:无法评估 Dockerfile 路径中的符号链接(symbolic link):lstat/tmp/Dockerfile:没有这样的文件或目录

最佳答案

我已经在本地测试使用 Cloud Build Client Python library 构建容器镜像.即使 Dockerfile 文件存在于当前目录中,结果也有同样的错误:

错误:

Step #0: unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /workspace/Dockerfile: no such file or directory

构建步骤:

    build_client = cloudbuild_v1.CloudBuildClient()

build = cloudbuild_v1.Build()

build.steps = [{'name':'gcr.io/cloud-builders/docker',
'args':['build', '-t', 'us-central1-docker.pkg.dev/myproject/myrepo/imagename:latest','.']},
{'name':'gcr.io/cloud-builders/docker',
'args':['push', 'us-central1-docker.pkg.dev/myproject/myrepo/imagename:latest']}]

build_operation = build_client.create_build(project_id=myprojectid,build=build)

build_result = build_operation.result()

因为它使用 API 方法,所以我遵循了这个 documentation .您将看到 source 出现在 API 方法中。它是推进问题的缺失 key 。在 StorageSource ,您必须指定 bucketobject_。您需要压缩源代码并将其上传到 Cloud Storage 存储桶中。例如:

  1. 运行以下命令来压缩您的源代码:
tar -cvzf sourcecode.tar.gz .
  1. 上传到 Cloud Storage 存储桶(您可以使用 Cloud Build 存储桶):
gsutil cp sourcecode.tar.gz gs://myproject_cloudbuild
  1. build.source:
    build_client = cloudbuild_v1.CloudBuildClient()

build = cloudbuild_v1.Build()

build.source = {"storage_source":{"bucket":"myproject_cloudbuild", "object_":"gs://myproject_cloudbuild/sourcecode.tar.gz"}}

build.steps = [{'name':'gcr.io/cloud-builders/docker',
'args':['build', '-t', 'us-central1-docker.pkg.dev/myproject/myrepo/imagename:latest','.']},
{'name':'gcr.io/cloud-builders/docker',
'args':['push', 'us-central1-docker.pkg.dev/myproject/myrepo/imagename:latest']}]

build_operation = build_client.create_build(project_id=myprojectid,build=build)

build_result = build_operation.result()

因此,它解决了使用 Client Library 构建图像的问题。我建议在您的 Cloud Function 函数中执行所有这些操作。

关于python - 从云函数内部构建容器镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70428362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com