Chubbyb0b
Chubbyb0b

Reputation: 21

How to connect two api-s using docker and rabbitmq?

I'm experiencing difficulties connecting two APIs, SQL, and RabbitMQ inside a Docker container. I've successfully connected three of them, but I'm unable to connect the helper API to receive messages sent by the main API through RabbitMQ. The messages are being sent and are visible in the Docker console of the main API and in RabbitMQ management. However, the issue lies in the fact that they are not being received in the helper API. I suspect the problem might be within the Dockerfile or Docker Compose because when they operate outside the Docker environment, they communicate properly.

Additionally, there's an unusual behavior in the helper API's console inside the Docker container. It seems to require a connection to SQL within Docker, whereas outside Docker, it doesn't connect to any database at all. Its sole purpose is to display RabbitMQ messages, so I'm uncertain about the root of this problem. I attempted to add the second API inside the Dockerfile, but both APIs ended up being identical copies of each other, leading to the entire system breaking down. That approach didn't yield any success. I lack experience with Docker, and any assistance provided would be greatly appreciated.

This is the docker file:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 7166
ENV ASPNETCORE_URLS=http://+:7166

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY . .

FROM build AS publish
RUN dotnet publish "eCakeShop/eCakeShop.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .

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

This is the docker compose file:

version: "3"
services:
  ecakeshop-sql:
    image: "mcr.microsoft.com/mssql/server:2022-latest"
    restart: unless-stopped
    environment:
      SA_PASSWORD: "***********"
      ACCEPT_EULA: "Y"
      MSSQL_PID: "Developer"
    ports:
      - "1434:1433"
    networks:
      - ecakeshopnet

  rabbitmq:
    image: "rabbitmq:latest"
    hostname: "rabbitmq"
    restart: unless-stopped
    ports:
      - "5672:5672"
      - "15672:15672"
    networks:
      - ecakeshopnet

  ecakeshop-api:
    restart: unless-stopped
    build: .
    environment:
      - "ConnectionStrings:eCakeShop=Server=ecakeshop-sql,1433;Database=eCakeShop;Encrypt=true;MultipleActiveResultSets=true;TrustServerCertificate=True;User=sa;Password=**********;"
      - "ASPNETCORE_ENVIRONMENT=Development"
    ports:
      - "7166:7166"
    networks:
      - ecakeshopnet
    depends_on:
      - ecakeshop-sql
      - rabbitmq
    links:
      - ecakeshop-sql

  helper-api:
    restart: unless-stopped
    build: .
    environment:
      - "ConnectionStrings:eCakeShop=Server=ecakeshop-sql,1433;Encrypt=true;MultipleActiveResultSets=true;TrustServerCertificate=True;User=sa;Password=***********;"
      - "ASPNETCORE_ENVIRONMENT=Development"
    ports:
      - "7215:7215"
    networks:
      - ecakeshopnet
    depends_on:
      - rabbitmq

networks:
  ecakeshopnet:
    driver: bridge

I attempted numerous rebuilds, altered ports and localhost settings, deleted the database, and even created a new helper API from scratch, but unfortunately, none of these attempts resolved the issue.

Upvotes: 1

Views: 86

Answers (1)

Guru Stron
Guru Stron

Reputation: 143098

There is at least one potential issue in your setup - ConnectionStrings:eCakeShop should be ConnectionStrings__eCakeShop. From Environment variable configuration provider docs:

The : delimiter doesn't work with environment variable hierarchical keys on all platforms. For example, the : delimiter is not supported by Bash. The double underscore (__), which is supported on all platforms, automatically replaces any : delimiters in environment variables.

Upvotes: 0

Related Questions