简体   繁体   English

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

[英]How to get Docker host IP on Travis CI?

I have a Rails repo on Travis. 我在特拉维斯有一个Rails回购。 It has a docker-compose.yml file: 它有一个docker-compose.yml文件:

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

(I had to use 5433 as the host port because 5432 gave me an error: Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use ) (我不得不使用5433作为主机端口,因为5432给了我一个错误: Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use

And a travis.yml: 还有一个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

I am trying to figure out what to put in my database.yml so my tests can run on Travis CI. 我试图找出放在我的database.yml中的内容,以便我的测试可以在Travis CI上运行。 In my other environments, I can do: 在我的其他环境中,我可以这样做:

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

But unfortunately this doesn't work on Travis because there is no docker-machine on Travis. 但不幸的是,这对Travis不起作用,因为Travis上没有docker-machine I get an error: docker-machine: command not found 我收到一个错误: docker-machine: command not found

How can I get the Docker host's IP on Travis? 如何在Travis上获取Docker主机的IP?

I think what you want is actually the container IP, not the docker engine IP. 我想你想要的实际上是容器IP,而不是docker引擎IP。 On your desktop you had to query docker-machine for the IP because the VM docker-machine created wasn't forwarding the port. 在桌面上,您必须向docker-machine查询IP,因为创建的VM docker-machine没有转发端口。

Since you're exposing a host port, you can actually use localhost for the host value. 由于您正在公开主机端口,因此您实际上可以使用localhost作为host值。

There are two other options as well: 还有两个选项:

  • run the tests in a container and link to the database container, so you can just use postgres as the host value. 在容器中运行测试并链接到数据库容器,因此您可以使用postgres作为host值。
  • if you don't want to use a host port, you can use https://github.com/swipely/docker-api (or some other ruby client) to query the docker API for the container IP, and use that for the host value. 如果您不想使用主机端口,可以使用https://github.com/swipely/docker-api (或其他一些ruby客户端)查询docker API以获取容器IP,并将其用于host价值。 Look for the inspect or inspect container API call. 查找inspect或inspect容器API调用。

For others coming across this, you should be able to get the host IP by running 对于遇到此问题的其他人,您应该能够通过运行来获取主机IP

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

from within the container. 从容器内。 You can then edit your database configuration via a script to insert this in - it'll be in the $HOST_IP_ADDRESS variable. 然后,您可以通过脚本编辑数据库配置以将其插入 - 它将位于$ HOST_IP_ADDRESS变量中。

However like dnephin said, I'm not sure this is what you want. 但是像dnephin说的那样,我不确定这是你想要的。 This would probably work if you were running Travis' Postgres service and needed to access it from within a container (depending on which IP address they bind it to). 如果您运行Travis的Postgres服务并且需要从容器内访问它(取决于它们将它绑定到哪个IP地址),这可能会有效。

But, it appears you're running it the opposite way to this, in which case I'm fairly sure localhost should get you there. 但是,看起来你正在以相反的方式运行它,在这种情况下,我很确定localhost应该让你到那里。 You might need to try some other debugging steps to make sure the container has started and is ready, etc. 您可能需要尝试其他一些调试步骤以确保容器已启动并准备就绪等。

EDIT: If localhost definitely isn't working, have you tried 127.0.0.1? 编辑:如果localhost肯定不起作用,你试过127.0.0.1吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM