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…

--

--