question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MassTransit on Docker container cannot access host machine's RabbitMQ instance

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
patolaxcommented, Aug 14, 2017

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_bridge connect both app and rabbitmq containers to same network docker network connect my_bridge web For masstransit uri use rabbitmq container IP on that network or container name

  • To 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)

1reaction
gertjvrcommented, Aug 12, 2017

I played around a bit more and was able to connect my example to my host

  • Step 1: Start rabbitmq in docker and expose ports on the host
docker run -d -p 5672:5672 -p 15672:15672 --hostname my-rabbit --name some-rabbit rabbitmq:3-management
  • Step 2: Build and run console app.
docker build -t dotnetapp .
docker run -d -e RABBITMQ_URI=rabbitmq://guest:guest@172.17.0.2:5672 --name some-dotnetapp dotnetapp
  • Step 3: Verify your receiving messages.
docker logs some-dotnetapp --follow

should see the following output

Application is starting...
Connecting to rabbitmq://guest:guest@172.17.0.2:5672
Received: Hello, World [08/12/2017 04:35:53]
Received: Hello, World [08/12/2017 04:35:58]
Received: Hello, World [08/12/2017 04:36:03]
Received: Hello, World [08/12/2017 04:36:08]
Received: Hello, World [08/12/2017 04:36:13]
...
  • Notes: 172.17.0.2 was 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 with guest as username and password.

  • Side note portainer.io useful application to view docker environment locally

Read more comments on GitHub >

github_iconTop Results From Across the Web

Masstransit cannot access host machine RabbitMQ from a ...
My findings are as follows. to connect to a rabbitmq instance on another docker container, they have to be moved/connected to the same...
Read more >
Binding your docker app container to rabbitmq container
Lets first start by downloading the rabbitmq-3-management image and running the container on your local workstation. ... Once the container starts ...
Read more >
Creating a MassTransit client/server application using ...
Let's test the versatile MassTransit framework using RabbitMQ, .NET Core and Linux and see if it can serve as a reliable messaging system....
Read more >
Configuration
MassTransit adds a hosted service so that the generic host can start and stop the bus (or buses, if multiple bus instances are...
Read more >
RabbitMQ URI Specification
This specification defines an "amqp" URI scheme. Conforming URIs represent the information needed by AMQP 0-9-1 clients as well as some RabbitMQ plugins...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found