如何在Travis CI上获得Docker主机IP? [英] How to get Docker host IP on Travis CI?

查看:289
本文介绍了如何在Travis CI上获得Docker主机IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Travis上有一个Rails repo。它有一个docker-compose.yml文件:

I have a Rails repo on Travis. It has a docker-compose.yml file:

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

(我不得不使用5433作为主机端口,因为5432给了我一个错误:启动userland代理时出错:listen tcp 0.0.0.0:5432:bind:address already在使用中

(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)

和travis.yml:

And a 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中的内容,所以我的测试可以在Travis CI上运行。在我的其他环境中,我可以做:

I am trying to figure out what to put in my database.yml so my tests can run on 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

但不幸的是,这不在Travis上工作,因为Travis上没有 docker-machine 。我收到一个错误: docker-machine:command not found

But unfortunately this doesn't work on Travis because there is no docker-machine on Travis. I get an error: docker-machine: command not found

如何在Travis上获取Docker主机的IP

How can I get the Docker host's IP on Travis?

推荐答案

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

I think what you want is actually the container IP, not the docker engine IP. On your desktop you had to query docker-machine for the IP because the VM docker-machine created wasn't forwarding the port.

由于您暴露了主机端口,您实际上可以使用 localhost 主机值。

Since you're exposing a host port, you can actually use localhost for the host value.

另外还有两个选项:


  • 容器和链接到数据库容器,因此您可以使用 postgres 作为主机值。

  • 如果您不想使用主机端口,可以使用 https:// github.com/swipely/docker-api (或某些其他ruby客户端)查询docker API的容器IP,并将其用于主机值。寻找检查或检查容器API调用。

  • run the tests in a container and link to the database container, so you can just use postgres as the host value.
  • 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. Look for the inspect or inspect container API call.

这篇关于如何在Travis CI上获得Docker主机IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆