背景關系:我正在 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 路徑中的符號鏈接:lstat /tmp/Dockerfile:沒有這樣的檔案或目錄
uj5u.com熱心網友回復:
我在本地測驗了使用Cloud Build Client Python library 構建容器映像。事實證明,即使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 方法,因此我遵循了本檔案。您將看到sourceAPI 方法中的is 。這是解決問題的缺失鑰匙。在 中StorageSource,您必須指定bucket和object_。您需要壓縮源代碼并將其上傳到 Cloud Storage 存盤桶中。例如:
- 運行以下命令來壓縮您的源代碼:
tar -cvzf sourcecode.tar.gz .
- 上傳到 Cloud Storage 存盤桶(您可以使用 Cloud Build 存盤桶):
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()
結果它解決了使用客戶端庫構建影像的問題。我建議在您的 Cloud Function 中執行所有這些操作。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/390053.html
標籤:Python 码头工人 谷歌云功能 谷歌云构建 google-artifact-registry
