Google cloud platform 云运行:容器图像建立,但膝关节炎服务器无法启动
我有一台服务器,在本地运行良好,但在云上运行不好Google cloud platform 云运行:容器图像建立,但膝关节炎服务器无法启动,google-cloud-platform,google-cloud-run,Google Cloud Platform,Google Cloud Run,我有一台服务器,在本地运行良好,但在云上运行不好 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 POR
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",
下面是编译为.js等效文件的src
中的server.ts文件
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()}=====`));
知道问题是什么吗?它在更早的时候就已经开始工作了,当时它只使用了一条路线,然后使用了listen方法。此后,我添加了与shopify相关的库、一些routes和dotenv,用于本地环境处理
注释
build
未被忽略。Dockerfile或Google cloud没有忽略文件。我只是.gitingnode\u模块
、.idea
和.env
buildAndDeploy
。构建总是有效的,部署到云上运行总是失败的复制生成。///在Dockerfile中
CMD[“node”,“build/server.js”]
(使用上述复制命令时)
编辑
注释和相关SO post建议将端口变量设置为8080
我将这样做作为一个后备方案,但检查我在下面发布的解决方案,您可以看到实际错误是在COPY和CMD语句的Dockerfile配置中。由于Dockerfile内的.Env中没有反映更新的名称,因此也存在Env变量问题。进行了三项更改:
COPY build./
需要COPY build./build
来镜像本地路径CMD[“node”,“build/server.js]
,以说明从COPY语句中移动的文件使用这三个修复程序,服务器配置应镜像本地主机。它已在云运行时成功部署。进行了三个更改:
COPY build./
需要COPY build./build
来镜像本地路径CMD[“node”,“build/server.js]
,以说明从COPY语句中移动的文件通过这三个修复,服务器配置应该镜像localhost。它已在云运行中成功部署。错误消息的哪一部分令人困惑?您的容器未在$PORT(8080)上侦听。Cloud Run等待容器在该端口上侦听,然后超时终止容器。堆栈驱动程序可能有其他详细信息。请公开8080端口。错误消息的哪一部分令人困惑?您的容器未在$PORT(8080)上侦听。Cloud Run等待容器在该端口上侦听,然后超时终止容器。堆栈驱动程序可能有其他详细信息。请公开8080端口。