3

My local installation of docker cannot access to other ports.

This is my docker-compose.yml file:

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"

This is the config for mailcatcher config/environments/development.rb:

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

This is how I run the rails app:

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

This is what I see when running 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

This is what I get when rails app tries to send an email:

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

I get the same error when I try to connect with another rails server that is running in another port.

I am working with Docker-beta in a Mac OSX.

Fran Martinez
  • 2,994
  • 2
  • 26
  • 38

1 Answers1

7

localhost on a docker instance will refer to the ports exposed by that instance. If you want to access ports from another instance in the same docker-compose.yml file, use links, which take the form of service-name:alias. (If you exclude the alias, it is the same as the source-name.)

So in your example above, the config for mailcatcher config/environments/development.rb should be:

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { address: "mailcatcher", port: 1025 }
lsowen
  • 3,728
  • 1
  • 21
  • 23