Building without a DB connection
One of the most common problems when building a site for production, especially with Docker - is the DB connection requirement.
The important note is that Payload by itself does not have this requirement, But Next.js' SSG does if any of your route segments have SSG enabled (which is default, unless you opted out or used a Dynamic API) and use the Payload Local API.
Solutions:
Using the experimental-build-mode Next.js build flag
You can run Next.js build using the pnpx next build --experimental-build-mode compile
command to only compile the code without static generation, which does not require a DB connection. In that case, your pages will be rendered dynamically, but after that, you can still generate static pages using the pnpx next build --experimental-build-mode generate
command when you have a DB connection.
Opting-out of SSG
You can opt out of SSG by adding this all the route segment files:
Note that it will disable static optimization and your site will be slower. More on Next.js documentation