【问题标题】:Gcloud app deploy cannot ignore DockerfileGcloud app deploy 不能忽略 Dockerfile
【发布时间】:2021-03-26 22:26:16
【问题描述】:

我通常将我的 NodeJS 应用部署到 Google App Engine,并在通过 .gcloudignore 文件部署时忽略所有 docker 资产,如下所示:

.git
.gitignore
Dockerfile
docker-compose.yml
nginx/
redis-data/
.vscode/
.DS_Store
.prettierrc
README.md
node_modules/
.env

上周我已成功将我的应用部署到 App Engine,没有任何问题。但是今天(除了源代码没有任何更改)它失败并给我一个错误:

ERROR: (gcloud.app.deploy) There is a Dockerfile in the current directory, and the runtime field in /Users/tranphongbb/Works/unstatic/habitify-annual-report-backend/app.yaml is currently set to [runtime: nodejs]. To use your Dockerfile to build a custom runtime, set the runtime field to [runtime: custom]. To continue using the [nodejs] runtime, please remove the Dockerfile from this directory.

即使删除.gcloudignore 文件并使用app.yaml 中的skip_files 选项,它仍然失败。

我的源代码树

.dockerignore
.eslintrc.json
.gcloudignore
.gitignore
.prettierrc
.vscode
Dockerfile
README.md
app.yaml
docker-compose.yml
nginx
package.json
src

【问题讨论】:

    标签: node.js docker google-app-engine gcloud


    【解决方案1】:

    .gcloudignore 文件的目的是避免将某些文件上传到 App Engine、Cloud Functions 等、记录在 here 的部署。使用gcloud app deploy 时,此命令将注意到是否存在Dockerfile,并将关联app.yaml 中设置的runtime: custom。如果不满足该条件,您将收到类似的错误消息,如下所示:

    ERROR: (gcloud.app.deploy) There is a Dockerfile in the current directory, and the runtime field in path/app.yaml is currently set to [runtime: nodejs]. To use your Dockerfile to build a custom runtime, set the runtime field to [runtime: custom]. To continue using the [nodejs] runtime, please remove the Dockerfile from this directory.
    

    现在是最后一个问题,为什么这适用于 gcloud beta app deploy 而不是 gcloud app deploy

    查看任何人都可以查看的Cloud SDK的源代码,gcloud app deploy有如下代码,实现了前面提到的验证:

    if info.runtime == 'custom':
        if has_dockerfile and has_cloudbuild:
          raise CustomRuntimeFilesError(
              ('A custom runtime must have exactly one of [{}] and [{}] in the '
               'source directory; [{}] contains both').format(
                   config.DOCKERFILE, runtime_builders.Resolver.CLOUDBUILD_FILE,
                   source_dir))
        elif has_dockerfile:
          log.info('Using %s found in %s', config.DOCKERFILE, source_dir)
          return False
        elif has_cloudbuild:
          log.info('Not using %s because cloudbuild.yaml was found instead.',
                   config.DOCKERFILE)
          return True
        else:
          raise NoDockerfileError(
              'You must provide your own Dockerfile when using a custom runtime. '
              'Otherwise provide a "runtime" field with one of the supported '
              'runtimes.')
      else:
        if has_dockerfile:
          raise DockerfileError(
              'There is a Dockerfile in the current directory, and the runtime '
              'field in {0} is currently set to [runtime: {1}]. To use your '
              'Dockerfile to build a custom runtime, set the runtime field to '
              '[runtime: custom]. To continue using the [{1}] runtime, please '
              'remove the Dockerfile from this directory.'.format(info.file,
    

    另一方面,gcloud beta app deploy 根本不进行此验证(假设我查看了正确的代码):

    if runtime == 'custom' and self in (self.ALWAYS,
                                        self.WHITELIST_BETA,
                                        self.WHITELIST_GA):
      return needs_dockerfile
    

    总之,.gcloudignore 将阻止某些文件/文件夹上传,但在对该命令进行一些预检查时不会考虑。在这种情况下,应考虑使用 Dockerfile,因为它可能是部署的一部分。

    【讨论】:

      【解决方案2】:

      我通过克隆 Node.js App Engine Flex 快速入门并将 Dockerfile 添加到与 app.yaml 文件相同的文件夹中重现了您的问题。 确实,我收到了与您相同的错误消息。但是我可以看到,如果我将 Dockerfile 移动到不同的目录,则部署成功。似乎 gcloud app deploy 不尊重 .gcloudignore 文件。

      对于柔性环境中的 node.js,App Engine Official Documentation 中没有 skip_files 条目。

      要忽略您在 .gcloudignore 文件中定义的文件,请运行命令 gcloud beta app deploy 在 app.yaml 中使用 Nodejs Runtime 时我可以忽略 Dockerfile,或者您可以使用 gcloud app deploy 命令但将您的 Dockerfile 移动到另一个目录。

      【讨论】:

      • 将 Dockerfile 移动到另一个目录不是一个好方法,但我没有剩下的。谢谢。我会接受你的回答:D。
      • 您是否尝试使用 gcloud beta app deploy 进行部署,并在同一目录中维护 Dockerfile?
      • 它上传了超过 20k 个文件到云端,我想那将是 node_modules 文件夹。因此,它只是遵循 app.yaml 定义的运行时,而不尊重其他任何内容。
      • 我已代表您重新打开此Public Issue Tracker request,Google Cloud Platform 团队将在此调查该问题。请跟踪该问题,他们将收到有关此行为的最新信息。
      猜你喜欢
      • 2018-03-08
      • 1970-01-01
      • 2017-01-09
      • 1970-01-01
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 2020-12-29
      • 2021-04-27
      相关资源
      最近更新 更多