2016-06-07 83 views
2

我的本地安裝的docker無法訪問其他端口。Docker,連接拒絕其他所有正在運行的服務

這是我docker-compose.yml文件:

db: 
    image: library/mysql:5.6 
    environment: 
    MYSQL_ALLOW_EMPTY_PASSWORD: "yes" 
    expose: 
    - "3306" 
    ports: 
    - "3306:3306" 

mailcatcher: 
    image: yappabe/mailcatcher 
    ports: 
    - "1025:1025" 
    - "1080:1080" 

rails-app: 
    build: . 
    dockerfile: "Dockerfile" 
    environment: 
    RAILS_ENV: development 
    links: 
    - mailcatcher 
    - db 
    command: bundle exec rails server -p 3005 -b '0.0.0.0' 
    volumes: 
    - ".:/home/app" 
    volumes_from: 
    - bundle 
    expose: 
    - "3005" 
    ports: 
    - "3005:3005" 

這是mailcatcher config/environments/development.rb的配置:

config.action_mailer.delivery_method = :smtp 
config.action_mailer.smtp_settings = { address: "localhost", port: 1025 } 

這是我如何運行Rails應用程序:

docker-compose run --service-ports rails-app 

這是我運行時看到的是什麼docker ps

> docker ps 
CONTAINER ID  IMAGE     COMMAND     CREATED    STATUS    PORTS           NAMES 
1fa8ac2ad8fd  pmt_rails-app   "bundle exec rails se" 5 seconds ago  Up 3 seconds  0.0.0.0:3005->3005/tcp       pmt_rails-app_run_1 
4f65bb2fc9ac  yappabe/mailcatcher "/run.sh"    About an hour ago Up About an hour 0.0.0.0:1025->1025/tcp, 0.0.0.0:1080->1080/tcp pmt_mailcatcher_1 
cfb364ee569f  library/mysql:5.6  "docker-entrypoint.sh" About an hour ago Up About an hour 0.0.0.0:3306->3306/tcp       pmt_db_1 

這是我所得到的,當Rails應用程序試圖發送電子郵件:當我嘗試用另一個rails server了在另一個端口運行連接

Errno::ECONNREFUSED: Connection refused - connect(2) for "localhost" port 1025 
from /usr/local/lib/ruby/2.3.0/net/smtp.rb:542:in `initialize' 

我得到同樣的錯誤。

我在Mac OSX上使用Docker-beta。

回答

3

localhost泊塢窗實例將引用該實例公開的端口。如果要訪問同一docker-compose.yml文件中另一個實例的端口,請使用links,其格式爲service-name:alias。 (如果排除alias,這是一樣的source-name

所以在你上面的例子,對於mailcatcher config/environments/development.rb的配置應該是:

config.action_mailer.delivery_method = :smtp 
config.action_mailer.smtp_settings = { address: "mailcatcher", port: 1025 } 
+0

你救了我的命! –

+0

很高興我可以幫助:) – lsowen

+0

2生命,並計數! – unmultimedio