Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't connect to db from docker container [duplicate]

Tags:

docker

mysql

I have MySQL server installed on a server and dockerized project, The DB is not publicly accessible, only from inside the server. It's used by local non dockerized apps too. I want to connect to it from inside the docker with it remaining not publicly accessible, I tried 172.17.0.1 but I get connection refused. the current bind_address is 127.0.0.1, What do you suggest the bind_address would be ??

like image 514
t2149573 Avatar asked Jun 29 '19 23:06

t2149573


2 Answers

You can run the application through the host network mode which will make the container able to connect to the localhost of your main server (the docker host) while keeping the bind-address points to 127.0.0.1.

So you need to run your application like this if you are using docker cli:

docker run --network=host myappimage

In case of docker-compose you will use is in your service:

network_mode: host
like image 199
Mostafa Hussein Avatar answered Nov 13 '22 23:11

Mostafa Hussein


Trying to describe a bit better what I understand from your question at first:

  1. You have a host with mysql installed (not dockerized, but directly on your host)

  2. You have some client apps connecting to that MySQL in your host using your localhost IP and the mysql port (let's say: 127.0.0.1:3306 by default)

  3. Now you created a docker container with another app (inside the container) that you want to have connected with your MySQL server, which is still running in your host, accessible locally, but out of your new container (or any other container)

  4. You want your dockerized service to remain isolated, with no other contact with anything else outside your container (but the MySQL server, of course)

Trying to connect a containerized client with an external mysql server running in the host


⚠️ Well, I'm going to start by ruling out the option of using --net=host (even when it could work in similar cases to allow your services to "live" in your host's network) since you don't want your container to be available from your other host's processes nor having access to anything else (which would happen with host networking)


Connecting to [non-dockerized] services in your host

✔️ In this case, what you actually need to do (from your app inside your container) is to connect to your Docker Host's IP

But not to the IP in your regular local network (e.g.: 192.168.1.5) nor in your public/wlan network, but to the IP that docker assigns to your host within your docker network itself (the network that docker creates to comunicate with and between your containers)

By default (if you don't specify any other network settings for your container) your container should be using the docker's bridge network, which is setup in the host (not inside the container) and commonly named docker0

So, it's as simple as finding the IP corresponding to your Host, and using that one as your mysql client's bind_address (e.g.: jdbc:mysql://172.X.XX.XX:3306 or $dbhost = "172.X.XX.XX"; etc.)


What is my Docker Host IP address?

Ok... and how can I find the IP that docker assigns to my host (the one for the docker's bridge network)?

Manually

You can just list all of your current assigned IP addresses, to find the one corresponding to that docker0 network:

ip -4 addr

or even better by directly filtering the one that you want:

ip -4 addr show docker0

You'll get an output similar to this:

3: docker0: mtu 1500 qdisc noqueue

inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0

valid_lft forever preferred_lft forever

And copy the ip that appears there after inet


Automatically

I like using this script to directly grab my current docker's host IP (especially useful when testing across many hosts):

ip -4 addr show docker0 | awk '$1=="inet" {print $2}' | cut -d/ -f1

Just put it in some handly place (e.g. in your /.bashrc) assigned to some environment variable:

export DOCKER_HOST_IP="$(ip -4 addr show docker0 | awk '$1=="inet" {print $2}' | cut -d/ -f1 )"

Hope it could be useful!

like image 12
SalDevOps Avatar answered Nov 14 '22 01:11

SalDevOps