I have a Rails application. In the development and testing environment, I want the Rails application to connect to the docked Postgres. The Rails application alone will not be in the container, but just Postgres.
What should my database.yml look like?
I have a docker default machine running. I created docker-compose.yml:
postgres: image: postgres ports: - "5432:5432" environment: - POSTGRES_USER=timbuktu - POSTGRES_PASSWORD=mysecretpassword
I ran docker-compose up to run Postgres.
Then I ran docker-machine ip default to get the IP address of the Docker virtual machine, and I updated database.yml accordingly:
... development: adapter: postgresql host: 192.168.99.100 port: 5432 database: timbuktu_development username: timbuktu password: mysecretpassword encoding: unicode pool: 5 ...
So, all is well and I can connect to Postgres in my container.
But, if someone else pulls out the repo, they will not be able to connect to Postgres using my .yml database, because the IP address of their Docker machine will be different from mine by default.
So how can I modify my database.yml to account for this?
One of my ideas is to ask them to get the default IP address of their Docker computer by running docker-machine env default and inserting the ENV DOCKER_HOST line into their bash_rc. For instance,
export DOCKER_HOST="tcp://192.168.99.100:2376"
Then my database.yml node may include the line
host: <%= ENV['DOCKER_HOST'].match(/tcp:\/\/(.+):\d{3,}/)[1] %>
But it seems ugly and hacked. Is there a better way?