Docker,连接拒绝其他所有正在运行的服务

时间:2016-06-07 15:55:56

标签: ruby-on-rails port docker-compose microservices

我本地安装的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 app的方式:

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 app尝试发送电子邮件时得到的结果:

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

当我尝试连接另一个端口上运行的另一个rails server时出现同样的错误。

我在Mac OSX中使用Docker-beta。

1 个答案:

答案 0 :(得分:4)

docker实例上的

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 }