【发布时间】: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 用于本地环境处理。
备注
-
build没有被 .gitignored。 Dockerfile 或谷歌云没有忽略文件。我只是 .gitignore ingnode_modules、.idea和.env - 我正在运行 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