title: Cannot assign requested address (localhost:xxxx) - Docker + Linux Containers
body: <p>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.</p>
  <p>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.</p>
  <p>Now, my problem is, the post request to API-2 always returns <strong>Cannot assign requested address (localhost:XXXX)</strong>.</p>
  <p>If I try without docker, it works fine.
  Also, a separate request to each of the API works fine (using POSTMAN)</p>
  <p>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.</p>
  <p>Here is my docker-compose file as well as the docker file for both of the APIs.</p>
  <p><em><strong>Docker-Compose.yml</strong></em></p>
  <pre><code>version: &quot;3.7&quot;
  
  networks : 
    localdev:
      name: localdev
      external: true
  
  services:
    api-01:
      build:
        context: .
        dockerfile: api-01/Dockerfile
      restart: always
      container_name: &quot;api-01&quot;
      ports:
         - &quot;8082:80&quot;
      environment:
        ASPNETCORE_ENVIRONMENT: &quot;Development&quot;
      networks:
       - localdev
  
    api-02:
      build:
        context: .
        dockerfile: api-02/Dockerfile
      restart: always
      container_name: &quot;api-02&quot;
      ports:
         - &quot;8083:80&quot;
      environment:
        ASPNETCORE_ENVIRONMENT: &quot;Development&quot;
      networks:
       - localdev
  </code></pre>
  <p><strong>DockerFile(API-1)</strong></p>
  <pre><code>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 [&quot;api-01/api-01.csproj&quot;, &quot;api-01/&quot;]
  COPY [&quot;CommonEntities/CommonEntities.csproj&quot;, &quot;CommonEntities/&quot;]
  RUN dotnet restore &quot;api-01/api-01.csproj&quot;
  COPY . .
  WORKDIR &quot;/src/api-01&quot;
  RUN dotnet build &quot;api-01.csproj&quot; -c Release -o /app/build
  
  FROM build AS publish
  RUN dotnet publish &quot;api-01.csproj&quot; -c Release -o /app/publish
  
  FROM base AS final
  WORKDIR /app
  COPY --from=publish /app/publish .
  ENTRYPOINT [&quot;dotnet&quot;, &quot;api-01.dll&quot;]
  </code></pre>
  <p><strong>DockerFile(API-2)</strong></p>
  <p>FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
  WORKDIR /app
  EXPOSE 80</p>
  <pre><code>FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
  WORKDIR /src
  COPY [&quot;api-02/api-02.csproj&quot;, &quot;api-02/&quot;]
  COPY [&quot;CommonEntities/CommonEntities.csproj&quot;, &quot;CommonEntities/&quot;]
  RUN dotnet restore &quot;api-02/api-02.csproj&quot;
  COPY . .
  WORKDIR &quot;/src/api-02&quot;
  RUN dotnet build &quot;api-02.csproj&quot; -c Release -o /app/build
  
  FROM build AS publish
  RUN dotnet publish &quot;api-02.csproj&quot; -c Release -o /app/publish
  
  FROM base AS final
  WORKDIR /app
  COPY --from=publish /app/publish .
  ENTRYPOINT [&quot;dotnet&quot;, &quot;api-02.dll&quot;]
  </code></pre>
  <p>I'm not sure what I'm missing here!
  I have tried most of the solutions from the internet, but nothing works.</p>