By Phil


2014-06-20 03:54:16 8 Comments

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?

20 comments

@prabhakar palanivel 2019-04-18 15:47:41

Running the mysql docker container on thee host network (by starting the docker with "--network host ") seems to be the straightforward solution.

@patryk.beza 2019-03-22 14:10:42

Until fix is not merged into master branch, to get host IP just run from inside of the container:

ip -4 route list match 0/0 | cut -d' ' -f3

(as suggested by @Mahoney here).

@Felipe Toledo 2019-03-22 02:40:19

Very simple and quick, check your host IP with ifconfig (linux) or ipconfig (windows) and then create a

docker-compose.yml

version: '3' # specify docker-compose version

services:
  nginx:
    build: ./ # specify the directory of the Dockerfile
    ports:
      - "8080:80" # specify port mapping
    extra_hosts:
      - "dockerhost:<yourIP>"

This way, your container will be able to access your host. When accessing your DB, remember to use the name you specified before, in this case "dockerhost" and the port of your host in which the DB is running

@Thomasleveil 2014-06-20 11:46:51

Edit: If you are using Docker-for-mac or Docker-for-Windows 18.03+, just connect to your mysql service using the host host.docker.internal.

As of Docker 18.09.3, this does not work on Docker-for-Linux. A fix has been submitted on March the 8th, 2019 and will hopefully be merged to the code base. Until then, a workaround is to use a container as described in qoomon's answer.


TLDR

Use --network="host" in your docker run command, then 127.0.0.1 in your docker container will point to your docker host.

Note: This mode only works on Docker for Linux, per the documentation.


Note on docker container networking modes

Docker offers different networking modes when running containers. Depending on the mode you choose you would connect to your MySQL database running on the docker host differently.

docker run --network="bridge" (default)

Docker creates a bridge named docker0 by default. Both the docker host and the docker containers have an IP address on that bridge.

on the Docker host, type sudo ip addr show docker0 you will have an output looking like:

[[email protected]:~] $ sudo ip addr show docker0
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 56:84:7a:fe:97:99 brd ff:ff:ff:ff:ff:ff
    inet 172.17.42.1/16 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::5484:7aff:fefe:9799/64 scope link
       valid_lft forever preferred_lft forever

So here my docker host has the IP address 172.17.42.1 on the docker0 network interface.

Now start a new container and get a shell on it: docker run --rm -it ubuntu:trusty bash and within the container type ip addr show eth0 to discover how its main network interface is set up:

[email protected]:/# ip addr show eth0
863: eth0: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 66:32:13:f0:f1:e3 brd ff:ff:ff:ff:ff:ff
    inet 172.17.1.192/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::6432:13ff:fef0:f1e3/64 scope link
       valid_lft forever preferred_lft forever

Here my container has the IP address 172.17.1.192. Now look at the routing table:

[email protected]:/# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         172.17.42.1     0.0.0.0         UG    0      0        0 eth0
172.17.0.0      *               255.255.0.0     U     0      0        0 eth0

So the IP Address of the docker host 172.17.42.1 is set as the default route and is accessible from your container.

[email protected]:/# ping 172.17.42.1
PING 172.17.42.1 (172.17.42.1) 56(84) bytes of data.
64 bytes from 172.17.42.1: icmp_seq=1 ttl=64 time=0.070 ms
64 bytes from 172.17.42.1: icmp_seq=2 ttl=64 time=0.201 ms
64 bytes from 172.17.42.1: icmp_seq=3 ttl=64 time=0.116 ms

docker run --network="host"

Alternatively you can run a docker container with network settings set to host. Such a container will share the network stack with the docker host and from the container point of view, localhost (or 127.0.0.1) will refer to the docker host.

Be aware that any port opened in your docker container would be opened on the docker host. And this without requiring the -p or -P docker run option.

IP config on my docker host:

[[email protected]:~] $ ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:98:dc:aa brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe98:dcaa/64 scope link
       valid_lft forever preferred_lft forever

and from a docker container in host mode:

[[email protected]:~] $ docker run --rm -it --network=host ubuntu:trusty ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:98:dc:aa brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe98:dcaa/64 scope link
       valid_lft forever preferred_lft forever

As you can see both the docker host and docker container share the exact same network interface and as such have the same IP address.


Connecting to MySQL from containers

bridge mode

To access MySQL running on the docker host from containers in bridge mode, you need to make sure the MySQL service is listening for connections on the 172.17.42.1 IP address.

To do so, make sure you have either bind-address = 172.17.42.1 or bind-address = 0.0.0.0 in your MySQL config file (my.cnf).

If you need to set an environment variable with the IP address of the gateway, you can run the following code in a container :

export DOCKER_HOST_IP=$(route -n | awk '/UG[ \t]/{print $2}')

then in your application, use the DOCKER_HOST_IP environment variable to open the connection to MySQL.

Note: if you use bind-address = 0.0.0.0 your MySQL server will listen for connections on all network interfaces. That means your MySQL server could be reached from the Internet ; make sure to setup firewall rules accordingly.

Note 2: if you use bind-address = 172.17.42.1 your MySQL server won't listen for connections made to 127.0.0.1. Processes running on the docker host that would want to connect to MySQL would have to use the 172.17.42.1 IP address.

host mode

To access MySQL running on the docker host from containers in host mode, you can keep bind-address = 127.0.0.1 in your MySQL configuration and all you need to do is to connect to 127.0.0.1 from your containers:

[[email protected]:~] $ docker run --rm -it --network=host mysql mysql -h 127.0.0.1 -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 36
Server version: 5.5.41-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

note: Do use mysql -h 127.0.0.1 and not mysql -h localhost; otherwise the MySQL client would try to connect using a unix socket.

@Ben 2015-05-12 05:45:49

Thank you for such a detailed answer! From what I've gathered, using host mode is the only way to get this functionality through localhost. I haven't tried but I would assume you could create a separate network to connect containers over their own bridge offering them a common 'localhost'.

@Thomasleveil 2015-05-12 08:26:27

There is also a guide on Advanced Networking at docs.docker.com/articles/networking

@Charlie Dalsass 2015-10-01 22:19:29

Note for OSX users: log into your docker virtual machine first (boot2docker), using "docker-machine ssh default" , then run "sudo ip addr show docker0". Continue with Thomas' instructions from there.

@zx1986 2016-06-30 03:35:52

I am running Docker for Mac, and there is no 172.17.42.1 anymore, no docker0 anymore. It was 172.17.0.1 as gateway, and can't even telnet 172.17.0.1 3306

@Jonah 2016-08-10 07:41:10

There is a reason they didn't try to make this easy. It's better to have all the dependencies dockerized as well, which allows you to sidestep this whole problem.

@Code Commander 2016-08-18 18:44:02

I'm also running Docker for Mac and want to use Xdebug remote debugging with my container. It doesn't make sense to have my IDE (IntelliJ) run as a dependency in a container. Is there no longer a way to telnet out from in the container?

@chx 2016-09-02 05:30:46

You can mount the mysql socket into the container instead of networking like -v /var/run/mysqld/mysqld.sock:/tmp/mysql.sock this.

@e4c5 2016-12-22 05:23:19

one of those rare occaisions when an answer is really worth the hundreds of votes it has recieved

@TheJKFever 2017-02-09 19:10:48

Can someone address the situation when your running Docker on Mac and not using boot2docker as such there is no docker0 interface?

@ospider 2017-07-13 09:16:22

if you get access denied from inside docker container, you may need to allow root access outside localhost, with GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;

@user64141 2017-08-24 13:47:20

What would this command be ("sudo ip addr show docker0") if you're running Docker for Windows?

@user64141 2017-10-26 20:11:59

What if you want to access a machine on the host machine's network (not the host machine's IP)? I tried using both bridge and host mode, and both times I'm unable to access the IP address from the docket container.

@MikeV 2018-03-21 23:50:07

For anyone trying to connect to a host service and running Docker for Mac. There is special Mac-only DNS name docker.for.mac.host.internal, which resolves to the internal IP address used by the host. source

@funseiki 2018-04-19 20:33:30

The latest version of Docker supports host.docker.internal as a domain name from the container (as per the latest update in @Janne Annala 's answer ). Going forward, that appears to be the more general solution when you still want some level of isolation between the container and host networks.

@Thomasleveil 2018-04-20 05:55:58

@funseiki note that for docker 18.04.0-ce host.docker.internal does not work for Linux. It only works for docker-for-Windows and docker-for-mac. See github.com/docker/for-linux/issues/264

@funseiki 2018-04-21 06:23:51

@Thomasleveil - Ah, that's unfortunate. Thanks for the clarification.

@Eric 2018-05-02 17:26:25

--net=host breaks published port translation so that if the container exposes port 80 (using --publish XXX:80 or more simply -p XXX:80 then if a service on the host is already bound to port 80 (a docker instance or a host native service) then the container will not be able to bind the port 80. So it's not good in any situation.

@Kostya Vyrodov 2018-06-01 14:29:24

Please make this phrase 'As of Docker 18.04, this does not work on Docker-for-Linux.' as bold.

@Vano 2018-07-03 18:56:17

For the bridge mode case, would you need to configure iptables as well to accept incoming traffic from container?

@ctrl-alt-delor 2018-09-02 16:45:02

ip addr show docker0 does not need sudo

@Danilo Caetano 2018-10-01 14:33:13

How can I use it on Ubuntu(linux)?

@Clay Lenhart 2018-10-12 20:57:46

This might be a better way to get the host IP address: ip -4 addr show docker0 | awk '/inet .*\//{print $2}' | awk '{split($0,a,"/"); print a[1]}'

@azulay7 2019-02-13 09:23:28

I have similar issue with elasticsearch . how can I connect node.js container to elastic search runs on the container host.

@Thomasleveil 2019-03-08 21:11:19

A fix for host.docker.internal to work on Linux has been submitted today. It will hopefully be merged

@Prem Sompura 2019-03-19 09:40:41

Will the IP address of docker0 network interface remain same after restarting the container?

@ToolmakerSteve 2019-03-31 09:56:21

@PremSompura - I'm not an expert, but my understanding is that docker0 IP address is same as bridge's gateway address - which won't be affected by anything docker or docker containers do. OTOH, if you are testing locally, and then deploying elsewhere, docker0 usually won't be same where you deploy to, so you'll need a solution that gets it from its environment anyway. Hence the discussion of host.docker.internal (where available), and export ... to acquire the current value (elsewhere).

@Elad 2018-11-09 22:23:58

None of the answers worked for me when using Docker Toolbox on Windows 10 Home, but 10.0.2.2 did, since it uses VirtualBox which exposes the host to the VM on this address.

@TheManish 2019-03-27 14:25:56

It works. Even no need to specify --network=host. looks like 10.0.2.2 is set as a default IP for the host. Thanks.

@qoomon 2018-10-17 15:08:44

until host.docker.internal is working for every platform you can use my container acting as a NAT gateway without any manually setup https://github.com/qoomon/docker-host

@Lokesh S 2018-09-17 15:18:21

For Windows Machine :-

Run the below command to Expose docker port randomly during build time

$docker run -d --name MyWebServer -P mediawiki

enter image description here

enter image description here

In the above container list you can see the port assigned as 32768. Try accessing

localhost:32768 

You can see the mediawiki page

@user833482 2015-05-27 13:36:34

This worked for me on an NGINX/PHP-FPM stack without touching any code or networking where the app's just expecting to be able to connect to localhost

Mount mysqld.sock from the host to inside the container.

Find the location of the mysql.sock file on the host running mysql:
netstat -ln | awk '/mysql(.*)?\.sock/ { print $9 }'

Mount that file to where it's expected in the docker:
docker run -v /hostpath/to/mysqld.sock:/containerpath/to/mysqld.sock

Possible locations of mysqld.sock:

/tmp/mysqld.sock
/var/run/mysqld/mysqld.sock 
/var/lib/mysql/mysql.sock
/Applications/MAMP/tmp/mysql/mysql.sock # if running via MAMP

@user1226868 2015-06-06 14:45:01

This is a much cleaner solution, not exposing Mysql to the outside (if not using a firewall).

@Joel E Salas 2016-01-19 21:54:48

Sockets do not scale as well as TCP because they block more often and can cause weird behavior. Use TCP whenever possible.

@Private 2016-10-13 09:21:03

@JoelESalas Do you have a source for that claim?

@Private 2016-10-13 10:32:53

I have found this to be the easiest solution. Thumbs up user833482! You should contribute more often to StackOverflow.

@M Conrad 2017-01-11 01:28:04

@JoelESalas I think you're mistaken. The mysql client library even uses unix sockets by default when connecting to localhost rather than actually making a connection to localhost. A unix socket avoids the overhead of the TCP stack and routing, and should run faster.

@Stephens 2017-04-09 00:12:13

I tried this approach but got error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111 "Connection refused") When I do /bin/bash to the container I do see mysql.sock file, meaning that the volume is mounted correctly, and I use brew installed mysql. How can I fix this situation?

@hasnat 2018-06-11 08:47:06

You can get the host ip using alpine image

docker run --rm alpine ip route | awk 'NR==1 {print $3}'

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

DOCKER_HOST=$(docker run --rm alpine ip route | awk 'NR==1 {print $3}') docker-compose up

@Ralph Willgoss 2017-11-09 10:49:13

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 mentioned windows but it should be win)

Overview
I needed to do something similar, that is connect from my Docker container to my localhost, which was running the Azure Storage Emulator and CosmosDB 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 and IIS, both running locally on my host with default port settings.

@Janne Annala 2017-04-21 11:33:46

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 localhost 127.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

@Richard Frank 2017-07-22 05:46:28

brilliant and thanks, this worked for me from within the container. mysql -uroot -hdocker.for.mac.localhost

@99Sono 2017-12-03 15:06:38

docker.for.mac.localhost Is exactly what I was looking for. But this is dirty as hell at the same time. IN docker, one would expect that the hook docker.for.mac.localhost would be a generic docker internal name that would be valid for any operating system, not just for Mac. But for development purposes this is good enough.

@99Sono 2017-12-03 16:38:33

@Achilleus 2017-12-19 15:59:36

How do I access a port which is exposed at say 9093.. I am trying to telnet docker.for.mac.localhost 9093 . Its unreachable but if I ping docker.for.mac.localhost , its reachable.Am i missing something here?

@Jay 2018-02-19 13:46:17

DNS name docker.for.mac.host.internal should be used instead of docker.for.mac.localhost (still valid) for host resolution from containers, since there is an RFC banning the use of subdomains of localhost. See tools.ietf.org/html/draft-west-let-localhost-be-localhost-06‌​.

@Jorge Orpinel 2018-03-11 01:18:19

This doesn't work for me. When I docker run -e HOSTNAME= docker.for.mac.host.internal , the container is created but nothign happens. I have to then crtl+C. With --net=host -e HOSTNAME=localhost at least the container runs and complains that it can't find the service I need (MySQL db).

@victorkurauchi 2018-03-20 20:17:38

worked like a charm. thanks for that !!! Docker for Mac v 17.12 => docker.for.mac.host.internal

@Teenage 2018-06-20 13:06:25

Perfect if you want to use php xdebug on Windows 10 : ``` zend_extension = xdebug.so xdebug.default_enable = 0 xdebug.remote_enable = 1 xdebug.remote_connect_back = 0 xdebug.remote_host = host.docker.internal```

@Sk96 2019-02-14 05:59:27

if you want to connect to a database hosted on your mac from a container simply update your jdbc connection URL from jdbc:mysql://localhost/mydb to jdbc:mysql://docker.for.mac.host.internal/mydb PS: the internal docker hostname is different for different versions of docker, kindly check the website to get the appropriate hostname for your version

@Ray D 2017-09-27 13:22:52

Solution for Linux (kernel >=3.6).

Ok, your localhost server has default docker interface docker0 with ip address 172.17.0.1. Your container started with default network settings --net="bridge".

  1. Enable route_localnet for docker0 interface:
    $ sysctl -w net.ipv4.conf.docker0.route_localnet=1
  2. Add this rules to iptables:
    $ iptables -t nat -I PREROUTING -i docker0 -d 172.17.0.1 -p tcp --dport 3306 -j DNAT --to 127.0.0.1:3306
    $ iptables -t filter -I INPUT -i docker0 -d 127.0.0.1 -p tcp --dport 3306 -j ACCEPT
  3. Create mysql user with access from '%' that mean - from anyone, excluding localhost:
    CREATE USER 'user'@'%' IDENTIFIED BY 'password';
  4. Change in your script the mysql-server address to 172.17.0.1


From the kernel documentation:

route_localnet - BOOLEAN: Do not consider loopback addresses as martian source or destination while routing. This enables the use of 127/8 for local routing purposes (default FALSE).

@IvanM 2018-11-11 19:27:19

172.17.0.1 is working

@mohan08p 2017-09-06 12:05:22

The CGroups and Namespaces are playing major role in the Container Ecosystem.

Namespace provide a layer of isolation. Each container runs in a separate namespace and its access is limited to that namespace. The Cgroups controls the resource utilization of each container, whereas Namespace controls what a process can see and access the respective resource.

Here is the basic understanding of the solution approach you could follow,

Use Network Namespace

When a container spawns out of image, a network interface is defined and create. This gives the container unique IP address and interface.

$ docker run -it alpine ifconfig

By changing the namespace to host, cotainers networks does not remain isolated to its interface, the process will have access to host machines network interface.

$ docker run -it --net=host alpine ifconfig

If the process listens on ports, they'll be listened on the host interface and mapped to the container.

Use PID Namespace By changing the Pid namespace allows a container to interact with other process beyond its normal scope.

This container will run in its own namespace.

$ docker run -it alpine ps aux

By changing the namespace to the host, the container can also see all the other processes running on the system.

$ docker run -it --pid=host alpine ps aux

Sharing Namespace

This is a bad practice to do this in production because you are breaking out of the container security model which might open up for vulnerabilities, and easy access to eavesdropper. This is only for debugging tools and understating the loopholes in container security.

The first container is nginx server. This will create a new network and process namespace. This container will bind itself to port 80 of newly created network interface.

$ docker run -d --name http nginx:alpine

Another container can now reuse this namespace,

$ docker run --net=container:http mohan08p/curl curl -s localhost

Also, this container can see the interface with the processes in a shared container.

$ docker run --pid=container:http alpine ps aux

This will allow you give more privileges to containers without changing or restarting the application. In the similar way you can connect to mysql on host, run and debug your application. But, its not recommend to go by this way. Hope it helps.

@Mariano Ruiz 2016-08-03 21:30:39

I doing a hack similar to above posts of get the local IP to map to a alias name (DNS) in the container. The major problem is to get dynamically with a simple script that works both in Linux and OSX the host IP address. I did this script that works in both environments (even in Linux distribution with "$LANG" != "en_*" configured):

ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d: | head -n1

So, using Docker Compose, the full configuration will be:

Startup script (docker-run.sh):

export DOCKERHOST=$(ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d: | head -n1)
docker-compose -f docker-compose.yml up

docker-compose.yml:

myapp:
  build: .
  ports:
    - "80:80"
  extra_hosts:
    - "dockerhost:$DOCKERHOST"

Then change http://localhost to http://dockerhost in your code.

For a more advance guide of how to customize the DOCKERHOST script, take a look at this post with a explanation of how it works.

@enderland 2017-03-15 15:51:11

Depending on your use case, you might even just get away with using the DOCKERHOST value here instead of "localhost" or 0.0.0.0 in whatever service your docker container needs to connect to locally.

@dieresys 2017-04-26 17:09:06

I have slightly modified your solution to be compatible with custom networks: export DOCKERHOST=$(docker network inspect --format='{{range .IPAM.Config}}{{.Gateway}}{{end}}' <NETWORK-NAME> | awk -F "/" 'NR==1{print $1}') Where <NETWORK-NAME> could be bridge or the name of the network as defined by docker-compose (usually path-name-network_name).

@Justin 2017-06-30 11:12:41

You need to add a comment: use dockerhost as the host for db connection (usualy replace for localhost in config file).

@sopheamak 2017-07-17 07:47:55

Here is my solution : it works for my case

  • set local mysql server to public access by comment #bind-address = 127.0.0.1 in /etc/mysql/mysql.conf.d

  • restart mysql server sudo /etc/init.d/mysql restart

  • run the following command to open user root access any host mysql -uroot -proot GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; FLUSH PRIVILEGES;

  • create sh script : run_docker.sh

    #!bin/bash

    HOSTIP=`ip -4 addr show scope global dev eth0 | grep inet | awk '{print \$2}' | cut -d / -f 1`


      docker run -it -d --name web-app \
                  --add-host=local:${HOSTIP} \
                  -p 8080:8080 \
                  -e DATABASE_HOST=${HOSTIP} \
                  -e DATABASE_PORT=3306 \
                  -e DATABASE_NAME=demo \
                  -e DATABASE_USER=root \
                  -e DATABASE_PASSWORD=root \
                  sopheamak/springboot_docker_mysql

  
  • run with docker-composer

    version: '2.1'

    services:
    tomcatwar: extra_hosts: - "local:10.1.2.232" image: sopheamak/springboot_docker_mysql
    ports: - 8080:8080 environment: - DATABASE_HOST=local - DATABASE_USER=root - DATABASE_PASSWORD=root - DATABASE_NAME=demo - DATABASE_PORT=3306

@Stephens 2017-05-22 19:02:27

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 your localhost and port 10117 is mapped to your local port 3306. 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

@Vlad 2017-06-16 12:41:40

Connect to local mysql via remote ngrok server ? OMG

@ZUNJAE 2019-03-01 08:55:39

This has to be the least intuitive answer I've ever seen on StackOverflow...

@dansalmo 2017-05-17 18:15:00

Simplest solution for Mac OSX

Just use the IP address of your Mac. On the Mac run this to get the IP address and use it from within the container:

$ ifconfig | grep 'inet 192'| awk '{ print $2}'

As long as the server running locally on your Mac or in another docker container is listening to 0.0.0.0, the docker container will be able to reach out at that address.

If you just want to access another docker container that is listening on 0.0.0.0 you can use 172.17.0.1

@Eduard B. 2018-03-22 11:55:03

You might not always have an IP address in the 192 group...

@Matt 2018-03-23 03:02:38

Docker for Mac exposes the docker.for.mac.host.internal hostname now.

@sivabudh 2017-02-07 07:15:57

Edit: I ended up prototyping out the concept on GitHub. Check out: https://github.com/sivabudh/system-in-a-box


First, my answer is geared towards 2 groups of people: those who use a Mac, and those who use Linux.

The host network mode doesn't work on a Mac. You have to use an IP alias, see: https://stackoverflow.com/a/43541681/2713729

What is a host network mode? See: https://docs.docker.com/engine/reference/run/#/network-settings

Secondly, for those of you who are using Linux (my direct experience was with Ubuntu 14.04 LTS and I'm upgrading to 16.04 LTS in production soon), yes, you can make the service running inside a Docker container connect to localhost services running on the Docker host (eg. your laptop).

How?

The key is when you run the Docker container, you have to run it with the host mode. The command looks like this:

docker run --network="host" -id <Docker image ID>

When you do an ifconfig (you will need to apt-get install net-tools your container for ifconfig to be callable) inside your container, you will see that the network interfaces are the same as the one on Docker host (eg. your laptop).

It's important to note that I'm a Mac user, but I run Ubuntu under Parallels, so using a Mac is not a disadvantage. ;-)

And this is how you connect NGINX container to the MySQL running on a localhost.

@sivabudh 2017-02-07 08:21:19

It's important to note that the host mode offers better performance since it uses the OS network stack.

@Xavier Huppé 2017-02-24 17:13:18

Very good point there. Although it IS possible to connect from a container to a host service with unused IP attach docs.docker.com/docker-for-mac/networking . Not a pleasant solution... but works.

@Michael 2017-05-22 10:39:19

Good stuff here. Once inside the container with --network="host", how does one connect to the host mysql for example?

@sivabudh 2017-05-22 11:00:13

@Buccleuch Just use localhost. Check out my GitHub source code: github.com/sivabudh/system-in-a-box/blob/master/dj_host_dock‌​er/…. Search for 'HOST', you will see 127.0.0.1 to connect to Postgresql.

@Michael 2017-05-22 11:07:14

I was able to get access to the host mysql databases by mounting the volume as per @user833482, and after installing mysql-client and server on the docker container of course.

@Casey 2016-12-10 15:53:05

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.

@Michael 2017-08-03 23:08:30

You deserve a medal! I've been working on this for two full days. Thank you for helping out!

@Contango 2017-11-02 15:44:54

I was also working on this for two full days. This is the only place on the web I've found it mentioned. Microsoft is completely silent on this when it talks about connecting to MSSQL from within a Docker container. It makes you wonder if they ever got it working themselves!

@orzel 2015-03-12 23:31:45

I disagree with the answer from Thomasleveil.

Making mysql bind to 172.17.42.1 will prevent other programs using the database on the host to reach it. This will only work if all your database users are dockerized.

Making mysql bind to 0.0.0.0 will open the db to outside world, which is not only a very bad thing to do, but also contrary to what the original question author wants to do. He explicitly says "The MySql is running on localhost and not exposing a port to the outside world, so its bound on localhost"

To answer the comment from ivant

"Why not bind mysql to docker0 as well?"

This is not possible. The mysql/mariadb documentation explicitly says it is not possible to bind to several interfaces. You can only bind to 0, 1, or all interfaces.

As a conclusion, I have NOT found any way to reach the (localhost only) database on the host from a docker container. That definitely seems like a very very common pattern, but I don't know how to do it.

@Thomasleveil 2015-03-13 23:31:09

from the docker host you could still connect to the MySQL server using the 172.17.42.1 address. But your note is right otherwise. Aso, I edited my answer with the host networking mode that allows to keep the MySQL server bound to 127.0.0.1 while allowing containers to connect to it

@orzel 2015-03-14 04:19:38

no, as I said, you can't connect to 172.17.42.1 if mysql is bound to localhost.

@0x89 2016-03-09 16:00:24

"Making mysql bind to 172.17.42.1 will prevent other programs using the database on the host to reach it." - that is not true. Other programs can use mysql, they just have to connect to 172.17.42.1 instead of localhost/127.0.0.1.

@halfer 2017-06-16 15:05:39

The solution to the problem in this answer generally is to get the MySQL server to bind to 0.0.0.0 and then set up a firewall so that the db is not accessible from the internet.

Related Questions

Sponsored Content

13 Answered Questions

[SOLVED] How to list containers in Docker

  • 2013-05-30 15:41:46
  • w00t
  • 624737 View
  • 805 Score
  • 13 Answer
  • Tags:   docker

44 Answered Questions

[SOLVED] How to get a Docker container's IP address from the host?

  • 2013-06-17 22:10:22
  • Murali Allada
  • 895668 View
  • 1028 Score
  • 44 Answer
  • Tags:   docker

15 Answered Questions

[SOLVED] Copying files from Docker container to host

13 Answered Questions

[SOLVED] How to upgrade docker container after its image changed

  • 2014-11-04 11:48:22
  • Yaroslav Stavnichiy
  • 335102 View
  • 423 Score
  • 13 Answer
  • Tags:   docker

14 Answered Questions

[SOLVED] Exposing a port on a live Docker container

  • 2013-11-11 02:00:56
  • reberhardt
  • 372342 View
  • 358 Score
  • 14 Answer
  • Tags:   docker

36 Answered Questions

[SOLVED] Copying files from host to Docker container

20 Answered Questions

[SOLVED] How to mount a host directory in a Docker container

54 Answered Questions

[SOLVED] How to remove old Docker containers

  • 2013-06-21 13:41:42
  • qkrijger
  • 664459 View
  • 1092 Score
  • 54 Answer
  • Tags:   docker

21 Answered Questions

[SOLVED] How to remove old and unused Docker images

  • 2015-09-22 17:23:37
  • Quanlong
  • 277685 View
  • 611 Score
  • 21 Answer
  • Tags:   docker

19 Answered Questions

[SOLVED] How is Docker different from a virtual machine?

Sponsored Content