【问题标题】:Specify Dockerfile for gcloud build submit为 gcloud 构建提交指定 Dockerfile
【发布时间】:2020-02-08 03:35:09
【问题描述】:

我了解gcloud 使用在源(.)的根目录中指定的 Dockerfile,如下所示: gcloud builds submit --tag gcr.io/[PROJECT_ID]/quickstart-image .

但我正在尝试指定用于构建映像的 Dockerfile,但我没有找到任何有关如何执行此操作的资源,我不知道这是否可能。

【问题讨论】:

    标签: docker kubernetes gcloud google-cloud-build


    【解决方案1】:

    我不确定你是否可以指定 Dockerfile,但你可以使用cloudbuild.yaml 文件。检查 gcloud documentation。如果要重命名此文件,可以使用config 选项。

        gcloud builds submit --config cloudbuild.yaml .
    

    cloudbuild.yaml 文件示例如下所示,

    steps:
    - name: 'gcr.io/cloud-builders/docker'
      args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/quickstart-image', '.' ]
    images:
    - 'gcr.io/$PROJECT_ID/quickstart-image'
    

    【讨论】:

      【解决方案2】:

      指定 Dockerfile(即除 ./Dockerfile 之外)的唯一方法是为每个 techtabu@ 创建一个 cloudbuild.yaml。然后,此配置可以使用 docker 构建器并提供特定的 Dockerfile,即:

      steps:
      - name: "gcr.io/cloud-builders/docker"
        args:
        - build
        - "--tag=gcr.io/$PROJECT_ID/quickstart-image"
        - "--file=./path/to/YourDockerFile"
        - .
      ...
      images:
      - "gcr.io/$PROJECT_ID/quickstart-image"
      

      如果您愿意,您还可以指定一个替代名称而不是 cloudbuild.yaml

      ./Dockerfile 假设可能是为了简化向 Cloud Build 的过渡。

      我建议您改用cloudbuild.yaml,因为它提供了灵活性。

      【讨论】:

      【解决方案3】:

      您可以通过将. 替换为./path/to/YourDockerFile 来轻松完成此操作,因此gcloud 命令将是:

      gcloud builds submit --tag gcr.io/[PROJECT_ID]/quickstart-image ./path/to/YourDockerFile
      

      因此您不必为此使用cloudbuild.yaml

      【讨论】:

      • 这对我不起作用:ERROR: (gcloud.builds.submit) Local file [{src}] is none of .zip, .tgz, .gz
      • @ZaarHai 只需指定包含 dockerfile 的 目录不是文件本身的路径
      • 这在我的用例中并没有真正的帮助——我想要一个包含两个 dockerfile 的文件夹——一个用于开发(即运行单元测试),一个用于生产。最终使用 tar --transoform。见下文。
      • 这适用于您可以使用指定目录的构建上下文的情况。但是,对我来说,它需要来自 repo root,所以使用上面的@DazWilkin 答案
      【解决方案4】:

      最终,以下 hack 对我来说非常有效。如果你有例如Dockerfile.prodDockerfile.dev 使用以下内容来构建后者。

      tar --exclude-vcs-ignores \  # sort-of .dockerignore support
          --transform='s|^\./Dockerfile.dev|./Dockerfile|' -zcf /tmp/togo.tgz . && \
                  gcloud builds submit --tag=gcr.io/my-project/foo:latest /tmp/togo.tgz
      

      【讨论】:

        【解决方案5】:

        gcloud 构建提交将自动检测您的Dockerfile(如果存在),请确保将文件放在您的root directory 中。如果你对 docker 不太了解,可以使用这个命令

        gcloud beta run deploy --source=.
        

        在你的根目录执行它。 buildpacks 将创建 dockerfile 为您服务,它能够检测您的应用程序堆栈,以便使用相关工具进行构建。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-10-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多