I have a server that runs fine locally but not on 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 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.
My 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",
Here is the server.ts file inside src
that compiles to the .js equivalent
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()}=====`));
Any idea what the issue is? It was working earlier when minimalized down to just a single route and then the listen method. I've since added shopify related libraries, a couple routes, and dotenv for local env handling.
Notes
build
is not being .gitignored. There is no ignore file for Dockerfile or Google cloud. I am only .gitignore ingnode_modules
,.idea
, and.env
- I am running the npm script
buildAndDeploy
. The build always works, the deploy onto Cloud Run always fails.
Also Tried
COPY build ./ // in Dockerfile
CMD ["node", "build/server.js"]
(when using the above COPY command)
Edit
Comments and a related SO post advised setting the PORT variable to 8080.
I will do this as a fallback but checking my posted solution below, you can see the actual error was in the Dockerfile configuration on the COPY and CMD statements. There were also Env variable issues from not reflecting updated names in .env inside Dockerfile.