Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gRPC error: "address not available" on Docker with NestJS

I'm getting this error when starting my NestJS application on Docker with gRPC:

    {
        "created": "@1616799250.993753300",
        "description": "Only 1 addresses added out of total 2 resolved",
        "file": "../deps/grpc/src/core/ext/transport/chttp2/server/chttp2_server.cc",
        "file_line": 403,
        "referenced_errors": [{
            "created": "@1616799250.993738500",
            "description": "Failed to add port to server",
            "file": "../deps/grpc/src/core/lib/iomgr/tcp_server_custom.cc",
            "file_line": 403,
            "referenced_errors": [{
                "created": "@1616799250.993724200",
                "description": "Failed to bind to port",
                "file": "../deps/grpc/src/core/lib/iomgr/tcp_uv.cc",
                "file_line": 72,
                "grpc_status": 14,
                "os_error": "address not available"
            }]
        }]
    }

The error does not occur on my local machine. Only on Docker.

Some useful info:

  • My bootstrap URL: localhost:5000
  • Docker image: node:14-alpine
  • OS: macOS 11.2 Bug Sur
  • Dockerfile:

    FROM node:14-alpine
    
    WORKDIR /app
    
    RUN apk -U --no-cache add protobuf \
      protobuf-dev \
      git \
      gcc \
      g++ \
      make \
      python
    
    COPY . .
    
    RUN git clone https://github.com/googleapis/googleapis.git /googleapis
    
    RUN npm install -g @nestjs/cli
    
    RUN npm install && sh compile.proto.sh
    
    RUN npm rebuild grpc --force
    
    CMD ["npm", "start"]

  • docker-compose.yml:
    version: "3.7"
    
    services:
      health:
        build:
          context: .
          dockerfile: Docker/Dockerfile
        ports:
          - 5000:5000
-

  • protoc script:
    protoc \
      --include_imports \
      --include_source_info \
      --proto_path=/googleapis \
      --proto_path=/googleapis/google/api \
      --proto_path=. \
      --descriptor_set_out=api_descriptor.pb \
      --plugin=./node_modules/.bin/protoc-gen-ts_proto \
      --ts_proto_out=./src/health src/health/health.proto \
      --ts_proto_opt=outputEncodeMethods=false,outputJsonMethods=false,outputClientImpl=false

  • protobuf for this example:
    syntax = "proto3";
    
    package health;
    
    service HealthService {
        rpc GetStatus(GetStatusRequest) returns (Status);
    }
    
    message Status {
        bool alive = 1;
    }
    
    message GetStatusRequest {}

  • Application bootstrap:

    const logger = new Logger('Main');
    const url = 'localhost:5000';
    
    async function bootstrap() {
      const app = await NestFactory.createMicroservice<MicroserviceOptions>(
        AppModule,
        {
          transport: Transport.GRPC,
          options: {
            package: 'health',
            protoPath: join(__dirname, 'health/health.proto'),
          },
        },
      );
      await app.listen(() =>
        logger.log('Nest Service Template Listening on ' + url),
      );
    }
    bootstrap();

  • nest start/npm start script output: nest start

Does anyone know how to troubleshoot this error?

like image 640
Guilherme Morais Avatar asked Dec 30 '25 08:12

Guilherme Morais


1 Answers

I've found the error (pretty dumb, by the way). I had to add the url parameter to the grpcClientOptions with 0.0.0.0:5000 (as the container does not recognize localhost without an alias).

This led me to another error: the gRPC package used by NestJS is built to the host machine when npm install is executed (in my case, to macOS), so it does not work inside the Linux container.

A possible solution is copying the whole project to the container and executing npm install inside the container -- which I think is not the best way to use Docker for local development.

Another one would be

$ rm -rf node_modules
$ npm install --target=12.0.0 --target_platform=linux --target_arch=x64 --target_libc=musl

Then you will not be able to run npm start locally, but it should work inside the container.

This issue will probably be fixed in the next major release of NestJS, since there is a pull request updating the gRPC package: https://github.com/nestjs/nest/pull/6349.

like image 66
Guilherme Morais Avatar answered Jan 01 '26 00:01

Guilherme Morais



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!