Access Host machine from Docker

Published on

Problem

We want to access the host machine port from the docker container, some use cases are you have existing DB in local machine and want to use.

Solution

  • open bash inside you docker container

  • run the following command

    ip route show
    • The command will return a result something like below

      default via 172.15.0.1 dev eth0
      172.15.0.0/16 dev eth0 proto kernel scope link src 172.15.0.4
    • The eth0 is the ip can be used to interact with host machine

    • To get a IP alone, we can use following IP

      ip route show | awk '/default/ {print $3}'