Search code examples
c#dockermicroservicesdocker-for-windows

Call Microservice from another Microservice within Docker


I created several Microservices in C# that are running on docker in windows, I need to call Microservice from another Microservice so I used this way to call:

    [HttpGet("GetOrder/{Object_ID}")]
    public Order GetOrder (int id)
    {
        string Baseurl = "http://189.29.0.100/";
        …..

        using (var client = new HttpClient())
        {
            //Passing service base url  
            client.BaseAddress = new Uri(Baseurl);

            client.DefaultRequestHeaders.Clear();
            //Define request data format  
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //Sending request to find web api REST service resource GetAllEmployees using HttpClient  
            borrowerData = await client.GetStringAsync("api/order/" + Id.ToString());

        }

       …
    }

I used the fix IP in Composed file as follows:

 orderservice:
    environment:
     - ASPNETCORE_ENVIRONMENT=Development
   ports:
  - "80"
networks:
  default:
    ipv4_address: 189.29.0.100

The problem is when we deploy this project in VM, how to make it work with these Ips?


Solution

  • Docker compose create a virtual network on you host and the services are not routable from outside of docker with those IPs. But inside the virtual network all services can access to each other with their names (http://otherservice/). If you want to access to all deployed microservices on VM you will need a reverse proxy inside your docker compose. For example an nginx service that dispatch request to other services.