简体   繁体   中英

Connect to rabbitmq on local host from a Docker container

I have a java app running in a docker container and a rabbitmq running in local host (mac). The app need to communicate with the rabbitmq on local host, could some one point out how could i achieve this?

Docker engine provide a virtual network interface as a getway if you use --net="bridge" . It's a standard value. On linux you can know the name of the interface with ifconfig command.

docker0   Link encap:Ethernet  IndirizzoHW 02:42:56:6c:95:26  
      indirizzo inet:172.17.0.1  Bcast:0.0.0.0  Maschera:255.255.0.0
      indirizzo inet6: fe80::42:56ff:fe6c:9526/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:28267 errors:0 dropped:0 overruns:0 frame:0
      TX packets:25451 errors:0 dropped:0 overruns:0 carrier:0
      collisioni:0 txqueuelen:0 
      Byte RX:52948314 (52.9 MB)  Byte TX:25718113 (25.7 MB)

Inside my container I just use, in this case, 172.17.0.1 as gateway to access to my host.

Inside your container you can also show routes with the route command and see what is the default gateway

root@15fae92c516f:/# route
 Kernel IP routing table
 Destination     Gateway         Genmask         Flags Metric Ref    Use      Iface
 default         172.17.0.1      0.0.0.0         UG    0      0        0  eth0
 172.17.0.0      *               255.255.0.0     U     0      0        0  eth0
root@15fae92c516f:/# 

@Giorgio Thanks for your answer, it showed me the right place to look at. I couldnt get it running with your solution as I am using Docker on Mac and your solution is suitable for Linux environment.

Networing feature for Docker on Mac does not support docker0 interface as explained in https://docs.docker.com/docker-for-mac/networking/

I followed the given solution under the section "I want to connect from a container to a service on the host" of the above link.

Creating an alias on lo0 and adjusting the rabbitmq.config to listen on that IP solved my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM