【问题标题】:Cloud Run: Container image builds but Koa server fails to startCloud Run:容器镜像构建,但 Koa 服务器无法启动
【发布时间】:2020-05-20 20:16:16
【问题描述】:

我有一台在本地运行良好但在云上运行不正常的服务器。

Deploying container to Cloud Run service [testserver] in project [buyusedshopify] region [us-central1]
X Deploying... Cloud Run error: Container failed to start. Failed to start and then listen 
on the port defined by the PORT environment variable. Logs for this revision might contain 
more information.                                                                          
  X Creating Revision... Cloud Run error: Container failed to start. Failed to start and th
  en listen on the port defined by the PORT environment variable. Logs for this revision mi
  ght contain more information.                                                            
  . Routing traffic...                                                                     
Deployment failed                                                                          
ERROR: (gcloud.run.deploy) Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. Logs for this revision might contain more information.

我的 Dockerfile:

FROM node:12-slim

# Create app folder
WORKDIR /usr/src/app

# Install app deps. Copy the lock file
COPY package*.json ./
RUN npm install

ENV SCOPES=read_products,write_products,unauthenticated_read_product_listings \
    SHOPIFY_API_KEY=removed \
    SHOPIFY_API_SECRET=removed \
    CLIENT_APP_URL=placeholder

COPY build ./
CMD ["node", "server.js"]

package.json

"buildImage": "gcloud builds submit --tag gcr.io/buyusedshopify/testserver",
"deployCloudRun": "gcloud run deploy --image gcr.io/buyusedshopify/testserver --platform managed",
"buildAndDeploy": "npm run buildImage && npm run deployCloudRun",

这是src 中的 server.ts 文件,它编译为 .js 等效项

import path from "path";
require("dotenv").config();
console.log(process.env, `=====process.env=====`);

import Koa from "koa";
import Router from "koa-router";
import session from "koa-session";
import authorizeForShopify, {verifyRequest} from "@shopify/koa-shopify-auth";

const koa = new Koa();
const router = new Router();

const {SHOPIFY_BUYUSED_API_KEY, SHOPIFY_BUYUSED_API_SECRET, SHOPIFY_BUYUSED_SCOPES} = process.env;

koa.keys = [SHOPIFY_BUYUSED_API_SECRET];
koa.use(session({secure: true, sameSite: "none"}, koa));


koa.use(authorizeForShopify({
  apiKey : SHOPIFY_BUYUSED_API_KEY
  , secret : SHOPIFY_BUYUSED_API_SECRET
  , scopes : SHOPIFY_BUYUSED_SCOPES.split(",")
  , afterAuth(ctx: Koa.Context): void {
    console.log(`=====inside afterAuth()=====`);
    const {shop, accessToken} = ctx.session;

    console.log({
      message : "from inside afterAuth()"
      , shop
      , accessToken
    });

    ctx.redirect("/");
  }
}));


router.get('/', async ctx => {
  ctx.body = "Koa server running, '/' route triggered"
});

router.get('/2nd', async ctx => {
  ctx.response.body = "2nd route message";
});

////// Protected Routes //////
koa.use(verifyRequest());



koa.use(router.routes())
  .use(router.allowedMethods());

const port: number = Number(process.env.PORT) || 3000;

koa.listen(port, undefined, undefined, () => console.log(`=====Koa listening on port ${port.toString()}=====`));

知道问题是什么吗?当最小化为单个路由,然后是侦听方法时,它更早地工作。从那以后,我添加了 shopify 相关库、一些路由和 dotenv 用于本地环境处理。

备注

  1. build 没有被 .gitignored。 Dockerfile 或谷歌云没有忽略文件。我只是 .gitignore ing node_modules.idea.env
  2. 我正在运行 npm 脚本buildAndDeploy。构建始终有效,部署到 Cloud Run 始终失败。

也试过了

COPY build ./ // in Dockerfile CMD ["node", "build/server.js"](使用上述COPY命令时)

编辑

评论和相关的 SO 帖子建议将 PORT 变量设置为 8080。

我将这样做作为后备,但检查我在下面发布的解决方案,您可以看到实际错误出现在 COPY 和 CMD 语句的 Dockerfile 配置中。 Dockerfile 中的 .env 中没有反映更新后的名称也存在 Env 变量问题。

【问题讨论】:

  • 错误信息的哪一部分令人困惑?您的容器未在 $PORT (8080) 上侦听。 Cloud Run 等待您的容器侦听该端口,然后超时终止您的容器。堆栈驱动程序可能有其他详细信息。
  • 暴露 8080 端口。

标签: google-cloud-platform google-cloud-run


【解决方案1】:

进行了三处更改:

  1. .env 中的环境变量名称已更改,但在 Dockerfile 中未更新,已修复
  2. COPY build ./ 必须是 COPY build ./build 才能镜像本地路径。
  3. 我将脚本命令更新为 CMD ["node", "build/server.js] 以说明从 COPY 语句中移动的文件。

通过这三个修复,服务器配置应该镜像本地主机。已在 Cloud Run 上成功部署。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-18
    • 2019-10-12
    • 2021-12-26
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    相关资源
    最近更新 更多