Rabbitmq - Docker connection refused on port 5672
Asked Answered
S

3

7

I have a web server written in Go that interacts with Rabbitmq and Mongodb. When I run all these servers on my machine without containers (rabbitmq url: amqp://guest:guest@localhost:5672) it works fine.

Now I am trying to run all these services in a separate container. Here is my compose file

version: '3'
services:
  rabbitmq:
    image: rabbitmq
    container_name: rabbitmq
    ports:
      - 5672:5672
  mongodb:
    image: mongo
    container_name: mongodb
    ports:
      - 27017:27017
  web:
    build: .
    image: palash2504/collect
    container_name: collect-server
    restart: on-failure
    ports:
      - 3000:3000
    depends_on:
      - rabbitmq
      - mongodb
    links: ["rabbitmq", "mongodb"]

networks:
  default:
    external:
      name: collect-net

This is my servers dockerfile

FROM golang

ENV GO111MODULE=on


WORKDIR /app

COPY go.mod .
COPY go.sum .

RUN go mod download

COPY . .

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build

EXPOSE 3000


ENTRYPOINT ["/app/social-cops-assignment"]

My server can't seem to connect to rabbitmq. This is the log message I get when running docker compose up and the rabbitmq url in my config being amqp://guest:guest@rabbitmq:5672 (since the container name is rabbitmq I replace localhost with rabbitmq so that my server is able to find the rabbitmq container)

collect-server | 2019/03/10 08:50:51 Failed to connect to AMQP compatible broker at: amqp://guest:guest@rabbitmq:5672/, with errror: dial tcp 172.24.0.3:5672: connect: connection refused

But rabbitmq seems to be ready to accept connections. These are the last two lines of the rabbitmq logs from docker-compose up

rabbitmq    | 2019-03-10 08:50:55.164 [info] <0.489.0> accepting AMQP connection <0.489.0> (172.24.0.4:49784 -> 172.24.0.3:5672)
rabbitmq    | 2019-03-10 08:50:55.205 [info] <0.489.0> connection <0.489.0> (172.24.0.4:49784 -> 172.24.0.3:5672): user 'guest' authenticated and granted access to vhost '/'

I am new to docker-networking and I don't know what am I doing wrong? Is it the rabbitmq address that I am using or I need some additional configuration with respect to rabbitmq or expose some ports?

Slough answered 10/3, 2019 at 8:59 Comment(2)
can you execute docker-compose restart web and check if the issue still exist ? just make sure you don't restart rabbitmqMarinara
yes it still existsSlough
M
9

When targeting services, dont append a port. Use rabbitmq, not rabbitmq:5672.

I already answered this in your question here: How to connect to rabbitmq container from the application server container

All the docker network configuration (after the described fix above) seems to be fine. The remaining error is likely due to authentication issues related to your source code and/or authentication data/setup.

Mehetabel answered 10/3, 2019 at 10:36 Comment(5)
did that. replaced the rabbitmq url in my config file with this amqp://guest:guest@rabbitmq but it still doesn't connect. Here is the error log Failed to connect to AMQP compatible broker at: amqp://guest:guest@rabbitmq/, with errror: dial tcp 172.24.0.2:5672: connect: connection refusedSlough
What library is printing that error? I can't find it on the web, except for a blog post where it is included as an example.Mehetabel
I am using the go library for amqp godoc.org/github.com/streadway/amqp This log message is from my application code but the last part is from the library dial tcp 172.24.0.2:5672: connect: connection refusedSlough
connection refused is likely related to authentication, not networking. I've added this to my answer.Mehetabel
I have had a similar problem due to an @ character in the login and password.Divertimento
D
4

Two things that fixed it for me:

  1. Make sure not to use localhost, instead use the container's name. For me it was amqp.
amqp:
    image: rabbitmq:3-management-alpine
    ports:
      - "5672:5672"
      - "15672:15672"

Then the connection string will be amqp://guest:guest@amqp/.

Dynamometer answered 22/9, 2021 at 0:45 Comment(0)
B
1

I also had trouble with RabbitMQ and docker-compose. If your code works well without containers, then look at those 2 things:

  1. As mentionned above, you need to use the service name (you can also use the container name if you specify it in your docker compose file) to target a service from another container. It is well explained here: https://docs.docker.com/compose/networking/. The documentation indicates to append the port to the service, even though it seems to work without.

  2. This is what I struggled with: the rabbitMQ service takes a while to start, so other services (containers) try to connect before it's running. The main reason is that depends_on clause only guarantees that rabbitMQ is started, but not that it is running and healthy. To fix this, you can add restart: unless-stopped or restart: on-failure to your other services depending on RabbitMQ.

Another possible solution is to add a healthcheck clause in RabbitMQ service and check it in your other services like this:

depends_on:
    rabbitmq:
        condition: service_healthy
Butterfat answered 23/6 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.