Docker Container Connects to Host
Created at 2016-05-22T06:00:26.000Z

Thanks to ideas from:

Prepare a file below:


export DOCKER_HOST_IP=$(ip route show | awk '/default/ {print $3}')
exec "$@"

Then, in your Dockerfile, make this executable as an entrypoint:

FROM ubuntu:14.04

When you run this image, you can see $DOCKER_HOST_IP is available anywhere:

$ docker build -t some_image .
$ docker run -it --rm some_image /bin/bash
/# echo $DOCKER_HOST_IP  

If you're making Rails app, your config/database.yml will be something like this:

  adapter: postgresql
  encoding: unicode
  template: template0
  reconnect: true
  pool: 5
  host: <%= ENV['DOCKER_HOST_IP'] %>
  port: 5432
  database: xxxx
  username: yyyy
  password: zzzz

I used this on Ubuntu machine as docker host, but it didn't work for OS X.