P
Prisma•3mo ago
Psuedo

NextJS + Prisma + Docker | "can't reach db server"

I will supply all of my files in comments because of the character limit. The first issue is that when I run my docker compose up, it generates the prisma client, then it runs npm build. On the build, it cannot connect to the database server (shown on the included image) For context, I am using this as a reference: https://github.com/99lalo/nextjs-prisma-docker docker-compose.yml
services:
postgres:
image: postgres:latest
environment:
POSTGRES_DB: $POSTGRES_DB
POSTGRES_USER: $POSTGRES_USER
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
ports:
- 5432:5432
expose:
- 5432

frontend:
build: .
restart: always
environment:
- NODE_ENV=production
ports:
- 3001:3000
env_file: .env
depends_on:
- postgres
services:
postgres:
image: postgres:latest
environment:
POSTGRES_DB: $POSTGRES_DB
POSTGRES_USER: $POSTGRES_USER
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
ports:
- 5432:5432
expose:
- 5432

frontend:
build: .
restart: always
environment:
- NODE_ENV=production
ports:
- 3001:3000
env_file: .env
depends_on:
- postgres
No description
16 Replies
Psuedo
PsuedoOP•3mo ago
Dockerfile: https://pastebin.com/QHJdMmJi package.json: https://pastebin.com/7wfWMjQf .env: https://pastebin.com/PYKkx8k2 I can supply anything else that might be needed. I've been stuck on this for a couple of days just trying different things 😦
Yetzederixx
Yetzederixx•3mo ago
So your postgres instance is essentially on the same image? Note; I don't actually know how to fix this, but it's a docker networking issue I can almost guarantee your host isn't correct DATABASE_URL="postgresql://postgres:postgres@postgres:5432/postgres?schema=public" The really real question here is: is this how you actually plan to deploy it out live?
Psuedo
PsuedoOP•3mo ago
I had planned to use this docker compose, yes. However, I'm willing to change it up if need be. I'm just trying to get anything working then I will work towards a cleaner solution. I'm all ears 🙂
Yetzederixx
Yetzederixx•3mo ago
What I do, is have a postgres server on my local rig, and just point my local build at it. For any remote deployment I'll have RDS/GCP SQL/DO db/etc
Psuedo
PsuedoOP•3mo ago
I see. So you just decouple the frontend and db?
Yetzederixx
Yetzederixx•3mo ago
Look, you can save some bucks having the db on the same rig (make sure it has a persistant volume or it's gonna go poof on reboot), but the hassle isn't worth it imo as I can't think of an implementation I've seen in the last 12 years that actually used an on-server postgres like this except for a test suite That's not saying it's not done, or that you shouldn't solve this, but the is solving it gonna be worth it lol
Psuedo
PsuedoOP•3mo ago
I'm also just needing something quick. This is something needed for work that will be deployed for a few weeks and then taken down. I just want to take the path of least resistance lol
Yetzederixx
Yetzederixx•3mo ago
tl;dr, figure out how cross service chatter actually works so you can address the postgres image from your front/backend image Digital ocean db's are $15/month, RDS has a free tier, Neon... I'm not impressed with their pricing unless they've fixed egress pricing in the past year So, is your time worth more than $15 is what I'm saying
Psuedo
PsuedoOP•3mo ago
I hear you, and I'm willing to change how I'm hosting. I just have to figure out how to navigate this with work things. I don't directly have access to any of our hosting stuff. Thanks for the input and I will go back to the drawing board
Yetzederixx
Yetzederixx•3mo ago
and yes, this is super insanely common
When you're running a Next.js app with PostgreSQL in Docker, the host for your PostgreSQL connection string depends on how your Docker setup is configured. Here are a couple of common scenarios:

1.
PostgreSQL running in a separate container:
• If PostgreSQL is running in its own Docker container, you should use the name of the PostgreSQL container as the host. For example, if your PostgreSQL container is named postgres, your connection string might look like this:

DATABASE_URL=postgresql://user:password@postgres:5432/mydatabase

1.
PostgreSQL running on the host machine:
• If PostgreSQL is running on your host machine (outside of Docker), you can use host.docker.internal as the host. This allows the Docker container to connect to the PostgreSQL instance running on your host machine:

DATABASE_URL=postgresql://user:[email protected]:5432/mydatabase

Make sure to replace user, password, and mydatabase with your actual PostgreSQL credentials and database namehttps://www.travisluong.com/how-to-develop-a-full-stack-next-js-fastapi-postgresql-app-using-docker/.

Do you need help with anything else related to your Docker setup?
When you're running a Next.js app with PostgreSQL in Docker, the host for your PostgreSQL connection string depends on how your Docker setup is configured. Here are a couple of common scenarios:

1.
PostgreSQL running in a separate container:
• If PostgreSQL is running in its own Docker container, you should use the name of the PostgreSQL container as the host. For example, if your PostgreSQL container is named postgres, your connection string might look like this:

DATABASE_URL=postgresql://user:password@postgres:5432/mydatabase

1.
PostgreSQL running on the host machine:
• If PostgreSQL is running on your host machine (outside of Docker), you can use host.docker.internal as the host. This allows the Docker container to connect to the PostgreSQL instance running on your host machine:

DATABASE_URL=postgresql://user:[email protected]:5432/mydatabase

Make sure to replace user, password, and mydatabase with your actual PostgreSQL credentials and database namehttps://www.travisluong.com/how-to-develop-a-full-stack-next-js-fastapi-postgresql-app-using-docker/.

Do you need help with anything else related to your Docker setup?
Psuedo
PsuedoOP•3mo ago
I was following step 1. I've done very similar things in the past with Docker and using the container name has always worked. Just not sure why it isn't in this one case haha I'm just going to throw it in it's own container and use host ip
Yetzederixx
Yetzederixx•3mo ago
So, I gave it (bing copilot) your dockerfile and it pooped out this
Thanks for sharing your Dockerfile! It looks well-structured. To connect your Next.js app to a PostgreSQL database running in a Docker container, you can use Docker Compose to manage both services. Here's an example docker-compose.yml file that sets up both your Next.js app and PostgreSQL:

version: '3.8'

services:
nextjs:
build: .
ports:
• "3000:3000"

environment:
• DATABASE_URL=postgresql://user:password@postgres:5432/mydatabase

depends_on:
• postgres


postgres:
image: postgres:13
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: mydatabase
volumes:
• postgres_data:/var/lib/postgresql/data


volumes:
postgres_data:

In this setup:
• The nextjs service builds your Next.js app using the Dockerfile you provided.

• The postgres service runs a PostgreSQL database.

• The DATABASE_URL environment variable in the nextjs service uses postgres as the host, which is the name of the PostgreSQL service defined in the same docker-compose.yml file.

Make sure to replace user, password, and mydatabase with your actual PostgreSQL credentials and database name.

Would you like any further assistance with this setup?
Thanks for sharing your Dockerfile! It looks well-structured. To connect your Next.js app to a PostgreSQL database running in a Docker container, you can use Docker Compose to manage both services. Here's an example docker-compose.yml file that sets up both your Next.js app and PostgreSQL:

version: '3.8'

services:
nextjs:
build: .
ports:
• "3000:3000"

environment:
• DATABASE_URL=postgresql://user:password@postgres:5432/mydatabase

depends_on:
• postgres


postgres:
image: postgres:13
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: mydatabase
volumes:
• postgres_data:/var/lib/postgresql/data


volumes:
postgres_data:

In this setup:
• The nextjs service builds your Next.js app using the Dockerfile you provided.

• The postgres service runs a PostgreSQL database.

• The DATABASE_URL environment variable in the nextjs service uses postgres as the host, which is the name of the PostgreSQL service defined in the same docker-compose.yml file.

Make sure to replace user, password, and mydatabase with your actual PostgreSQL credentials and database name.

Would you like any further assistance with this setup?
SO, I'm interested, can you connect to that postgres image with psql or dbeaver or something using that connection string?
Psuedo
PsuedoOP•3mo ago
I was previously using adminer and could connect with the docker container name. But I'm not sure about the connection string
Yetzederixx
Yetzederixx•3mo ago
I just learned about this db host. https://www.instantdb.com/
InstantDB: A Modern Firebase
We make you productive by giving your frontend a real-time databse.
Yetzederixx
Yetzederixx•3mo ago
it's technically postgres on the backside, and stupid new, so it may not work with prisma but who knows
Psuedo
PsuedoOP•3mo ago
thanks for sharing. I'll keep you updated what I end up doing
Want results from more Discord servers?
Add your server