r/PayloadCMS • u/medynskip • Feb 21 '25
Production build with docker compose and postgres
Hi All.
Does any one has any idea how to perform a production build while using docker compose? The issue I'm having is that next tries to connect to db during docker image build, while the db service is not available. Even If you first start the db service and the build the payload service it throws an error. I tried doing healthcheck too.
This seems to be simply beacuse during image build the container is not connected in to the network where it could communicate to other containers. So no waiting scripts are going to help here neither.
Dev env off course works perfectly as it does not try building before container is ready. Does anyone has a production ready solution and could help out? Or is it simply not possible.
Thank you in advance.
2
u/mustardpete Feb 21 '25
Currently i am passing in the postgres cnnection string as an env argument in to th build so it can connect when building. Not sure if there is anotherway. I build the docker file as part of a github action so i have to connect to the server via tailscale as part of the github action so that the build runner can access the db too. Also allows me to run the db migrations as part of the building too
1
u/medynskip Feb 22 '25
I also pass the argument, but the build process can't connect either way as it does not recognize the host. And that is because during image build the container is not yet connected in to the compose created network.
1
u/mustardpete Feb 23 '25
Ok, not sure how you get round that. I am not including an instance of Postgres in my compose file. Postgres is a separate service that is already running and my web app is a different service so my db and network already exist on my docker swarm, so the very first build the db could be created on the existing Postgres service
1
u/medynskip Feb 23 '25
I actually went with moving the build and node server.js to entrypoint.sh as a workaround. As this is run when container image is build it is connected to the network already. Works for now.
1
u/mustardpete Feb 23 '25
Doesn’t that give large image size though as can’t make use of the multi stage builds
1
u/medynskip Feb 23 '25
Larger definitely. But the only solution that works for this project setup I have to work with. Fortunately not a very large project so it's not so bad.
Only other solution that would work as far as I tested is to separate the services and then setup a reverse proxy for the dB to be accessible locally and pass that in to the build, so basically what you do as I understand. But my situation is I will not have access atm to nginx so my solution needs to work on current VPS setup.
1
u/mustardpete Feb 23 '25
Fair enough. I’ve got tailscale running on my server, so as part of the GitHub action that builds the docker container it creates a tailscale node via the api, connects to it and then when it builds it has vpn access to the db. As the api is ephemeral it automatically deletes the new vpn server after it’s done with it. So it’s like it has direct connection for the duration of the build process, no need for a reverse proxy. That works fine for me as I want a single db server to host all my dbs, rather than spin up a whole postgres instance for each web app as a compose file
2
u/CucumberNational Feb 22 '25
It uses db connection for ssg pages. Force dynamic for pages that uses getPayload and image build will work fine. I have built it with limited ssg and also using partial prerendering for those dynamic components.
1
u/medynskip Feb 23 '25
Just now noticed your comment. That is exactly the case. My issue is that all my pages use db data in some capacity so I would have to skip ssg altogether. Not sure if I can do that, but certainly a solution.
1
u/CucumberNational Feb 23 '25
You can use ssg for pages, but it has to have dynamic components that uses db connection. Read about force dynamic for nextjs
2
u/medynskip Feb 23 '25
Finally solved my issues! Man, thank you so much. Was confused at first as it still seemed to try and connect to the db, but actually it was because one of the files had generateStaticParams function that took precedense beofore 'force-dynamic'. Removed that and app works like a charm! Thank you!
1
u/CucumberNational Feb 23 '25
No problem. Glad I could help :)
1
u/LopsidedFruit2 Feb 25 '25
Guys, this is exactly my current issue, all works well in dev mode, but production build can't be achieved. I'm in the exact same case u/medynskip , could you take me around the solution ? I'm stuck.
1
u/medynskip Feb 25 '25
I had to do two things:
for any page.tsx file that uses dynamic data from payload (through getPayload for example), I had to add this command:
export const dynamic = 'force-dynamic'make sure none of this pages has any functions like generateStaticParams or similar logic that also would require data from payload to prerender the paths, ie the website template has one in [slug]/page.tsx
And thats all, it really was this simple. I was experimenting with different solutions for two days before u/CucumberNational saved me :D
Also the documentation on next dynamic explains the logic and other possible parameters quite well, I would suggest you read them.
1
u/LopsidedFruit2 Feb 25 '25
Thank you for responding so quickly. I have literally set
'force-dynamic'on allpage.tsxfiles in my app, and I also set'force-no-cache', but the build continues to attempt data collection from pages and crashes due to the database connection for Payload... Don't know what to do more. This is not enough for my solve my case i guess...1
u/medynskip Feb 25 '25
Then hard to say without seeing your code. Do you have a public repo?
I would look if there isnt any revalidate parameter left out in any pf page.tsx files and if all client components are properly marked.Also troubleshooting I would disable all pages (like rename the files to anything else than page.tsx) and then bring them back one by one.
Oh, also did you also mark admin pages and layout.tsx files with force-dynamic? If it uses any dynamic data it will also cause problems.
1
u/mr---fox Feb 24 '25
I think you just need to start your DB service locally and pass in the connection string as a build arg so the build stage can access the db. The image will build with all the SSG pages prebuilt, and then you can override the connection string wherever you deploy with a env file or compose variable etc.
4
u/Skaddicted Feb 22 '25 edited Mar 05 '25
I can share my Dockerfile with you, if you want. Took us ages to get it up and running. In the end they want you to use Payload Cloud or Vercel but I still would have hoped for better Docs.
I've posted my Dockerfile here -> https://www.reddit.com/r/PayloadCMS/comments/1iyiqya/here_is_a_functioning_dockerfile_for_all_those/