Categories
docker docker-networking

From inside of a Docker container, how do I connect to the localhost of the machine?

2659

So I have a Nginx running inside a docker container, I have a mysql running on the host system, I want to connect to the MySql from within my container. MySql is only binding to the localhost device.

Is there any way to connect to this MySql or any other program on localhost from within this docker container?

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). 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.

3

  • 7

    Why not bind mysql to docker0 as well?

    – ivant

    Jun 20, 2014 at 11:42

  • 3

    For Windows Machine :- $docker run -d –name MyWebServer -P httpd

    – Lokesh S

    Sep 17, 2018 at 15:14


  • 14

    Without network: host you can’t go back from a container to the host. Only host to container. This is the main ideology behind containers. They are isolated for both stability and security reasons.

    May 17, 2020 at 20:26

3652

+300

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 (instead of the 127.0.0.1 in your connection string).

If you are using Docker-for-Linux 20.10.0+, you can also use the host host.docker.internal if you started your Docker container with the --add-host host.docker.internal:host-gateway option.

Otherwise, read below


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.

26

  • 22

    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’.

    – Ben

    May 12, 2015 at 5:45

  • 33

    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.

    Oct 1, 2015 at 22:19

  • 36

    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

    – zx1986

    Jun 30, 2016 at 3:35


  • 36

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

    – chx

    Sep 2, 2016 at 5:30

  • 9

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

    Feb 9, 2017 at 19:10

558

For all platforms

Docker v 20.10 and above (since December 14th 2020)

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.

On Linux, add --add-host=host.docker.internal:host-gateway to your Docker command to enable this feature. To enable this in Docker Compose on Linux, add the following lines to the container definition:

extra_hosts:
    - "host.docker.internal:host-gateway"

For older macOS and Windows versions of Docker

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

For older macOS 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

9

  • 7

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

    Jul 22, 2017 at 5:46

  • 3

    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

    Dec 3, 2017 at 15:06


  • 1

    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.

    – Jya

    Feb 19, 2018 at 13:46

  • 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).

    Mar 11, 2018 at 1:18


  • So quick and useful. Thanks!

    – iedmrc

    Sep 18, 2020 at 7:47

205

using

host.docker.internal

instead of

localhost

works flawlessly for me. 👍

10

  • 14

    Perhaps this answer could be expanded and clarified a bit? Does this mean you can still use Bridge mode?

    – Jolta

    Oct 8, 2020 at 11:21


  • 15

    @ArnoldRoa this will only work on Mac/Windows not Linux

    Mar 23, 2021 at 23:54

  • 12

    Doesn’t work as of 4/26/2021. host.docker.internal resolves to nothing.

    – Triynko

    Apr 26, 2021 at 4:31

  • 2

    For setting host.docker.internal with docker-compose, see stackoverflow.com/a/67158212/243392

    May 25, 2021 at 4:43

  • 3

    Thank you @DeyaEldeen, this work perfectly for mac

    Jan 23 at 20:53