So I have a Nginx running inside a docker container, I have a mysql running on localhost, I want to connect to the MySql from within my Nginx. The MySql is running on localhost and not exposing a port to the outside world, so its bound on localhost, not bound on the ip address of the machine.
Is there any way to connect to this MySql or any other program on localhost from within this docker container?
Solution for Windows 10
Docker Community Edition 17.06.0-ce-win18 2017-06-28 (stable)
You can use DNS name of the host
docker.for.win.localhost
, to resolve to the internal IP. (Warning some sources mentionedwindows
but it should bewin
)Overview
I needed to do something similar, that is connect from my Docker container to my localhost, which was running the
Azure Storage Emulator
andCosmosDB Emulator
.The
Azure Storage Emulator
by default listens on 127.0.0.1, while you can change the IP its bound too, I was looking for a solution that would work with default settings.This also works for connecting from my Docker container to
SQL Server
andIIS
, both running locally on my host with default port settings.For those on Windows, assuming you're using the bridge network driver, you'll want to specifically bind MySQL to the ip address of the hyper-v network interface.
This is done via the configuration file under the normally hidden C:\ProgramData\MySQL folder.
Binding to 0.0.0.0 will not work. The address needed is shown in the docker configuration as well, and in my case was 10.0.75.1.
You can get the host ip using alpine image
This would be more consistent as you're always using alpine to run the command.
Similar to Mariano's answer you can use same command to set an environment variable
For macOS and Windows
Docker v 18.03 and above (since March 21st 2018)
Use your internal IP address or connect to the special DNS name
host.docker.internal
which will resolve to the internal IP address used by the host.Linux support pending https://github.com/docker/for-linux/issues/264
MacOS with earlier versions of Docker
Docker for Mac v 17.12 to v 18.02
Same as above but use
docker.for.mac.host.internal
instead.Docker for Mac v 17.06 to v 17.11
Same as above but use
docker.for.mac.localhost
instead.Docker for Mac 17.05 and below
To access host machine from the docker container you must attach an IP alias to your network interface. You can bind whichever IP you want, just make sure you're not using it to anything else.
sudo ifconfig lo0 alias 123.123.123.123/24
Then make sure that you server is listening to the IP mentioned above or
0.0.0.0
. If it's listening on localhost127.0.0.1
it will not accept the connection.Then just point your docker container to this IP and you can access the host machine!
To test you can run something like
curl -X GET 123.123.123.123:3000
inside the container.The alias will reset on every reboot so create a start-up script if necessary.
Solution and more documentation here: https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds
You can use ngrok to create a secure tunnel to your localhost machine and then expose that tunnel to your docker container.
ngrok is free to use as of 05/22/2017.
Steps:
1) go to ngrok
2) download the ngrok client and follow installation instructions
3) SIGN UP for an account and they will provide an authentication token. Signing up is required because ngrok only give you tcp port tunnel after signing up. There is no cost or credit card required to sign up.
4) in your terminal do
ngrok tcp 3306
.3306
is the port that mysql runs on my local, you can do it with any other port as well.5) You will receive an address from step
4
such as this:tcp://0.tcp.ngrok.io:10117
. This is the tunnel connection to your local machine.0.tcp.ngrok.io
is mapped to yourlocalhost
and port10117
is mapped to your local port3306
. Now you can access your localhost port 3306 from anywhere using this address, including any docker container running on this machine. In your docker container(wherever it is), assuming you have mysql client already installed, do the following:mysql --host 0.tcp.ngrok.io --port 10117 -u root
You will be able to log into your
root
account of your local machine from inside of the docker container!I've blogged about this solution see more details here