Rabbitmq connection refused from Docker container to local host
Solution 1
for me this works fine!
I have been installed the image docker pull rabbitmq:3-management
and run
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 you host in docker
docker-machine ip
return in my case:
192.168.99.100
Go to management http://192.168.99.100:15672
For Spring Boot you can configure this or works good for another connections
spring.rabbitmq.host=192.168.99.100
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin2017
spring.rabbitmq.port=5672
Best wishes
Solution 2
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
Solution 3
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
- "localhost" on IIS Express
- "localhost" on Docker build from Visual Studio
- "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
- docker network ls
- docker network inspect bridge # from this step you'll get to know what containers are associated with this
- 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:
- docker network create givenetworknamehere
- 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
CandiedCode
Updated on July 09, 2022Comments
-
CandiedCode almost 2 years
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)
- 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)
- I tested this via telnet on my local machine and have no issues
telnet <local-ip> 5672
Inside my docker container
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}]
When i try to telnet from inside the container I get
"Connection closed by foreign host"
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?
-
The_Tourist over 7 yearsWhat is
local-ip
exactly? and can you paste the output ofdocker ps
with your container running and alsodocker inspect
? -
CandiedCode over 7 yearsit's my local-ip address, which is 192.168.1.XXX
-
CandiedCode over 7 yearsdocker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9722a49c4e99 e9096fc4f5ff "/bin/sh" 3 hours ago Up 2 hours 5672/tcp, 7888/tcp testing
-
KdgDev almost 5 yearsThanks for this. Just ran into this situation myself, feeling stupid for not realizing I had to open the ports :P