user12856224
user12856224

Reputation:

How to solve issue "Could not establish TCP connection to any of the configured hosts"

I'm new in programming and I'm trying to set relationships between my rails apps. I'm using docker-compose and rabbitmq. Here is my code:

some code in controller in Rails app

def some_method
  connection = Bunny.new(
    host: 'localhost',
    port: 5672,
    vhost: '/',
    user: 'guest',
    password: 'guest')
  connection.start
  channel = connection.create_channel
  queue = channel.queue('hello')
  channel.default_exchange.publish('Hello World!', routing_key: queue.name)
  puts " [x] Sent 'Hello World!'"
  connection.close
end

docker-compose.yml:

version: '3'

services: 
  db:
    image: postgres
  environment:
    POSTGRES_USER: root
    POSTGRES_PASSWORD: mysecretpassword
  volumes:
    - pgdata:/var/lib/postgresql/data
   
  web:
    build: .
    command: bundle exec rails s webrick -b '0.0.0.0'
    volumes:
      - .:/lab2_app
    ports:
      - "3000:3000"
    depends_on:
      - db
    tty: true
    stdin_open: true

docker-compose.yml for rabbitmq:

version: '3'
services:
  rabbitmq:
    image: rabbitmq:3-management
    ports:
      - 5672:5672
      - 15672:15672

I can use RabbitMQ Management in my browser and I can send request via curl: curl http://localhost:15672 => AMQP

But I have error "Could not establish TCP connection to any of the configured hosts" when I call my "some_method.

I was wondering if somebody could help me :)

Upvotes: 0

Views: 1566

Answers (1)

Karol Ostrowski
Karol Ostrowski

Reputation: 94

As far as i know when You run it in container Rabbit instance isn't local for rails instead of 'localhost' You should use 'rabbitmq'

Upvotes: 2

Related Questions