Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next.js duild fails in Docker due to missing DATABASE_URL with Prisma and BetterAuth #77436

Open
mcorbelli opened this issue Mar 23, 2025 · 3 comments

Comments

@mcorbelli
Copy link

mcorbelli commented Mar 23, 2025

Link to the code that reproduces this issue

https://github.com/mcorbelli/hello-world

To Reproduce

  1. Set up a Next.js project inside a Docker container.
  2. Use BetterAuth for authentication.
  3. Configure authentication settings to retrieve values from the database using Prisma.
  4. Mark app/api/auth/[...all]/route.ts file as dynamic.
  5. Build the project inside Docker: "docker compose -f docker/compose.prod.yml build".

Current vs. Expected behavior

Current behavior:
The build fails with the following error:

Image

Expected behavior:
The build should proceed successfully without Prisma failing due to a missing DATABASE_URL, given that the affected files are marked as dynamic and that the variable is properly set in the environment.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.4.0: Wed Mar 12 21:24:23 PDT 2025; root:xnu-11417.101.13~4/RELEASE_ARM64_T6030
  Available memory (MB): 18432
  Available CPU cores: 12
Binaries:
  Node: 22.12.0
  npm: 10.9.0
  Yarn: N/A
  pnpm: 10.6.3
Relevant Packages:
  next: 15.2.3 // Latest available version is detected (15.2.3).
  eslint-config-next: N/A
  react: 19.0.0
  react-dom: 19.0.0
  typescript: N/A
Next.js Config:
  output: standalone

Which area(s) are affected? (Select all that apply)

Not sure

Which stage(s) are affected? (Select all that apply)

next build (local)

Additional context

I’m trying to deploy my Next.js project in a Docker container, but the build process fails due to a missing DATABASE_URL environment variable.

I use BetterAuth for authentication, and in my package configuration, I retrieve settings from the database using Prisma. The affected files are marked as dynamic (e.g., dynamic = "force-dynamic"), but the issue persists.

Image Image
@mcorbelli mcorbelli changed the title Next.js Build Fails in Docker Due to Missing DATABASE_URL with Prisma and BetterAuth Next.js duild fails in Docker due to missing DATABASE_URL with Prisma and BetterAuth Mar 23, 2025
@zedquach
Copy link

@mcorbelli env file name shouldn't have example. Please rename it to .env or refer to the following link

https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables

@mcorbelli
Copy link
Author

I don’t understand why an .env file would be needed during the build process. From my experience, environment variables like DATABASE_URL are typically set at runtime rather than at build time.

Also, embedding the database URL directly into the image seems risky, as it could expose sensitive credentials if the image is shared or deployed incorrectly. Usually, I rename .env.example to .env.local for local development, while production environments manage secrets separately.

Could you clarify why the build process requires an .env file? Wouldn’t it be safer to provide the database URL at runtime instead?

@soulchild
Copy link

soulchild commented Mar 27, 2025

Welcome to the funny dev world of 2025 where apparently people are creating dedicated builds for specific environments with baked-in environment-specific configuration. I've been building web apps for the last 25 years and configuration was ALWAYS done at run-time. But if you ask why things are the way they are in Next.js-land you get looked at like you're some kind of psycho from the middleages. 😄

Accessing your database from within your build pipeline is also perfectly normal nowadays. Of course, you're using some database SaaS provider and your database is accessible from anywhere (including your CI), right? Right? 😉 But I digress…

In your case, I suppose this happens because the route in app/api/auth/[...all] imports @/lib/auth/server which immediately executes setupAuthServer() which then wants to perform a Prisma query. I had a similar issue using next-auth. It yelled at me during build-time, because it couldn't find the necessary OAuth2 client credentials, which are a runtime-thing in my opinion.

I worked around this by making auth() lazy:

let _auth: Auth | undefined;

export const auth() = () => {
  _auth ??= ... do auth setup
  return _auth;
}

Now I need to use auth() instead of auth throughout my code, but at least auth() isn't evaluated at build-time anymore. Still don't know whether this is a recommended approach though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants