Docker and Rabbitmq: ECONNREFUSED between containers

I am trying to configure separate docker containers for rabbitmq and consumer for container, i.e. A process that will listen on the queue and perform the necessary tasks. I created a yml file and a docker file.

I can run the yml file, however, when I check the docker logs, I see where there are ECONNREFUSED errors.

NewUserNotification.js:

require('seneca')() .use('seneca-amqp-transport') .add('action:new_user_notification', function(message, done) { … return done(null, { pid: process.pid, status: `Process ${process.pid} status: OK` }) .listen({ type: 'amqp', pin: ['action:new_user_notification'], name: 'seneca.new_user_notification.queue', url: process.env.AMQP_RECEIVE_URL, timeout: 99999 }); 

error message in docker log:

  {"notice":"seneca: Action hook:listen,role:transport,type:amqp failed: connect ECONNREFUSED 127.0.0.1:5672.","code": "act_execute","err":{"cause":{"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1", "port":5672},"isOperational":true,"errno":"ECONNREFUSED","code":"act_execute","syscall":"connect","address":"127.0.0.1", "port":5672,"eraro":true,"orig":{"cause":{"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1", "port":5672},"isOperational":true,"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":5672}, "seneca":true,"package":"seneca","msg":"seneca: Action hook:listen,role:transport,type:amqp failed: connect ECONNREFUSED 127.0.0.1:5672.", "details":{"message":"connect ECONNREFUSED 127.0.0.1:5672","pattern":"hook:listen,role:transport,type:amqp","instance":"Seneca/…………/…………/1/3.4.3/-", "orig$":{"cause":{"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":5672},"isOperational":true, "errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":5672} 

sample docker-compose.yml file:

 version: '2.1' services: rabbitmq: container_name: "4340_rabbitmq" tty: true image: rabbitmq:management ports: - 15672:15672 - 15671:15671 - 5672:5672 volumes: - /rabbitmq/lib:/var/lib/rabbitmq - /rabbitmq/log:/var/log/rabbitmq - /rabbitmq/conf:/etc/rabbitmq/ account: container_name: "account" build: context: . dockerfile: ./Account/Dockerfile ports: - 3000:3000 links: - "mongo" - "rabbitmq" depends_on: - "mongo" - "rabbitmq" new_user_notification: container_name: "app_new_user_notification" build: context: . dockerfile: ./Account/dev.newusernotification.Dockerfile links: - "mongo" - "rabbitmq" depends_on: - "mongo" - "rabbitmq" command: ["./wait-for-it.sh", "rabbitmq:5672", "-t", "90", "--", "node", "newusernotification.js"] 

amqp connection string: (I tried both ways, with and without a user) AMQP: // username: password @RabbitMQ: 5672

I added the link attribute to the docker layout file and specified the name in the .env file (rabbitmq). I tried to run the NewUserNotification.js file from outside the container, and everything started fine. What can cause this problem? Connection string error? Problem setting up Docker-Compose.yml? Other?

+5
source share
2 answers

It seems that the environment variable AMQP_RECEIVE_URL not built properly. According to the error log, the listener tries to connect to localhost (127.0.0.1), which is not the rabbitmq service IP address. Find the modified configurations for the working sample.

1 docker-compose.yml

 version: '2.1' services: rabbitmq: container_name: "4340_rabbitmq" tty: true image: rabbitmq:management ports: - 15672:15672 - 15671:15671 - 5672:5672 volumes: - ./rabbitmq/lib:/var/lib/rabbitmq new_user_notification: container_name: "app_new_user_notification" build: context: . dockerfile: Dockerfile env_file: - ./un.env links: - rabbitmq depends_on: - rabbitmq command: ["./wait-for-it.sh", "rabbitmq:5672", "-t", "120", "--", "node", "newusernotification.js"] 

2 un.env

AMQP_RECEIVE_URL=amqp://guest: guest@rabbitmq :5672

Note that I passed AMQP_RECEIVE_URL as the environment variable to the new_user_notification service using env_file and got rid of the account service

3 dockerfile

 FROM node:7 WORKDIR /app COPY newusernotification.js /app COPY wait-for-it.sh /app RUN npm install --save seneca RUN npm install --save seneca-amqp-transport 

4 newusernotification.js use the same file in the question.

5 wait-for-it.sh

0
source

It is possible that your RabbitMQ service is not fully completed while the connection is being made from the consumer service.

If so, in Docker Compose you can wait to receive services using a container named dadarek / wait-for-dependencies .

1). Add a new waitforrabbit service to your docker-compose.yml file

 waitforrabbit: image: dadarek/wait-for-dependencies depends_on: - rabbitmq command: rabbitmq:5672 

2). Enable this service in the depends_on section of the service that requires RabbitMQ.

 depends_on: - waitforrabbit 

3). Commissioning

 docker-compose run --rm waitforrabbit docker-compose up -d account new_user_notification 

Starting a composition this way will by and large wait until RabbitMQ is fully inserted before the connection from the consumer service is made.

0
source

Source: https://habr.com/ru/post/1274375/


All Articles