I am using docker compose with a .net core service and RabbitMQ. I try to connect to rabbitMQ from a different service. I've seen many questions but none of them worked or had an answer.
I know you should not use 'localhost' to connect with another service but the name you gave in the docker-compose file.
I am really curious about the answer!
What I tried
- Add depends on in the docker compose file
- Add links in the docker compose file
- Add restart: always in the docker compose file
- Changed the connection link to : amqp://guest:guest@rabbitmq:5672
- Changed the connection link to : amqp://guest:guest@rabbitmq/
- Cleaned and rebuilt the solution many times
- Changed the order in the docker compose file. RabbitMQ is now Nr.1, after that the other services.
- Added container_name
- Added hostname
Here is my docker compose file
version: '3.0'
services:
rabbitmq:
container_name: rabbitmq
hostname: rabbitmq
image: rabbitmq:3-management
ports:
- "7100:15672"
- "7101:5672"
volumes:
- rabbitmq:/rabbitmq
dbPosts:
image: mysql:latest
ports:
- 3307:3306
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: postsdb
volumes:
- dbpostdata:/var/lib/mysql
post-service:
depends_on:
- dbPosts
- rabbitmq
build:
context: .
dockerfile: post-service/Dockerfile
ports:
- "8081:80"
volumes:
dbpostdata:
rabbitmq:
How the connection is made :
if (_connection == null)
{
ConnectionFactory factory = new ConnectionFactory()
{
Uri = new Uri("amqp://guest:guest@rabbitmq:5672"),
AutomaticRecoveryEnabled = true
};
_connection = factory.CreateConnection();
}
But when I try to run docker-compose up, I receive the following error message:
Unhandled Exception: RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable ---> RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: No such device or address
post-service
, and not from a development environment on your host? – David Mazeservices.AddMessagePublishing("PostService");
. And from that it will make the connection I think – Jay