【问题标题】:Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORTCloud Run 错误:容器无法启动。无法启动并监听 PORT 定义的端口
【发布时间】:2021-05-24 07:51:24
【问题描述】:

运行这段代码一切正常。

require('dotenv').config();
const express = require('express');
const Firestore = require('@google-cloud/firestore');
const path = require('path');


const app = express();

app.get('/', function (req, res) {
    console.log('hello-cloud-run', 'request received');

    const target = process.env.TARGET || 'Juan';
    res.send(`Hello ${target} from Notch - Shopify Webhooks!`);
 });

 const port = process.env.PORT || 8080;

 app.listen(port,  () => {
    console.log('Webhook app listening on port ', port);
 });

但是,如果我添加 Firestore 行(以 const db = 开头的行),我会收到 Cloud Run 错误:容器无法启动。无法启动并侦听 PORT 环境变量定义的端口。此修订的日志可能包含更多信息。

require('dotenv').config();
const express = require('express');
const Firestore = require('@google-cloud/firestore');
const path = require('path');


const app = express();

const db = new Firestore({
    projectId: 'kuracao-db',
    keyFilename: path.join(__dirname, process.env.GOOGLE_APPLICATION_CREDENTIALS)
});


app.get('/', function (req, res) {
    console.log('hello-cloud-run', 'request received');

    const target = process.env.TARGET || 'Juan';
    res.send(`Hello ${target} from Notch - Shopify Webhooks!`);
});

const port = process.env.PORT || 8080;

app.listen(port,  () => {
    console.log('Webhook app listening on port ', port);
 });

为什么添加 Firestore 数据库会导致 Cloud Run 错误?

错误:(gcloud.run.deploy) Cloud Run 错误:容器无法启动。无法启动并侦听 PORT 环境变量定义的端口。此修订的日志可能包含更多信息。

FROM node:14

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . . 

CMD ["npm", "start"]

【问题讨论】:

  • 你的容器是本地启动的吗?日志中有哪些错误?您确定在您的容器中设置了 GOOGLE_APPLICATION_CREDENTIAL 吗? (以及为什么要使用它。Cloud Run 上不需要它!)

标签: google-cloud-firestore google-cloud-run google-cloud-build


【解决方案1】:

我变了

const db = new Firestore({
  projectId: 'kuracao-db',
  keyFilename: path.join(__dirname, process.env.GOOGLE_APPLICATION_CREDENTIALS)
});

const db = new Firestore();

它有效,现在我想知道为什么。从 Cloud Run 运行时不需要凭据?有道理,但这与端口错误有什么关系?

【讨论】:

  • 大多数 Google Cloud 产品在云(GCE、GAE、GCF、Cloud Run 等)上运行时会自动从环境中获取凭据。你不需要设置GOOGLE_APPLICATION_CREDENTIALS。要在本地重现相同的行为,请使用 gcloud auth application-default login(而不是设置该环境变量)。我猜你的代码崩溃了。
猜你喜欢
  • 2021-11-10
  • 2021-10-07
  • 2021-11-10
  • 2019-09-03
  • 1970-01-01
  • 2021-06-13
  • 1970-01-01
  • 2021-05-13
  • 2021-07-26
相关资源
最近更新 更多