issue with docker not connecting to localhost

hi, I am trying to run a monorepo, frontend nextjs, backend postgreSQL with drizzle studio, docker containerizes both the database and the app. the project is a team project and everyone else from the team has no issue running the server. I have the same issue running it both on mac and windows. I execute pnpm docker:db:down && pnpm docker:db:init and pnpm dev and this error shows up (see screenshot). checks I made: database and app run on the same docker network, etc/hosts file - 127.0.0.1 resolves to localhost, I can access database both in drizzle studio and via the CL, all env variables and docker config files are correct (as I said it works for all other team members), I deleted and reinstalled node modules , deleted and reinstalled docker. I need a different perspective on this as I have been debugging it for nearly 10 days now. Thank you.
No description
19 Replies
ἔρως
ἔρως2mo ago
db.localtest.me wasnt found add the database and the ip to the hosts file of your os on windows, it's in %windir%\System32\Drivers\etc and MUST be edited with admin rights
mioara
mioaraOP2mo ago
thanks for the suggestion. what I did: checked docker network to grab the IP, with this command docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' c88a8163f662. I grabbed the network IP and pasted in the hosts file with the database name. when I run psql -h <network IP> -U postgres -d postgres, I get error connection failed. but when I run psql -h host.docker.internal -U postgres -d postgres, I can access the databse but still the localhost server won't run. page is loading until the error appears again. so it connects to host.docker.internal but not to the docker network IP?
ἔρως
ἔρως2mo ago
the request is going to db.localtest.me
mioara
mioaraOP2mo ago
so in hosts file I need to have network ip db.localtest.me ?
ἔρως
ἔρως2mo ago
in windows, mac and/or linux and that is not an ip: it's a domain name but your dns doesnt know what that is, so, cant get the ip address to comunicate
mioara
mioaraOP2mo ago
thanks for the suggestions, I did this but still the error persists..
ἔρως
ἔρως2mo ago
did you add that address to all hosts files in all oses? actually, did you add it to the hosts file of the os that has docker running in it?
mioara
mioaraOP2mo ago
ok I will tell you what commands I run on mac and the output. please forgive me, my first time working with docker and backend, I am a frontend dev, but being part of a team project I encountered this issue as the app runs on docker. so on mac first I ran docker info | grep "OSType" which logged OSType: linux. so docker runs inside a VM, right?
ἔρως
ἔρως2mo ago
i have no idea maybe, maybe not i have no idea
mioara
mioaraOP2mo ago
next I ran this docker network inspect bridge | grep Gateway which returned this "Gateway": "172.17.0.1"
ἔρως
ἔρως2mo ago
i would need to be in your mac to see it, which i dont want to do
mioara
mioaraOP2mo ago
it's this IP the one I need to set in hosts file?
ἔρως
ἔρως2mo ago
no
mioara
mioaraOP2mo ago
or the host
ἔρως
ἔρως2mo ago
in whatever is running docker, you need to open the hosts file and add the database domain there
mioara
mioaraOP2mo ago
thank you for your help. What I did to fix this error on mac: modified the docker daemon.json file to add a custom dns server, modified the etc/hosts file so that 127.0.0.1 points to my custom domain and make sure the postgres server is running inside docker container. it was a very tricky error but your suggestions helped
ἔρως
ἔρως2mo ago
my suggestion was half the work putting the domain name in the hosts file is 50% of the work but the rest ... way way way way way way over my head but it's awesome that you fixed it now, if your group has a place to document your fix, DO IT you WILL forget it
mioara
mioaraOP2mo ago
yes It's already documented in a google docs. thanks
ἔρως
ἔρως2mo ago
you're welcome

Did you find this page helpful?