ASP.Net 5 cannot send requests to localhost in Docker enviroment

I’ve created two application, which run on the torizon in the different containers.

  1. Web-API application listens port 8842 and gives some information back, and sends REST queries to the second application, what listens to the 8841. It works fine

  2. ASP.Net Core 5 application listens to the port 8841 and it works fine as well. But if it tries to send REST-query to the first application it gets an exception

    settings = await client.GetFromJsonAsync(“settings”);

Exception has occurred: CLR/System.Net.Http.HttpRequestException
Eine Ausnahme vom Typ “System.Net.Http.HttpRequestException” ist in System.Private.CoreLib.dll aufgetreten, doch wurde diese im Benutzercode nicht verarbeitet.: ‘Cannot assign requested address (localhost:8842)’
Innere Ausnahmen gefunden. Weitere Informationen hierzu finden Sie unter “$exception” im Variablenfenster.
Innere Ausnahme System.Net.Sockets.SocketException: Cannot assign requested address

The client base address is defined as: http://localhost:8842/api/Exchange/
If I use, it works, but I have to use a localhost because in the production there is no external networks.

Greetings @programmier-hirsch,

Could you go into more detail on how you’re exactly exposing the ports and such. This seems like a port mapping issue.

Just to clarify I assume you’re applications are using port 8841 and port 8842 in the container. But how are they being mapped outside of the container? For example in the first container you have port 8842, but this port doesn’t “exist” in the second container since each container usually has their own network stack.

You would need to do something like map port 8842 in the container to port 1000 (for example) outside of the container. The the second container could then map in port 1000 to port 8842. Then through this way the second container could see the port of the first container.

With regards to localhost what happens is that each container has their own localhost. This blog here explains the container networking stack in more detail:

Best Regards,

Thank you @jeremias.tx for your answer,

I’ve seen this article already but haven’t rrealized yet how to implement it.
I suppose I have to add a network in portainer and then create two docker-compose files with referencies.

But I think the first application uses a host network because it is nesassary for CAN programming (I had to add extraparams: – cap_add with value CAP_NET_ADMIN – network_mode with value host). May be it’s a reason why this app can send messages to the local host.

I’ve tried to set network_mode=host in the second application as well, but it didn’t work. The application exited immediately after start.

I’ve read hier
another answer, but it’s connected to the Docker 20+

Ahh okay I think I understand your setup now.

So the web-API application listens on port 8842. This container is running on the host network stack so it’s listening on the host port 8842.

The ASP.NET container then send REST-queries on port 8842. But this container is using the container network stack separate from the host. What I imagine you need is an argument on container startup that maps port 8842 in the ASP.NET container to port 8842 on the host. If that makes sense. Basically add something like -p 8842:8842 to your docker run command or an equivalent field in the compose file.

Then I think if you send on localhost in the ASP.NET container it will send to the localhost in the container not the actual host. But since port 8842 in the container will be mapped to outside the container it “should” be ok.

There might be some strange issues with trying to mix two containers where one is using host network but I’m not currently aware of any such issues.

Best Regards,

An alternative solution, if the 2nd container (the one receiving the REST request) should not be exposed to the outside network you may create a private network and have the two containers joining it. If you assign a name to your container (via docker compose or docker command line) you can then use that name to reference the container instead of localhost or a fixed ip.

Thanx @jeremias.tx,
is it possible to add this key in torizon Add-In for VS Code? Or I have to write a docker file?
I don’t start docker directly at the moment, where I can add this key -p 8842:8842 ?

You can add the option via project properties -> Torizon C/C++ Application -> Configuration.

In there, there is a ports option. You can add the mapping here though the syntax is a bit different but the extension should correct you on the syntax. Will be something like 8842/tcp:8842.

Best Regards,