![](/img/trans.png)
[英]Docker cannot establish connection with postgresql in Travis CI
[英]How to get Docker host IP on Travis CI?
我在特拉維斯有一個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中的內容,以便我的測試可以在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不起作用,因為Travis上沒有docker-machine
。 我收到一個錯誤: docker-machine: command not found
如何在Travis上獲取Docker主機的IP?
我想你想要的實際上是容器IP,而不是docker引擎IP。 在桌面上,您必須向docker-machine
查詢IP,因為創建的VM docker-machine沒有轉發端口。
由於您正在公開主機端口,因此您實際上可以使用localhost
作為host
值。
還有兩個選項:
postgres
作為host
值。 host
價值。 查找inspect或inspect容器API調用。 對於遇到此問題的其他人,您應該能夠通過運行來獲取主機IP
export HOST_IP_ADDRESS="$(/sbin/ip route|awk '/default/ { print $3 }')"
從容器內。 然后,您可以通過腳本編輯數據庫配置以將其插入 - 它將位於$ HOST_IP_ADDRESS變量中。
但是像dnephin說的那樣,我不確定這是你想要的。 如果您運行Travis的Postgres服務並且需要從容器內訪問它(取決於它們將它綁定到哪個IP地址),這可能會有效。
但是,看起來你正在以相反的方式運行它,在這種情況下,我很確定localhost應該讓你到那里。 您可能需要嘗試其他一些調試步驟以確保容器已啟動並准備就緒等。
編輯:如果localhost肯定不起作用,你試過127.0.0.1嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.