如何在Travis CI上获得Docker主机IP?

我有一个关于Travis的Rails回购。 它有一个docker-compose.yml文件:

postgres: image: postgres ports: - "5433:5432" environment: - POSTGRES_USER=calories - POSTGRES_PASSWORD=secretpassword 

(我不得不使用5433作为主机端口,因为5432给了我一个错误: Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use

和一个travis.yml:

 sudo: required services: - docker language: ruby cache: bundler before_install: # Install docker-compose - curl -L https://github.com/docker/compose/releases/download/1.4.0/docker-compose-`uname -s`-`uname -m` > docker-compose - chmod +x docker-compose - sudo mv docker-compose /usr/local/bin # TODO: Remove this temporary fix when it's safe to: # https://github.com/travis-ci/travis-ci/issues/4778 - sudo iptables -N DOCKER || true - sleep 10 - docker-compose up -d before_script: - bundle exec rake db:setup script: - bundle exec rspec spec after_script: - docker-compose stop - docker-compose rm -f 

我想弄清楚在我的database.yml中放什么,所以我的testing可以在Travis CI上运行。 在我的其他环境中,我可以这样做:

 adapter: postgresql encoding: unicode host: <%= `docker-machine ip default` %> port: 5433 username: calories password: secretpassword # For details on connection pooling, see rails configuration guide # http://guides.rubyonrails.org/configuring.html#database-pooling pool: 5 

但不幸的是,这并不适用于特拉维斯,因为Travis上没有docker-machine 。 我收到一个错误: docker-machine: command not found

我如何才能在Travis上获得Docker主机的IP?

我想你想要的实际上是容器IP,而不是docker引擎IP。 在你的桌面上,你必须向docker-machine查询IP,因为创build的VM docker-machine没有转发端口。

由于您公开了主机端口,因此您可以使用localhost作为host值。

还有两个其他选项:

  • 在一个容器中运行testing并链接到数据库容器,所以你可以使用postgres作为host值。
  • 如果您不想使用主机端口,则可以使用https://github.com/swipely/docker-api (或其他某个ruby客户端)来查询容器IP的docker API,并将其用于host价值。 查找或检查容器API调用。

对于其他遇到此问题的人,您应该能够通过运行获取主机IP

 export HOST_IP_ADDRESS="$(/sbin/ip route|awk '/default/ { print $3 }')" 

从容器内。 然后,您可以通过脚本编辑您的数据库configuration,将其插入 – 它将位于$ HOST_IP_ADDRESSvariables中。

不过,像dnephin说的,我不确定这是你想要的。 如果你正在运行Travis的Postgres服务,并且需要从一个容器内访问它(取决于它绑定到哪个IP地址),这可能会起作用。

但是,看来你正在以相反的方式运行它,在这种情况下,我相当肯定本地主机应该到达那里。 您可能需要尝试其他一些debugging步骤,以确保容器已经启动并准备就绪,等等。

编辑:如果localhost肯定不工作,你有没有尝试过127.0.0.1?