22

I have a docker container running a java process that I am trying to connect to rabbitmq running on my localhost.

Here are the steps I've done so far:

On my Local machine (macbook running Docker version 1.13.0-rc3, build 4d92237 with firewall turned off)

  1. I've updated my rabbitmq_env.conf file to remove RABBITMQ_NODE_IP_ADDRESS so I am not tied to connect via localhost and i have an admin rabbitmq user. (not trying with guest user)
  2. I tested this via telnet on my local machine and have no issues telnet <local-ip> 5672

Inside my docker container

  1. able to ping local-ip and curl rabbitmq admin api curl -i -u username:password http://local-ip:15672/api/vhosts returns sucessfully

    [{"name":"/","tracing":false}]

  2. When i try to telnet from inside the container I get

    "Connection closed by foreign host"

  3. looking at the rabbitmq.logs

    =ERROR REPORT==== closing AMQP connection <0.30526.1> (local-ip:53349 -> local-ip:5672): {handshake_timeout,handshake}

My java stacktrace incase helpful

Caused by: java.net.ConnectException: Connection refused (Connection >refused) at java.net.PlainSocketImpl.socketConnect(Native Method) at >java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at >java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.>java:206) at >java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589) at >com.rabbitmq.client.impl.FrameHandlerFactory.create(FrameHandlerFactory.ja>va:32) at >com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newCon>nection(RecoveryAwareAMQConnectionFactory.java:35)

docker network inspect bridge

[ { "Name": "bridge", "Id": "716f935f19a107225650a95d06eb83d4c973b7943b1924815034d469164affe5", "Created": "2016-12-11T15:34:41.950148125Z", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": null, "Config": [ { "Subnet": "172.17.0.0/16", "Gateway": "172.17.0.1" } ] }, "Internal": false, "Attachable": false, "Containers": { "9722a49c4e99ca5a7fabe56eb9e1c71b117a1e661e6c3e078d9fb54d7d276c6c": { "Name": "testing", "EndpointID": "eedf2822384a5ebc01e5a2066533f714b6045f661e24080a89d04574e654d841", "MacAddress": "02:42:ac:11:00:02", "IPv4Address": "172.17.0.2/16", "IPv6Address": "" } }, "Options": { "com.docker.network.bridge.default_bridge": "true", "com.docker.network.bridge.enable_icc": "true", "com.docker.network.bridge.enable_ip_masquerade": "true", "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0", "com.docker.network.bridge.name": "docker0", "com.docker.network.driver.mtu": "1500" }, "Labels": {} } ]

What am I missing?

CandiedCode
  • 554
  • 1
  • 3
  • 7

4 Answers4

30

For me this works fine!

I installed the image:

docker pull rabbitmq:3-management

and ran:

docker run -d --hostname haroldjcastillo --name rabbit-server -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin2017 -p 5672:5672 -p 15672:15672 rabbitmq:3-management

The most important is to add the connection and management ports: -p 5672:5672 -p 15672:15672

See your host in docker:

docker-machine ip

returns in my case:

192.168.99.100

Go to the management console: http://192.168.99.100:15672

For Spring Boot you can configure this:

spring.rabbitmq.host=192.168.99.100
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin2017
spring.rabbitmq.port=5672

Best wishes!

Denis Abakumov
  • 355
  • 3
  • 11
Harold Castillo
  • 2,006
  • 19
  • 23
  • Thanks for this. Just ran into this situation myself, feeling stupid for not realizing I had to open the ports :P – KdgDev Aug 13 '19 at 09:49
  • Specifying the port solved a connectivity issue for me. Why does specifying the port allow me to connect when it already should have port 5672 exposed just using `docker run rabbitmq`? – jndietz Aug 02 '22 at 20:54
7

For anyone else searching for this error, I'm using spring boot and rabbitmq in docker container, starting them with docker compose. I kept getting org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused from the spring app.

The rabbitmq hostname was incorrect. To fix this, I'm using the container names in the spring app configuration. Either put spring.rabbitmq.host=my-rabbit in spring's application.properties (or yml file), or in docker-compose.yaml add environment: SPRING_RABBITMQ_HOST: my-rabbit to the spring service. Of course, "my-rabbit" is the rabbitmq container name described in the docker-compose.yaml

Georgian
  • 8,795
  • 8
  • 46
  • 87
2

I am using docker with linux container with rabbitmq:3-management and have created a dotnet core based web api. While calling from We API action method I faced the same issue and changed the value to "host.docker.internal"

following scenario worked for me

  1. "localhost" on IIS Express
  2. "localhost" on Docker build from Visual Studio
  3. "host.docker.internal" on Docker build from Visual Studio

"Messaging": { "Hostname": "host.docker.internal", "OrderQueue": "ProductQueue", "UserName": "someuser", "Password": "somepassword" },

But facing the same issue when, the container created via docker build command, but not when container created using Visual Studio F5 command.

Now find the solution there are two ways to do it:

by default all the containers get added into "bridge" network go through with these steps

Case1: If you have already containers (rabbitmq and api) in the docker and running then first check their ip / hostname

  1. docker network ls
  2. docker network inspect bridge # from this step you'll get to know what containers are associated with this
  3. find the rabbitmq container and internal IP, apply this container name or IP and then run your application it will work from Visual Studio and Docker build and run command

Case2: if you have no containers running then you may like to create your network in docker then follow these steps:

  1. docker network create givenetworknamehere
  2. add your container while using "docker run" command or after
    Step2.1: if using docker run command for your container then;
    docker run --network givenetworknamehere -d -p yourport:80 --name givecontainername giveyourimagename
    Step2.2 if adding newly created network after container creation then use below command docker network connect givenetworknamehere givecontainername

with these step you bring your container in your newly created same network and they can communicate.

Note: by default "bridge" network type get created

Manish Kumar
  • 335
  • 1
  • 5
  • 16
0

After a restart, all was working. I don't think Rabbit was using respecting .config changes

CandiedCode
  • 554
  • 1
  • 3
  • 7