Cannot connect postgress and nextjs using Docker
Unanswered
1pharaxh posted this in #help-forum
1pharaxhOP
Hey guys I keep getting erros while trying to connect to PG using drizzle ORM
this is the error
Error: connect ECONNREFUSED ::1:5432
Source
actions/auth.ts (92:16) @ async login
90 |
91 | // 2. Query the database for the user with the given email
92 | const user = await db.query.users.findFirst({
| ^
93 | where: eq(users.email, validatedFields.data.email),
94 | });
95 |
this is my docker file
FROM node:18-alpine
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
COPY next.config.mjs ./next.config.mjs
EXPOSE 3000
RUN apk add --no-cache postgresql-client
CMD ["sh", "-c", "npm run db & npm run dev"]
docker-compose.yml
version: "3.8"
services:
app:
build:
context: .
dockerfile: Dockerfile
ports:
"3000:3000"
environment:
NODE_ENV=development
DATABASE_URL=postgres://myuser:mypassword@db:5432/mydb?schema=public
SECRET="mysecret"
develop:
watch:
action: sync
path: ./app
target: /app
ignore:
.git
node_modules
action: rebuild
path: package.json
depends_on:
db
db:
image: postgres:15-alpine
environment:
POSTGRES_DB: mydb
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
ports:
"5432:5432"
where
"db": "drizzle-kit migrate && drizzle-kit generate && drizzle-kit push"
I am able to see the env variable if I do console.log don't know why it won't connect
Any help is appreciated.
this is the error
Error: connect ECONNREFUSED ::1:5432
Source
actions/auth.ts (92:16) @ async login
90 |
91 | // 2. Query the database for the user with the given email
92 | const user = await db.query.users.findFirst({
| ^
93 | where: eq(users.email, validatedFields.data.email),
94 | });
95 |
this is my docker file
FROM node:18-alpine
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
COPY next.config.mjs ./next.config.mjs
EXPOSE 3000
RUN apk add --no-cache postgresql-client
CMD ["sh", "-c", "npm run db & npm run dev"]
docker-compose.yml
version: "3.8"
services:
app:
build:
context: .
dockerfile: Dockerfile
ports:
"3000:3000"
environment:
NODE_ENV=development
DATABASE_URL=postgres://myuser:mypassword@db:5432/mydb?schema=public
SECRET="mysecret"
develop:
watch:
action: sync
path: ./app
target: /app
ignore:
.git
node_modules
action: rebuild
path: package.json
depends_on:
db
db:
image: postgres:15-alpine
environment:
POSTGRES_DB: mydb
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
ports:
"5432:5432"
where
"db": "drizzle-kit migrate && drizzle-kit generate && drizzle-kit push"
I am able to see the env variable if I do console.log don't know why it won't connect
Any help is appreciated.
18 Replies
it's a yml file so the number of spaces in front of every line is important, and in the message above that part is all messed up
also, get for yourself a
network
services:
app:
# ...
networks:
- webnet
db:
# ...
networks:
- webnet
networks:
webnet:
1pharaxhOP
version: "3.8"
services:
app:
build:
context: .
dockerfile: Dockerfile
ports:
- "3000:3000"
environment:
- NODE_ENV=development
- DATABASE_URL=postgres://myuser:mypassword@db:5432/mydb?schema=public
- SECRET="mysecret"
develop:
watch:
- action: sync
path: ./app
target: /app
ignore:
- .git
- node_modules
- action: rebuild
path: package.json
networks:
- headway-network
depends_on:
- db
db:
image: postgres:15-alpine
environment:
POSTGRES_DB: mydb
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
ports:
- "5432:5432"
networks:
- headway-network
networks:
headway-network:
driver: bridge
FROM node:18-alpine
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
COPY next.config.mjs ./next.config.mjs
EXPOSE 3000
RUN apk add --no-cache postgresql-client
CMD ["sh", "-c", "npm run db & npm run dev"]
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"db": "drizzle-kit migrate && drizzle-kit generate && drizzle-kit push"
},
Thanks for helping @joulev here are the docker files. I still get the error
Error: connect ECONNREFUSED ::1:5432
Source
actions/auth.ts (92:16) @ async login
90 |
91 | // 2. Query the database for the user with the given email
92 | const user = await db.query.users.findFirst({
| ^
93 | where: eq(users.email, validatedFields.data.email),
94 | });
95 |
Hmm say you still run the docker containers. Can you connect to the database, from outside the docker container, using the database connection string above? Except now it is localhost:5432 instead of db:5432
This is bizarre, your docker files look fine
1pharaxhOP
Yes I can connect to the db on docker IF I switch the mypassword@db to mypassword@localhost in the env variable.
1pharaxhOP
Do you think it is a windows specific issue ?
Sorry, I have no clue
Maybe try WSL, if the issue is still there then it’s not a windows specific issue – that said, docker is supposed to be cross platform so unlikely it is a windows specific issue
You can connect which means the db is online and healthy, which means the app should be able to connect to it… I don’t know why you can’t
Oh wait I think I know why. Give each of your service a container name in docker-compose.yml
Nvm
That’s not the cause
1pharaxhOP
Yeah the problem is I cannot connect with the string db it works if I use localhost but thanks for your help