2

I am trying to run docker-compose with two .net core console applications having dependency of rabbitMQ, I am using Docker with Windows.

I have added to console application, as described on RabbitMQ official docs https://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html

Outside of both applications I have added docker-compose.yml

Folder structure:

1. Send

  1. Send.cs (as it is in RabbitMQ docs)
  2. Send.csproj
  3. Dockerfile

    FROM mcr.microsoft.com/dotnet/core/sdk:2.2 as build-env
    WORKDIR /app
    
    # Copy the project file and restore the dependencies
    COPY *.csproj ./
    RUN dotnet restore
    
    # Copy the remaining source files and build the application
    COPY . ./
    RUN dotnet publish -c Release -o out
    
    # Build the runtime image
    FROM mcr.microsoft.com/dotnet/core/sdk:2.2
    WORKDIR /app
    COPY --from=build-env /app/out .
    ENTRYPOINT ["dotnet", "Send.dll"]
    

2. Recieve

  1. Recieve.cs (as it is in RabbitMQ docs)

  2. Recieve.csproj

  3. Dockerfile (Same as Sender Dockerfile except Send replaced with Recieve)

3. Docker-compose.yml

  version: '3'

    services:
      rabbitmq:
        image: rabbitmq:3-management
        ports:
          - "5672:5672"
          - "15672:15672"
        container_name: rabbitmq
        hostname: my-rabbit
      Send:
        image: sender
        build:
          context: ./Send
          dockerfile: Dockerfile
        depends_on:
            - rabbitmq
      Reciever:
        image: reciever
        build:
          context: ./Recieve
          dockerfile: Dockerfile
        depends_on:
            - rabbitmq

Error Thrown

Unhandled Exception: RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable ---> System.AggregateException: One or more errors occurred. (Connection failed) ---> RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: Connection refused 127.0.0.1:5672

halfer
  • 19,824
  • 17
  • 99
  • 186
harpal
  • 426
  • 4
  • 12
  • 1
    Are you connecting to the `rabbitmq` service from your host? – halfer Jul 05 '19 at 11:06
  • I am new to it, i have added RabbitMQClient from nuget package in both of applications. and i ma building(running up) compose file from my system. – harpal Jul 05 '19 at 11:19
  • 1
    You did not answer my question; please try to answer it directly. Is RabbitMQ being connected to just within your containers (one container to another), or do you want to connect from your host (outside of Docker)? – halfer Jul 05 '19 at 11:20
  • In my case RabbitMQ is within containers (one container to another), not outside the docker. – harpal Jul 05 '19 at 13:49
  • 1
    Great. You can remove the port publishing related to `rabbitmq`, in that case. Only open ports to the host if you need them. – halfer Jul 05 '19 at 14:16
  • Thanks, @halfer for your support, still I have not got success, I have to be more learn/prepared. I have added a repository if u want u can review it https://github.com/harpalsingh1993/DockerDemo – harpal Jul 05 '19 at 15:03
  • 1
    I would simplify this a bit - remove the `networks` and all references to it - all containers will appear on the default network anyway. Also remove the `hostname: my-rabbit` for the RabbitMQ service, and connect to `rabbitmq` instead - a magic DNS entry is made for the service name by default. Remove the ports, as I have suggested. – halfer Jul 05 '19 at 15:17
  • 1
    If you are still having problems, please update the post here - you cannot now be getting `127.0.0.1:5672` in an error, since you are not connecting to localhost. – halfer Jul 05 '19 at 15:18
  • I have made the change but still getting same error while using docker-compose up – harpal Jul 05 '19 at 15:46
  • OK, please ask a new question about that, and put the link here if you wish (the problem as expressed above is presumably no longer happening, since you are no longer connecting to RabbitMQ on `127.0.0.1`). Victor has correctly answered your first problem, and now you have a new one. `:=)` – halfer Jul 05 '19 at 18:08

2 Answers2

4

For making Docker containers able to communicate with each other, you need to set them in the same network. On your docker-compose.yml add the following:

version: '3'
    services:
      rabbitmq:
        image: rabbitmq:3-management
        ports:
          - "5672:5672"
          - "15672:15672"
        container_name: rabbitmq
        hostname: my-rabbit
        networks:
          - my-network-name
      Send:
        image: sender
        build:
          context: ./Send
          dockerfile: Dockerfile
        depends_on:
            - rabbitmq
        networks:
          - my-network-name
      Reciever:
        image: reciever
        build:
          context: ./Recieve
          dockerfile: Dockerfile
        depends_on:
            - rabbitmq
        networks:
          - my-network-name
networks:
  my-network-name:
    driver: bridge

As you can see, all the containers are in the same network, and they can communicate in the next way:

http://<container_name>:<port>

So if you want the Sender sends a message to RabbitMQ then:

amqp://rabbitmq:5672

If you use localhost as an IP inside any container, it will use its loopback interface, receiving the request itself.

  • Thanks, @Victor for your support, still I have not got success, I have to be more learn/prepared. I have added a repository if u want u can review it https://github.com/harpalsingh1993/DockerDemo – harpal Jul 05 '19 at 15:04
  • As per my comments on the other thread, I recommend you ask a new question Harpal, since Victor's solution for your first problem appears to be correct. – halfer Jul 05 '19 at 18:11
1

Victor's answer was also helpful but, problem was the rabbitmq starting later, while send and receiver started before.

While I restarted the same stopped sender and receiver container, it was working as expected(because the rabbitmq container was up). So I have added the retry policies on container, found from here.

The final version of my docker-compose file is looking like below, and is working fine. version: '3' services:

  rabbitmq:
    image: rabbitmq:3-management
    ports:
      - "5672:5672"
      - "15672:15672"
    container_name: rabbitmq
    hostname: my-rabbit
    networks:
      - my-network-name
    healthcheck:
        test: ["CMD", "curl", "-f", "http://localhost:15672"]
        interval: 30s
        timeout: 10s
        retries: 5

  Send:
    image: sender
    build:
      context: ./Send
      dockerfile: Dockerfile
    depends_on:
        - rabbitmq
    restart: on-failure
    networks:
      - my-network-name
    links: 
        - rabbitmq

  Reciever:
    image: reciever
    build:
      context: ./Recieve
      dockerfile: Dockerfile
    depends_on:
        - rabbitmq
    restart: on-failure
    networks:
      - my-network-name
    links: 
        - rabbitmq
networks:
  my-network-name:
    driver: bridge
harpal
  • 426
  • 4
  • 12