I have 3 docker images; rabbitmq, client and publisher image. Without using the docker images the three services communicate well on the default port 5672
.
Below is my docker-compose.yml
version: '3.4'
services:
rabbitmq.messageclient:
image: ${DOCKER_REGISTRY-}rabbitmqmessageclient
ports:
- "5672"
build:
context: .
dockerfile: RabbitMQ.Client/Dockerfile
rabbitmq.producer:
image: ${DOCKER_REGISTRY-}rabbitmqproducer
ports:
- "5672"
build:
context: .
dockerfile: RabbitMQ.Producer/Dockerfile
rabbitmq:
image: rabbitmq:3-management
container_name: 'rabbitmq'
ports:
- 5672:5672
- 15672:15672
volumes:
- ~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/mnesia
- ~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq
networks:
- rabbitmq_go_net
networks:
rabbitmq_go_net:
driver: bridge
my Client
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/runtime:5.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["RabbitMQ.Client/RabbitMQ.MessageClient.csproj", "RabbitMQ.Client/"]
RUN dotnet restore "RabbitMQ.Client/RabbitMQ.MessageClient.csproj"
COPY . .
WORKDIR "/src/RabbitMQ.Client"
RUN dotnet build "RabbitMQ.MessageClient.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "RabbitMQ.MessageClient.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "RabbitMQ.MessageClient.dll"]
my Producer
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/runtime:5.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["RabbitMQ.Producer/RabbitMQ.Producer.csproj", "RabbitMQ.Producer/"]
RUN dotnet restore "RabbitMQ.Producer/RabbitMQ.Producer.csproj"
COPY . .
WORKDIR "/src/RabbitMQ.Producer"
RUN dotnet build "RabbitMQ.Producer.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "RabbitMQ.Producer.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "RabbitMQ.Producer.dll"]
My hostname is : localhost
I am unable to send and receive data between the rabbitmq.messageclient
and the rabbitmq.producer
service. What am I missing?
CodePudding user response:
In short, each container behaves like an isolated machine, localhost
in a container refers to its local network. Communication between two containers is the same as communication between two machines with respective IP addresses. docker-compose
creates a bridged network for all containers by default so you can use containers' name to connect to them, and Docker internally do the address translation for you.
Not sure why you create a new bridge called rabbitmq_go_net
but connect only rabbitmq
to it. You may delete the network or add another two container to it as well. Then in your rabbitmq.messageclient
container, you may connect the rabbitmq
container using rabbitmq:5672
instead of localhost:5672
, and so on.
References for docket compose network:
- https://docs.docker.com/compose/networking/
- https://docs.docker.com/compose/compose-file/compose-file-v3/#network
CodePudding user response:
If you want to communicate with services inside your compose file, you should use service name as hostname. Also, "ports" section used for mapping ports to the host machine, you don't need it to connect between services.