Context:我在Google Vertex AI中为每个bigquery数据集训练一个非常相似的模型,但我希望为每个现有数据集(在Google bigquery中(都有一个自定义的训练图像。从这个意义上说,我需要按需在容器注册表中用程序构建一个自定义Docker映像。我的想法是让谷歌云函数来做这件事,由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中的符号链接路径:lstat/tmp/Dockerfile:没有这样的文件或目录
我已经使用Cloud Build Client Python库在本地测试了构建容器映像。事实证明,即使Dockerfile
文件存在于当前目录中,也存在相同的错误:
错误:
步骤#0:无法准备上下文:无法评估Dockerfile中的符号链接路径:lstat/workspace/Dockerfile:没有这样的文件或目录
构建步骤:
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方法,所以我遵循了此文档。您将看到source
存在于API方法中。这是解决问题的关键。在StorageSource
中,必须指定bucket
和object_
。您需要压缩源代码并将其上传到云存储桶中。例如:
- 运行以下命令压缩源代码:
tar -cvzf sourcecode.tar.gz .
- 上传到云存储桶(您可以使用云构建桶(:
gsutil cp sourcecode.tar.gz gs://myproject_cloudbuild
- 构建。源:
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()
因此,它解决了使用客户端库构建映像的问题。我建议在你的云功能中完成所有这些。