Best method for networking Docker containers with Postgres and Nextjs (fetching data from DB at build-time) reddit / selfhosted ---------------------------------------------------------------------- skipeeto · 2023-12-19 16:16 I have two docker containers, one with a Nextjs App and one with Postgres. My initial idea is to create a Docker network, add both containers to the network so the Nextjs app can fetch data from the postgresDB with using the container name as the DB hostname. However, this does not work as the Nextjs app needs to fetch some data when the image is BUILT and it is not "networked" at that time. So then instead I used the postgres containers IP address as the DB hostname in the nextjs app and removed the network from both apps. Now it works perfectly fine. However, if I add the Nextjs container to a network, using postgres container's IP address no longer works for connecting to the database (I get connection timed out error). I'd like at least the NextJS app to be on a network as I plan to eventually also have an NGINX container running, but that is for after I get this figured out. ​ Has anyone hosted Nextjs and Postgres in two containers before where the Nextjs app needs to access the Postgres container at build time? I'm not sure how to get this working with networks involved.