ahmed meraj
ahmed meraj

Reputation: 886

Connect RabbitMQ container from another container

I created rabbitmq container by running the below command

docker run -d --hostname My-rabbit --name test-rabbit -p 15672:15672 rabbitmq:3-management

Created a user called userrabbit and given the permissions as below

rabbitmqctl add_user userrabbit password
rabbitmqctl set_user_tags userrabbit administrator
rabbitmqctl set_permissions -p / userrabbit ".*" ".*" ".*"

IP of this(test-rabbit) is 172.17.0.3

I created one more container(172.17.0.4) in which my application is running and in which I need to provide the url of the rabbitmq and I've provided the url as below

transport_url = rabbit://userrabbit:[email protected]:15672/

In the logs of container(172.17.0.4), it's showing as

AMQP server 172.17.0.3:15672 closed the connection. Check login credentials: Socket closed

But I"m able to ping the RabbitMq from the container(172.17.0.4) with the same credentials as shown below

curl -i -u userrabbit:password http://172.17.0.3:15672/api/whoami
HTTP/1.1 200 OK
vary: Accept-Encoding, origin
Server: MochiWeb/1.1 WebMachine/1.10.0 (never breaks eye contact)
Date: Tue, 14 Feb 2017 17:06:39 GMT
Content-Type: application/json
Content-Length: 45
Cache-Control: no-cache

{"name":"userrabbit","tags":"administrator"}

Upvotes: 3

Views: 5723

Answers (3)

Alex Punnen
Alex Punnen

Reputation: 6224

Create a docker network so that rabbitmq client can connect to rabbitmq server, both running as docker containers. Ex

docker network create sdk-net

Then run the rabbitmq docker using that network and give a name to the same

docker run -d --rm --name demo-rabbit --net sdk-net -p 5672:5672 -p 15672:15672 rabbitmq:3.6.15-management-alpine

Run you client like (note the network name in the run command sdk-net

docker run --rm -p 8090:8090 --net sdk-net pythontest

In your client the name of Docker container is reachable. So AMQ connection string will become like

amqp_url ='amqp://demo-rabbit:5672/'

Upvotes: 0

Derick Bailey
Derick Bailey

Reputation: 72868

2 things...

first thing:

it's port 5672 for the transport_url

the 15672 port you listed is the web admin console

and second thing:

you need to network you containers together via docker networking.

easiest way is with the --link option, provided to the second container at run time.

docker run --link test-rabbit:test-rabbit (... other options here)

by adding the --link test-rabbit:test-rabbit option, your application will be able to see test-rabbit as a valid network name with ip address.

updating your transport url

with these two fixes, your transport_url then becomes this

transport_url = rabbit://userrabbit:password@test-rabbit:5672/

other networking options

using --link is the easiest way to start, but isn't very scalable.

docker-compose makes it really easy to add links between containers

and you can also create custom docker networks via command-line tools and docker-compose, to connect containers together. that's more work, but better long-term.

Upvotes: 2

cantSleepNow
cantSleepNow

Reputation: 10192

You need to specify a hostname for each docker container with --hostname option and to add /etc/host entries for all the other containers, this you can do with --add-host option or by manually editing /etc/hosts file.

First create a network so that you can assign IPs: docker network create --subnet=172.18.0.0/16 mynet1 Then run the containers:

docker run -d --net mynet1 --ip 172.18.0.11 --hostname rab1 --add-host rab2:172.18.0.12 --name rab1con -p 15672:15672 rabbitmq:3-management

and the second one

docker run -d --net mynet1 --ip 172.18.0.12 --hostname rab2 --add-host rab1:172.18.0.11 --name rab2con -p 15673:15672 rabbitmq:3-management

Upvotes: 0

Related Questions