Search code examples
dockerdocker-composedocker-network

Docker Network Compose


I have a docker compose file that is going to run MongoDB and RabbitMQ. I have another docker file that will be running a C# application (Consumer) that will read from the RabbitMQ and will be uploading in the MongoDB and will be running a Javascript A third docker file will run the Front end with a C# backend to pull and update records from the Mongo DB.

When running locally the Consumer connects to the Docker RabbitMQ Port 15672 and MongoDB Port 27017 using LocalHost. Everything works locally find, When I attempt to run the same application in docker the Consumer cannot connect to the RabbitMQ. I have named my Network, but I think I need something more because they are running in 2 to 3 different containers.

I have tried connecting using the Host ai-rabbitmq, rabbitmq_go_net and using a specific IP Address.

version: '3.1'
services:
  mongo:
    image: mongo
    restart: always
    container_name: ai-mongodb
    ports:
      - '27017:27017'
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
  mongo-express:
    image: mongo-express
    restart: always
    container_name: ai-mongoexpress
    ports:
      - '8081:8081'
    volumes:
      - 'C:\MongoDb\data:/data/db'
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: 'mongodb://root:example@ai-mongodb:27017/'
  rabbitmq:
    image: 'rabbitmq:3-management-alpine'
    container_name: ai-rabbitmq
    ports:
      - '5672:5672'
      - '15672:15672'
    volumes:
      - '~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/'
      - '~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq'
    networks:
      - rabbitmq_go_net
networks:
  rabbitmq_go_net:
    driver: bridge

This is my current docker file for the consumer

#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Consumer/Consumer.csproj", "Consumer/"]
RUN dotnet restore "Consumer/Consumer.csproj"
COPY . .
WORKDIR "/src"
RUN dotnet build "Consumer/Consumer.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Consumer/Consumer.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Consumer.dll"]

Solution

  • Why not able to connect with localhost ?

    In the context of a container, localhost means the container it self. So that you can't connect to the rabbitMQ from C# application.

    Why not able to connect with container name ?

    Since you have the rabbitMQ running within a network rabbitmq_go_net. Only the applications which is in this network are able to connect with the host name (container name) ai-rabbitmq. Containers outside this network can't able connect.

    Solution

    • You can add all the services (mongo, mongo-express, rabitmq and c#) within the same network and able to connect by using the container name.
    • Or you can use the system ip instead of localhost or container-name to connect them.

    Note : Since you didn't mention a network for the mongo and mongo-express, the docker-compose will create a network by default and run these containers within it.


    Here is the updated compose.yml

    version: '3.1'
    services:
      mongo:
        image: mongo
        restart: always
        container_name: ai-mongodb
        ports:
          - '27017:27017'
        environment:
          MONGO_INITDB_ROOT_USERNAME: root
          MONGO_INITDB_ROOT_PASSWORD: example
        networks:
          - rabbitmq_go_net
    
      mongo-express:
        image: mongo-express
        restart: always
        container_name: ai-mongoexpress
        ports:
          - '8081:8081'
        volumes:
          - 'C:\MongoDb\data:/data/db'
        environment:
          ME_CONFIG_MONGODB_ADMINUSERNAME: root
          ME_CONFIG_MONGODB_ADMINPASSWORD: example
          ME_CONFIG_MONGODB_URL: 'mongodb://root:example@ai-mongodb:27017/'
        networks:
          - rabbitmq_go_net
    
      rabbitmq:
        image: 'rabbitmq:3-management-alpine'
        container_name: ai-rabbitmq
        ports:
          - '5672:5672'
          - '15672:15672'
        volumes:
          - '~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/'
          - '~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq'
        networks:
          - rabbitmq_go_net
    
    networks:
      rabbitmq_go_net:
        name: rabbitmq_go_net
        driver: bridge
    

    and the yml for C# application

    version: "3.8"
    services:
      ai-c:
        container_name: ai-c-app
        build: .
        restart: on-failure
        environment:
          MONGO_HOST: ai-mongodb
          MONGO_PORT: 27017
          RABBITMQ_HOST: ai-rabbitmq
          RABBITMQ_PORT: 5672
        networks:
          - rabbitmq_go_net
    
    networks:
      rabbitmq_go_net:
        external: true