Sugumar
Sugumar

Reputation: 303

Cannot assign requested address (localhost:xxxx) - Docker + Linux Containers

We have a WPF application that will call a Web API called API-1 which is running in a docker container. I also have another API called API-2 which is also running in a docker but a different container.

Whenever the API-1 call happened from the application, the API-1 will do some logic and try to do a post request to API-2.

Now, my problem is, the post request to API-2 always returns Cannot assign requested address (localhost:XXXX).

If I try without docker, it works fine. Also, a separate request to each of the API works fine (using POSTMAN)

This problem occurs only if the API deployed in the docker. I was using docker-compose to create the containers. I have created a docker network bridge and allocated it to the respective APIs in the docker-compose.yml file.

Here is my docker-compose file as well as the docker file for both of the APIs.

Docker-Compose.yml

version: "3.7"

networks : 
  localdev:
    name: localdev
    external: true

services:
  api-01:
    build:
      context: .
      dockerfile: api-01/Dockerfile
    restart: always
    container_name: "api-01"
    ports:
       - "8082:80"
    environment:
      ASPNETCORE_ENVIRONMENT: "Development"
    networks:
     - localdev

  api-02:
    build:
      context: .
      dockerfile: api-02/Dockerfile
    restart: always
    container_name: "api-02"
    ports:
       - "8083:80"
    environment:
      ASPNETCORE_ENVIRONMENT: "Development"
    networks:
     - localdev

DockerFile(API-1)

FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["api-01/api-01.csproj", "api-01/"]
COPY ["CommonEntities/CommonEntities.csproj", "CommonEntities/"]
RUN dotnet restore "api-01/api-01.csproj"
COPY . .
WORKDIR "/src/api-01"
RUN dotnet build "api-01.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "api-01.csproj" -c Release -o /app/publish

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

DockerFile(API-2)

FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base WORKDIR /app EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["api-02/api-02.csproj", "api-02/"]
COPY ["CommonEntities/CommonEntities.csproj", "CommonEntities/"]
RUN dotnet restore "api-02/api-02.csproj"
COPY . .
WORKDIR "/src/api-02"
RUN dotnet build "api-02.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "api-02.csproj" -c Release -o /app/publish

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

I'm not sure what I'm missing here! I have tried most of the solutions from the internet, but nothing works.

Upvotes: 18

Views: 42613

Answers (2)

user1080381
user1080381

Reputation: 1786

Using host.docker.internal instead of localhost worked for me.

Source: https://hamy.xyz/labs/docker-dotnet-core-cannot-assign-requested-address

Upvotes: 15

Sugumar
Sugumar

Reputation: 303

It looks like I need to use {DOCKER_IP} instead of "localhost" if I want to have communication between two API in two different containers.

After I changed my post request to hit "http://{My_PC_IP_Address}:8082", the request was successful.

Thanks to David for redirecting me to the right documentation https://docs.docker.com/network/bridge/

Upvotes: 8

Related Questions