MassTransit RabbitMQ connection dropping immediately

I’m setting up a project using MassTransit with RabbitMQ. I bring up RabbitMQ and my image from a docker-compose.yml file, but my image connects then immediately disconnects without a very useful error message

I’ve done some googling around and so far have been unsuccessful. If anyone could give me any pointers that’d be much appreciated!

Mass Transit initialisation:

            IBusControl CreateBus(IServiceProvider serviceProvider)
            {
                return Bus.Factory.CreateUsingRabbitMq(cfg => {
                cfg.Host("rabbitmq", h => {
                    h.Username("guest");
                    h.Password("guest");
                    });
                });
            }

            services.AddMassTransit(CreateBus);

docker-compose.yml:

version: "2"

services:
  rabbit:
    hostname: rabbit
    image: rabbitmq:3-management
    environment:
      - RABBITMQ_DEFAULT_USER=guest
      - RABBITMQ_DEFAULT_PASS=guest
    ports:
      - 5672:5672
      - 15672:15672

  my-cool-api:
    build:
      context: .
      dockerfile: ./Dockerfile
    image: my-cool-api
    container_name: my-cool-api
    hostname: my-cool-api
    ports:
      - 1070:80
    environment:
      - RabbitMq:Hostname=rabbit
    depends_on:
      - rabbit

Error stack trace:

rabbit_1       | 2020-01-20 16:22:02.040 [info] <0.570.0> accepting AMQP connection <0.570.0> (172.22.0.3:34100 -> 172.22.0.2:5672)
rabbit_1       | 2020-01-20 16:22:02.053 [info] <0.570.0> Connection <0.570.0> (172.22.0.3:34100 -> 172.22.0.2:5672) has a client-provided name: dotnet
rabbit_1       | 2020-01-20 16:22:02.056 [info] <0.570.0> connection <0.570.0> (172.22.0.3:34100 -> 172.22.0.2:5672 - dotnet): user 'guest' authenticated and granted access to vhost '/'
rabbit_1       | 2020-01-20 16:22:02.081 [info] <0.570.0> closing AMQP connection <0.570.0> (172.22.0.3:34100 -> 172.22.0.2:5672 - dotnet, vhost: '/', user: 'guest')
my-cool-api    | Unhandled exception. System.AggregateException: One or more errors occurred. (One or more errors occurred. (Broker unreachable: [email protected]:5672/))
my-cool-api    |  ---> System.AggregateException: One or more errors occurred. (Broker unreachable: [email protected]:5672/)
my-cool-api    |  ---> MassTransit.RabbitMqTransport.RabbitMqConnectionException: Broker unreachable: [email protected]:5672/
my-cool-api    |  ---> RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable
my-cool-api    |  ---> System.AggregateException: One or more errors occurred. (Connection failed)
my-cool-api    |  ---> RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed
my-cool-api    |  ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (111): Connection refused 172.22.0.2:5672
my-cool-api    |    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception source)
my-cool-api    |    at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
my-cool-api    |    at System.Net.Sockets.Socket.<>c.<ConnectAsync>b__275_0(IAsyncResult iar)
my-cool-api    | --- End of stack trace from previous location where exception was thrown ---
my-cool-api    |    at RabbitMQ.Client.TcpClientAdapter.ConnectAsync(String host, Int32 port)
my-cool-api    |    at RabbitMQ.Client.Impl.TaskExtensions.TimeoutAfter(Task task, Int32 millisecondsTimeout)
my-cool-api    |    at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectOrFail(ITcpClient socket, AmqpTcpEndpoint endpoint, Int32 timeout)
my-cool-api    |    --- End of inner exception stack trace ---
my-cool-api    |    at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectUsingAddressFamily(AmqpTcpEndpoint endpoint, Func`2 socketFactory, Int32 timeout, AddressFamily family)
my-cool-api    |    at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectUsingIPv4(AmqpTcpEndpoint endpoint, Func`2 socketFactory, Int32 timeout)
my-cool-api    |    at RabbitMQ.Client.Impl.SocketFrameHandler..ctor(AmqpTcpEndpoint endpoint, Func`2 socketFactory, Int32 connectionTimeout, Int32 readTimeout, Int32 writeTimeout)
my-cool-api    |    at RabbitMQ.Client.Framing.Impl.IProtocolExtensions.CreateFrameHandler(IProtocol protocol, AmqpTcpEndpoint endpoint, Func`2 socketFactory, Int32 connectionTimeout, Int32 readTimeout, Int32 writeTimeout)
...

Source: StackOverflow