在docker容器中运行RabbitMQ

时间:2019-07-12 12:29:41

标签: c# docker docker-compose rabbitmq dockerfile

关于RabbitMQ,我还很陌生,我正在研究POC,以运行在Docker容器中使用RabbitMQ的C#解决方案。

我设法编写了docker-compose.yml,dockerfile并运行RabbitMQ。但是,我的解决方案无法到达RabbitMQ主机。我想我可能缺少一些配置,但是我不知道是什么。

docker-compose.yml

version: '3.4'

services:
  rabbit-sender:
    image: rabbit-sender
    container_name: rabbit-sender
    build: 
        context: ../SenderRabitMQ
        dockerfile: debug.Dockerfile
    env_file: common.env
    networks:
      - rabbitPoc
    expose:
      - "80"

  rabbit-receiver:
    image: rabbit-receiver
    container_name: rabbit-receiver
    build: 
      context: ../ReceiveRabitMQ
      dockerfile: debug.Dockerfile
    env_file: common.env
    networks:
      - rabbitPoc
    expose:
      - "80"

  rabbitmq:
    image: rabbitmq:3.7.15
    hostname: rabbitmq
    build: 
      context: rabbit
      dockerfile: debug.Dockerfile
    ports:
      - "5672:5672"
      - "15672:15672"
    volumes:
      - "./enabled_plugins:/etc/rabbitmq/enabled_plugins"

debug.Dockerfile

Install RabbitMQ
FROM ubuntu:14.04.1

CMD docker pull dockerfile/rabbitmq
CMD docker build -t="dockerfile/rabbitmq" github.com/dockerfile/rabbitmq

FROM dotnet-core-sdk-2.1-debug:latest AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY SenderRabitMQ/SenderRabitMQ.csproj SenderRabitMQ/
RUN dotnet restore SenderRabitMQ/SenderRabitMQ.csproj

# Copy everything else and build
COPY ./ ./
RUN dotnet publish SenderRabitMQ/SenderRabitMQ.csproj -c Debug -o out --no-restore

# Build runtime image
FROM dotnet-core-aspnet-2.1-debug:latest
WORKDIR /app

COPY --from=build-env /app/SenderRabitMQ/out .

ENTRYPOINT ["dotnet", "SenderRabitMQ.dll"]

RUN命令

docker run  --hostname myrabbit rabbitmq:3

连接到RabbitMQ


var factory = new ConnectionFactory() { HostName = "myrabbit:5672" , DispatchConsumersAsync = true };

这是运行RabbitSender时收到的错误,该消息应该将消息发布到队列中。

rabbit-sender      | 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

3 个答案:

答案 0 :(得分:4)

您的docker compose将RabbitMQ服务主机名设置为Rabbitmq而不是myrabbit(这是您要连接的名称)。尝试以下方法:

var factory = new ConnectionFactory() { HostName = "rabbitmq", port = 5672 , DispatchConsumersAsync = true };

您还需要Dockerfile rabbitmq部分与其他服务位于同一网络上

rabbitmq:
    image: rabbitmq:3.7.15
    hostname: rabbitmq
    build: 
      context: rabbit
      dockerfile: debug.Dockerfile
    ports:
      - "5672:5672"
      - "15672:15672"
    networks:
      - rabbitPoc
    volumes:
      - "./enabled_plugins:/etc/rabbitmq/enabled_plugins"

希望有帮助!

答案 1 :(得分:1)

您应该使用

HostName = "http://host.docker.internal:5672"

HostName = "host.docker.internal:5672"

代替

HostName = "myrabbit:5672"

原因是:

  

主机的IP地址正在更改(如果您没有网络,则没有IP地址)   访问)。从18.03开始,我们的建议是连接到   特殊的DNS名称host.docker.internal,解析为内部   主机使用的IP地址。这是出于发展目的,将   在Docker Desktop以外的生产环境中无法运行   Windows。

https://docs.docker.com/docker-for-windows/networking/

答案 2 :(得分:0)

在Docker容器中,您要与本地设置Rabbitmq建立连接的地方,您需要提供以下主机

  

host.docker.internal

它将起作用。