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 ??
-
We're gonna need more information. How did you start the docker container? Is the port exposed? – mtricht Jun 29 '19 at 23:49
-
Yes, it is port exposed and with shared volume, that's it – t2149573 Jun 29 '19 at 23:50
-
Is the DB running inside of a container? – BMitch Jun 30 '19 at 02:12
-
No, that would have been much easier @BMitch – t2149573 Jun 30 '19 at 06:32
-
In that case, I believe this is a duplicate. I've just posted an answer over there summarizing the different solutions and when to use each. And there are many other good answers on that question that you hopefully find useful. – BMitch Jun 30 '19 at 15:31
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

- 11,063
- 3
- 36
- 61
-
That's interesting, but it won't work in my case I'm not running MySQL from container, It's on the server, Binding it to 0.0.0.0 as is will expose it outside the server and I think you mean -p not -v – t2149573 Jun 30 '19 at 06:54
-
@t2149573, Sorry my bad, I got it wrong at the beginning, I have updated my answer – Mostafa Hussein Jun 30 '19 at 07:15
-
it solves partially my problem, after doing it i can connect to my database container nevertheless i've lost the port exposal with -p (can't do both) so my app running inside a container can't be reached from host machine :/ any idea ? – Nizar YAKOUBI Apr 26 '23 at 09:29
Trying to describe a bit better what I understand from your question at first:
You have a host with mysql installed (not dockerized, but directly on your host)
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)
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)
You want your dockerized service to remain isolated, with no other contact with anything else outside your container (but the MySQL server, of course)
⚠️ 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)?
ManuallyYou 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!

- 554
- 3
- 10
-
1Actually this is the first thing I tried as I said in the question < I tried 172.17.0.1 but I get connection refused>, It worked only when the I changed the database to be publicly accessible. mysql being binded to 127.0.0.1 will only be accessible via this interface only not even the docker gateway (172.17.0.1) can see it. I think I'm gonna try the --net=host to see if it works for me, I think it's the only logical solution for now. Thanks for the long answer though, it's a good reference if the database is publicly accessible – t2149573 Jul 01 '19 at 09:52
-
-
host.docker.internal you can use this one instead of localhost – shrikanta mazumder Dec 13 '22 at 10:55