0
votes

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

  1. build is not being .gitignored. There is no ignore file for Dockerfile or Google cloud. I am only .gitignore ing node_modules, .idea, and .env
  2. 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.

1
Which part of the error message is confusing? Your container is not listening on $PORT (8080). Cloud Run waits for your container to listen on that port and then times out killing your container. Stack driver might have additional details.John Hanley
EXPOSE the 8080 port.Pentium10

1 Answers

2
votes

Three changes were made:

  1. Env variable names were changed in .env but not updated in Dockerfile, that has been fixed
  2. COPY build ./ needs to be COPY build ./build to mirror local pathing.
  3. I updated the script command to be CMD ["node", "build/server.js] to account for the file being moved from the COPY statement.

With these three fixes the server configuration should mirror localhost. It was deployed successfully on Cloud Run.