6

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?

2
  • can you execute docker-compose restart web and check if the issue still exist ? just make sure you don't restart rabbitmq Mar 10, 2019 at 10:06
  • yes it still exists Mar 10, 2019 at 10:54

2 Answers 2

7

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.

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 refused Mar 10, 2019 at 10:54
  • 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. Mar 10, 2019 at 11:02
  • 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 refused Mar 10, 2019 at 11:06
  • 2
    connection refused is likely related to authentication, not networking. I've added this to my answer. Mar 10, 2019 at 11:10
  • I have had a similar problem due to an @ character in the login and password.
    – aptlin
    Jun 13, 2020 at 9:18
2

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/.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.