简体   繁体   中英

Node JS deployed in Docker container on Cloud Run not fetching data from Mongo DB

Summary: Node/Express API on Cloud Run not connecting to Mongo cluster on MongoDB Atlas

Our Express/Node API is being deployed to Google's new Cloud Run for container deployments. The API, when run locally with docker using docker run -p 8080:8080 <image_id> , fetches data from our Mongo database just fine. The relevant logs from within Cloud Run:

2020-04-22T18:15:15.408778Z(node:1) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connection timed out
2020-04-22T18:15:15.408824Z at new MongooseServerSelectionError (/usr/app/node_modules/mongoose/lib/error/serverSelection.js:22:11)
2020-04-22T18:15:15.408834Z at NativeConnection.Connection.openUri (/usr/app/node_modules/mongoose/lib/connection.js:823:32)
2020-04-22T18:15:15.408844Z at Mongoose.connect (/usr/app/node_modules/mongoose/lib/index.js:333:15)
2020-04-22T18:15:15.408854Z at Object.<anonymous> (/usr/app/models/index.js:11:10)
2020-04-22T18:15:15.408862Z at Module._compile (internal/modules/cjs/loader.js:1123:30)
2020-04-22T18:15:15.408871Z at Object.Module._extensions..js (internal/modules/cjs/loader.js:1143:10)
2020-04-22T18:15:15.408880Z at Module.load (internal/modules/cjs/loader.js:972:32)
2020-04-22T18:15:15.408893Z at Function.Module._load (internal/modules/cjs/loader.js:872:14)
2020-04-22T18:15:15.408902Z at Module.require (internal/modules/cjs/loader.js:1012:19)
2020-04-22T18:15:15.408911Z at require (internal/modules/cjs/helpers.js:72:18)
2020-04-22T18:15:15.408919Z at Object.<anonymous> (/usr/app/config/passport.js:7:12)
2020-04-22T18:15:15.408927Z at Module._compile (internal/modules/cjs/loader.js:1123:30)
2020-04-22T18:15:15.408937Z at Object.Module._extensions..js (internal/modules/cjs/loader.js:1143:10)
2020-04-22T18:15:15.408946Z at Module.load (internal/modules/cjs/loader.js:972:32)
2020-04-22T18:15:15.408954Z at Function.Module._load (internal/modules/cjs/loader.js:872:14)
2020-04-22T18:15:15.408964Z at Module.require (internal/modules/cjs/loader.js:1012:19)
2020-04-22T18:15:15.608498Z(node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
2020-04-22T18:15:15.608686Z(node:1) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

...

2020-04-22T18:19:59.772303ZGET504296 B300.5 sChrome 81 https://node-api-deploy-q62r35344q-uw.a.run.app/api/cbb/home-page-stats
The request has been terminated because it has reached the maximum request timeout. To change this limit, see https://cloud.google.com/run/docs/configuring/request-timeout
2020-04-22T18:20:26.472145ZGET504290 B300 sChrome 81 https://node-api-deploy-q62r35344q-uw.a.run.app/api/cbb/teams/list
The request has been terminated because it has reached the maximum request timeout. To change this limit, see https://cloud.google.com/run/docs/configuring/request-timeout

This makes it seem clear that the data is not being fetched, and the requests are then timing out. My Mongo database is hosted in MongoDB Atlas, and I haven't done anything in particular (whitelist the IP of the cloud run server, etc.) that I might have to do to make this work. Does cloud run even have an IP, or no because it is serverless. Maybe I have to pass the DB URI to cloud run through an env variable? I'm not sure.

Edit

Here's a relevant Stack Overflow post that also mostly went unanswered.

Cloud Run now has support for configuring VPC egress settings that lets you define a static IP for outbound requests through Cloud NAT. You can follow this step by step guide in the documentation to configure a static IP to whitelist at MongoDB Atlas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM