

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). 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. Inet6 fe80::a00:27ff:fe98:dcaa/64 scope linkĪnd from a docker container in host mode: $ docker run -rm -it -network=host ubuntu:trusty ip addr show eth0Īs you can see both the docker host and docker container share the exact same network interface and as such have the same IP address.Ĭonnecting to MySQL from containers bridge mode IP config on my docker host: $ ip addr show eth0Ģ: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 And this without requiring the -p or -P docker run option. 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.īe aware that any port opened in your docker container would be opened on the docker host. So the IP Address of the docker host 172.17.42.1 is set as the default route and is accessible from your container. Now look at the routing table: routeĭestination Gateway Genmask Flags Metric Ref Use Ifaceĭefault 172.17.42.1 0.0.0.0 UG 0 0 0 eth0 Here my container has the IP address 172.17.1.192.

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: ip addr show eth0Ĩ63: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 So here my docker host has the IP address 172.17.42.1 on the docker0 network interface. On the Docker host, type sudo ip addr show docker0 you will have an output looking like: $ sudo ip addr show docker0Ĥ: docker0: mtu 1500 qdisc noqueue state UP group default Both the docker host and the docker containers have an IP address on that bridge. docker run -network="bridge" (default)ĭocker creates a bridge named docker0 by default. Depending on the mode you choose you would connect to your MySQL database running on the docker host differently. Note on docker container networking modesĭocker offers different networking modes when running containers. Note: This mode only works on Docker for Linux, per the documentation. Use -network="host" in your docker run command, then 127.0.0.1 in your docker container will point to your docker host. If you are using Docker-for-Linux 20.10.0+, you can also use the host if you started your Docker container with the -add-host :host-gateway option. If you are using Docker-for-mac or Docker-for-Windows 18.03+, just connect to your mysql service using the host (instead of the 127.0.0.1 in your connection string). Even if you have the IP address of the docker host it does not mean you can connect to docker host from within the container given that IP address as your Docker network may be overlay, host, bridge, macvlan, none etc which restricts the reachability of that IP address. This question is different from "How to get the IP address of the docker host from inside a docker container" due to the fact that the IP address of the docker host could be the public IP or the private IP in the network which may or may not be reachable from within the docker container (I mean public IP if hosted at AWS or something). Is there any way to connect to this MySql or any other program on localhost from within this docker container? 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. 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.
