Next.js + MongoDB Private URL on Railway - Connection Issues During Deployment

massalinux
HOBBY

3 months ago

Hey everyone,

I'm running into a problem with my Next.js 15 application deployed on Railway. I'm trying to connect to MongoDB using its private URL (the xxx.railway.internal one), but the connection fails during the deployment phase.

Here's the setup:

  • Framework: Next.js 15

  • Database: MongoDB (hosted on Railway, using the private network URL)

The Issue:

During deployment, my Next.js app seems unable to establish a connection with the MongoDB instance via its private URL. I understand that Railway's private network might not be available during the build phase, and I suspect this might be the culprit if Next.js is trying to connect at that point (e.g., for SSG or pre-rendering).

What I've considered/checked:

  • My environment variables for the MongoDB private URL are set in Railway.

  • I'm trying to ensure the connection is made at runtime rather than build time.

  • Both my Next.js app and MongoDB are in the same Railway project and environment.

My Question:

Has anyone encountered a similar issue with Next.js on Railway and MongoDB private URLs? Any advice on:

  • The best practices for managing database connections in Next.js on Railway to avoid build-time issues with private networking?

  • Specific Railway configurations I might be missing?

  • How to effectively debug this during a Railway deployment? (e.g., are there specific logs or things to look for beyond the main deployment log?)

Any help or pointers would be greatly appreciated! Thanks in advance.

Solved$10 Bounty

3 Replies

massalinux
HOBBY

3 months ago

Here the debug log

code: undefined,

[cause]: [MongoNetworkError: getaddrinfo ENOTFOUND mongodb.railway.internal] {

errorLabelSet: Set(1) { 'ResetPool' },

beforeHandshake: false,

[cause]: [Error: getaddrinfo ENOTFOUND mongodb.railway.internal] {

errno: -3008,

code: 'ENOTFOUND',

syscall: 'getaddrinfo',

hostname: 'mongodb.railway.internal'

}


qgriffith
HOBBY

3 months ago

Railway sent out an email earlier this week that they disabled the feature that allowed access to the private network during the build phase.....They have now asked people to start using this https://docs.railway.com/guides/pre-deploy-command However the pre-deploy phase does not share the same volume as the deployed container, and you can't copy files. So I have no idea how this will work with a NextJS deploy that does pre-rendering. Thankfully I haven't had to re-deploy my nextjs app yet...but when I do I too will be broken. I think the only way around this is to deploy using a docker container, but that would mean the build process needs to proxy to the external DB connection, which of course has an additional cost. Starting to re-think my future on Railway because of this choice they made.


massalinux
HOBBY

3 months ago

For those who has this issue i solved setting a custom build command where you expose DB PUBLIC URL environment variable like "MONGODB_URL=xxxx npm run build" while using the PRIVATE URL in the railway environment variable sections to be used at runtime.


Status changed to Solved chandrika 3 months ago


Status changed to Solved chandrika about 2 months ago