MassTransit on Docker container cannot access host machine's RabbitMQ instance
See original GitHub issueI created a simple .net core console application with docker support. Following Masstransit code fails to connect to RabbitMQ instance on host machine. But implementation using RabitMq.Client and ‘RabbitMqAddressExtensions’ is able to connect to host machine RabbitMQ instance without any problem. – | –
Using MassTransit .net core library. This implementation throws ‘RabbitMqConnectionException: Connect failed: ctas@10.248.69.31:5672/watcherindustry —> RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable —> RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed —> System.TimeoutException: The operation has timed out. at RabbitMQ.Client.Impl.TaskExtensions.<TimeoutAfter>d__1.MoveNext()’
` string userName = “ct”; string password = “ct@123”; string assetServiceQueue = “hello”;
var bus = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
var host = cfg.Host("10.48.9.31", 5672, "test", rabbitMqUri, hst =>
{
hst.Username(userName);
hst.Password(password);
});
cfg.ReceiveEndpoint(host,
assetServiceQueue, e =>
{
});
});
Using RabbitMQ.Client library for .net core and MassTransit source code ‘RabbitMqAddressExtensions’ this version can access host machine’s RabbitMQ instance successfully.
`var factory = new ConnectionFactory
{
AutomaticRecoveryEnabled = false,
NetworkRecoveryInterval = TimeSpan.FromSeconds(1),
TopologyRecoveryEnabled = false,
HostName = "10.48.9.31",
Port = 5672,
VirtualHost = "test" ?? "/",
RequestedHeartbeat = 0,
RequestedConnectionTimeout = 10000
};
factory.UserName = "ct";
factory.Password = "ct@123";
//if (settings.ClusterMembers != null && settings.ClusterMembers.Any())
//{
// factory.HostName = null;
// factory.EndpointResolverFactory = x => new SequentialEndpointResolver(settings.ClusterMembers);
//}
//if (settings.UseClientCertificateAsAuthenticationIdentity)
//{
// factory.AuthMechanisms.Clear();
// factory.AuthMechanisms.Add(new ExternalMechanismFactory());
// factory.UserName = "";
// factory.Password = "";
//}
//else
//{
// if (!string.IsNullOrWhiteSpace(settings.Username))
// factory.UserName = settings.Username;
// if (!string.IsNullOrWhiteSpace(settings.Password))
// factory.Password = settings.Password;
//}
var defaultOptions = new SslOption();
factory.Ssl.Enabled = false;
factory.Ssl.Version = defaultOptions.Version;
factory.Ssl.AcceptablePolicyErrors = SslPolicyErrors.RemoteCertificateChainErrors;
factory.Ssl.ServerName = null;
factory.Ssl.Certs = null;
if (string.IsNullOrWhiteSpace(factory.Ssl.ServerName))
factory.Ssl.AcceptablePolicyErrors |= SslPolicyErrors.RemoteCertificateNameMismatch;
if (string.IsNullOrEmpty(""))
{
factory.Ssl.CertPath = "";
factory.Ssl.CertPassphrase = "";
}
//else
//{
// factory.Ssl.CertPath = settings.ClientCertificatePath;
// factory.Ssl.CertPassphrase = settings.ClientCertificatePassphrase;
//}
factory.ClientProperties = factory.ClientProperties ?? new Dictionary<string, object>();
var currentProcess = Process.GetCurrentProcess();
factory.ClientProperties["client_api"] = "MassTransit";
factory.ClientProperties["masstransit_version"] = "4.0";
factory.ClientProperties["net_version"] = "1.6";
factory.ClientProperties["hostname"] = Environment.MachineName;
factory.ClientProperties["connected"] = DateTimeOffset.Now.ToString("R");
factory.ClientProperties["process_id"] = currentProcess.Id.ToString();
factory.ClientProperties["process_name"] = currentProcess.ProcessName;
var entryAssembly = System.Reflection.Assembly.GetEntryAssembly();
var assemblyName = entryAssembly.GetName();
if (assemblyName.Name != null)
factory.ClientProperties["assembly"] = assemblyName.Name;
if (assemblyName.Version != null)
factory.ClientProperties["assembly_version"] = FileVersionInfo.GetVersionInfo(entryAssembly.Location).FileVersion;
//if (string.IsNullOrEmpty(settings.ClientProvidedName))
//{
factory.ClientProperties["connection_name"] = $"{Environment.MachineName}.{assemblyName.Name}_{currentProcess.ProcessName}";
//}
// else
//{
// factory.ClientProperties["connection_name"] = rabbitMqUri;
//}`
(write your answer here)
Can you also reproduce the problem with the lastest version?
Yes.
Environment
.net core 1.1 console app with Docker support. Used VS 2017 to deploy containers. Windows 10
Steps to Reproduce
Create a .net core 1.1 console app with Docker support and then copy and paste above code snippets to main.
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)

Top Related StackOverflow Question
Thanks for the response. I managed to resolve this issue. My findings are as follows.
to connect to a rabbitmq instance on another docker container, they have to be moved/connected to the same network. To do this create a newtork
docker network create -d bridge my_bridgeconnect both app and rabbitmq containers to same networkdocker network connect my_bridge webFor masstransit uri use rabbitmq container IP on that network or container nameTo connect rabbitmq instance of host machine from a app on docker container. masstransit uri should include Machine name ( I tried IP, that did not work)
I played around a bit more and was able to connect my example to my host
should see the following output
Notes:
172.17.0.2was my-rabbit container ip address but you can replace it with your machine ip address http://localhost:15672 is the rabbitmq management console log in withguestas username and password.Side note portainer.io useful application to view docker environment locally