Docker Containers and localhost: Cannot Assign Requested Address

Look out for your use of ‘localhost’ when using Docker and Docker Compose

Christopher Laine
IT Dead Inside
Published in
4 min readNov 9, 2019

--

This one might not be obvious at first glance. I hope this helps someone out there who may run into this same issue.

I’ve got an ASP.NET Core website which connects to an API. The website uses a proxy class to call the API, which connects to the API’s base URL, and then constructs calls to various API endpoints on behalf of the website.

The API and the website run as totally separate processes, as the API is used by other systems beyond the website.

For local dev and testing, the URLs in question for the website and the API are as follows:

  • Website: https://localhost:4222 (running in IIS Express)
  • API: https://localhost:5555 (running in Docker)

Now, when I run up the website in Visual Studio, using IIS Express, everything is hunky dory. Website calls the proxy, the proxy calls the API Docker container at port 5555, and everything runs smoothly.

However, in an effort to port the website to Docker and Docker Compose, I created a Docker and Docker Compose file, then fired up my website. The website’s API proxy class kept barfing up this error message every time I tried to call any API endpoints.

Cannot assign requested address

What the heck?

Docker Container Context: localhost is NOT your host workstation

Now that error message was not my proxy class. That was Docker complaining, and for good reason.

It is important to understand the use of the term ‘localhost’ when dealing with Docker on your workstation. Here’s a diagram of how it all works when running the website on my workstation under IIS Express.

My workstation is ‘localhost’, with the API and Website as apps running in that context

As you can see, my workstation is localhost. Any and all calls to localhost will be routed to my workstation for DNS resolution (all machines resolve themselves to…

--

--

Christopher Laine
IT Dead Inside

Author, programmer, would-be philosopher. Author of Screens https://christopherlaine.net/screens