Why Your Payload CMS Docker Build Fails on MongoDB (and Why It Should Never Need It)
If you deploy a Next.js + Payload CMS site as a Docker image, sooner or later you will meet this error in your build logs: Error: missing secret key. A secret key is needed to secure Payload. payloadInitError: true > Build error occurred Error: Failed to collect page data for /posts/[slug] And if you "fix" that with a dummy secret, its sibling shows up right behind it: ERROR: cannot connect to MongoDB. Details: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://" Error occurred prerendering page "/" The confusing part: your app works perfectly in development, and the database is up and healthy in production. So why does building an image want to talk to MongoDB at all? To answer that, we need to look at what Next.js and Payload each do — and at exactly when and where your code runs. Part 1: The cast of characters Next.js is not just a compiler When you run next build , two very different things h...