Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't reach database server at dockerhost #40

Closed
heriisei opened this issue Sep 29, 2020 · 5 comments
Closed

Can't reach database server at dockerhost #40

heriisei opened this issue Sep 29, 2020 · 5 comments
Assignees
Labels

Comments

@heriisei
Copy link

heriisei commented Sep 29, 2020

I followed https://medium.com/@sam_ngu/connecting-to-docker-host-mysql-from-docker-container-linux-ubuntu-766e526542fd to connect to my host MySQL database.

Inside the other container, I could ping the dockerhost container.

My project is using Prisma as a DB access layer, and pointed to the DB via URL like this:
mysql://user:password@dockerhost:3306/schema.

But when I tried to access the DB. I ended up getting this error:
"Can't reach database server at 'dockerhost':'3306'. Please make sure your database server is running at 'dockerhost':'3306'"

All the configurations are set up as mentioned in the above medium article, like the firewall, DB user, etc.

dockerhost:
  image: qoomon/docker-host
  cap_add:
    - NET_ADMIN
    - NET_RAW
  restart: on-failure
  networks:
    - internal

The dockerhost container log is also showing no error.

Docker Host: 172.22.0.1 (default gateway)
Forwarding ports: 0:65535

I would be so happy to get any solution 😃 from you guys.
Thank you

@qoomon
Copy link
Owner

qoomon commented Sep 30, 2020

which os do you use? you may need to bind your database port to 0.0.0.0 or the bridge network gateway in addition to localhost(127.0.0.1), to allow connections from docker host container.
By default most database server do bind only to 127.0.0.1

Use following docker command to get the bridge network gateway IP address
docker network inspect bridge --format='{{( index .IPAM.Config 0).Gateway}}'

@qoomon
Copy link
Owner

qoomon commented Sep 30, 2020

@qoomon qoomon self-assigned this Sep 30, 2020
@heriisei
Copy link
Author

heriisei commented Sep 30, 2020

@qoomon I'm using Linux Ubuntu. My bad, I used to write the bind-address in a wrong file (/etc/mysql/my.cnf), I just realized that my MySQL config file is /etc/mysql/mysql.conf.d/mysqld.cnf.

So I rewrite the bind-address from 127.0.0.1 to 0.0.0.0, because it didn't work with multiple IP address value configuration.
And now the connection between the container and my host database is pretty well established.

I would glad to know if there is any workaround to automatically detect the dockerhost container Gateway IP address and then run a bash command to add a new firewall rule to allow that gateway IP address?
For now, I do manually run docker inspect | grep Gateway to get the IP and manually run the firewall command via terminal, every time after I run docker-compose up --build.

Thank you so much.


Update:
Nevermind, I already make a simple bash script to run after docker-compose, to updating the UFW firewall rule.

Thank you!

@qoomon
Copy link
Owner

qoomon commented Sep 30, 2020

maybe you can contribute your script, that would be nice because you are not the first person having these troubles.

@outranker
Copy link

@heriisei would love to have a look at your script. i am currently having a hard time finding a solution to that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants