Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grpc server not working in docker-compose

I build sample project with grpc server.in vs every things is ok and my project nice worked. dokcer-compose.yaml is :

version: '3.3' 
services:
  grpcService:
    container_name: grpcserver
    image:  grpcImage
    ports:
      - "5003:5003"  //client
      - "5001:5001"  //grpc server
    restart: always

in project I set http://127.0.0.1:5001 for grpc server

and set http://127.0.0.1:5003 for client I handle (grpc server worked with http)

when docker-compose up and call api ,get faild and grpc server not found , etc

like image 617
Your Next Avatar asked Jan 25 '23 17:01

Your Next


2 Answers

you must change CreateHostBuilder to this code in program.cs Grpc Server

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>().ConfigureKestrel(options =>
                {
                    options.Listen(IPAddress.Any, 5001, listenOptions =>
                    {
                        listenOptions.Protocols = HttpProtocols.Http2;
                    });
                });
            });
}

5001 is grpc Server Port.(change this port to your grpc server port)

like image 53
mehdi farhadi Avatar answered Feb 04 '23 07:02

mehdi farhadi


It's not entirely clear from your question how your client and server are separated; I would usually expect to see 2 services in the Docker Compose file: one for the server and a second for the client.

It is probable (!) that your client is unable to connect to the server because you are using the loopback (127.0.0.1) address. Loopback is a short-circuit way to route traffic within a machine that may not use the machine's network stack. For this reason, loopback traffic is not exposed beyond the container and is thus inaccessible to your client.

It is better to use 0.0.0.0 as the server's address when you run (!) the server so that the server will be bound to the networking stack and can be used by the host and other services (containers). In your Docker Compose file, this service will be given the DNS name of grpcService (probably lowercase only!?) and can be accessed from another service running in the Docker Compose file (network) as grpcservice:5003.

NOTE With the Docker Compose file (and its network), the server will be bound to whatever port you run it as e.g. 5003. The ports: - "XXXX:5003" is redundant *unless you wish to access this service from the host machine (outside of the Docker Compose network) when it will be available on port XXXX.

like image 42
DazWilkin Avatar answered Feb 04 '23 08:02

DazWilkin