Pydgraph Client connection error : UNKNOWN - failed to connect to all addresses

Client connection error : UNKNOWN:failed to connect to all addresses: ipv4:127.0.0.1:9080: Failed to connect to remote host: Connection refused


Report a Dgraph Client Bug

What Dgraph client (and version) are you using?

(put “x” in the box to select)

  • Dgo
  • PyDgraph
  • Dgraph4J
  • Dgraph-js
  • Dgraph-js-http
  • Dgraph.NET

Version:
Pydgraph==21.3.2

What version of Dgraph are you using?

Latest docker image

Have you tried reproducing the issue with the latest release?

Yes

What is the hardware spec (RAM, OS)?

Apple M1 (Mac OS Monterey)
Apple Intel (Mac OS Catalina)

Steps to reproduce the issue (command/config used to run Dgraph).

  1. Run the docker-compose to bring up FastAPI and Dgraph(alpha, zero)
  2. Try to create connection client using pydgraph

We’re using docker-compose to spin up FastAPI with Dgraph DB and running into connection issues when creating the client. We also tried to whitelist all IPs for alpha instance.

Docker-compose file

version: '3.8'

services:
  web:
    build: .
    command: uvicorn webapp.app.main:app --host 0.0.0.0
    depends_on:
      - zero
      - alpha
    volumes:
      - .:/app
    ports:
      - 8000:8000

  zero:
    image: dgraph/dgraph:latest
    container_name: dgraph-zero
    volumes:
      - dgraph:/dgraph
    ports:
      - 5080:5080
      - 6080:6080
    restart: on-failure
    command: dgraph zero --my=zero:5080

  alpha:
    image: dgraph/dgraph:latest
    container_name: dgraph-alpha
    volumes:
      - dgraph:/dgraph
    ports:
      - 8080:8080
      - 9080:9080
    restart: on-failure
    command: dgraph alpha --my=alpha:7080  --security "whitelist=0.0.0.0/0" --zero=zero:5080

volumes:
  dgraph:


Expected behaviour and actual result.

We should be able to create a connection client which can be used to run mutations/queries

But instead we get the following error

This is the error trace

dgraph-assessment-poc-web-1  |   File "/webapp/app/./webapp/app/main.py", line 60, in query1_func
dgraph-assessment-poc-web-1  |     result = dgraph_object.client.txn(read_only=True).query(query, variables=variables)
dgraph-assessment-poc-web-1  |   File "/usr/local/lib/python3.9/site-packages/pydgraph/txn.py", line 62, in query
dgraph-assessment-poc-web-1  |     return self.do_request(req, timeout=timeout, metadata=metadata, credentials=credentials)
dgraph-assessment-poc-web-1  |   File "/usr/local/lib/python3.9/site-packages/pydgraph/txn.py", line 126, in do_request
dgraph-assessment-poc-web-1  |     self._common_except_mutate(query_error)
dgraph-assessment-poc-web-1  |   File "/usr/local/lib/python3.9/site-packages/pydgraph/txn.py", line 229, in _common_except_mutate
dgraph-assessment-poc-web-1  |     raise error
dgraph-assessment-poc-web-1  |   File "/usr/local/lib/python3.9/site-packages/pydgraph/txn.py", line 102, in do_request
dgraph-assessment-poc-web-1  |     response = self._dc.query(request, timeout=timeout,
dgraph-assessment-poc-web-1  |   File "/usr/local/lib/python3.9/site-packages/pydgraph/client_stub.py", line 60, in query
dgraph-assessment-poc-web-1  |     return self.stub.Query(req, timeout=timeout, metadata=metadata,
dgraph-assessment-poc-web-1  |   File "/usr/local/lib/python3.9/site-packages/grpc/_channel.py", line 946, in __call__
dgraph-assessment-poc-web-1  |     return _end_unary_response_blocking(state, call, False, None)
dgraph-assessment-poc-web-1  |   File "/usr/local/lib/python3.9/site-packages/grpc/_channel.py", line 849, in _end_unary_response_blocking
dgraph-assessment-poc-web-1  |     raise _InactiveRpcError(state)
dgraph-assessment-poc-web-1  | grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
dgraph-assessment-poc-web-1  |  status = StatusCode.UNAVAILABLE
dgraph-assessment-poc-web-1  |  details = "failed to connect to all addresses; last error: UNKNOWN: ipv4:127.0.0.1:9080: Failed to connect to remote host: Connection refused"
dgraph-assessment-poc-web-1  |  **debug_error_string = "UNKNOWN:failed to connect to all addresses; last error: UNKNOWN: ipv4:127.0.0.1:9080: Failed to connect to remote host: Connection refused {created_time:"2023-01-10T21:16:37.786622068+00:00", grpc_status:14}**"

What works

We’d like to also note that we can create client connection sucessfully if we run the standalone cluster using following command

docker run --rm -it -p "8080:8080" -p "9080:9080" -v ~/dgraph:/dgraph "dgraph/standalone:v22.0.2"

and then spinning up FastAPI using

uvicorn webapp.app.main:app --reload --host 0.0.0.0 --port 8000                                  

Let us know if you’d like any other information from our end to fix our docker-compose configuration file. Thank you.

Is host something from your application or Dgraph? if dgraph, in context of Docker you should use alpha:8080 for HTTP Raw or alpha:9080 in case of gRPC.

BTW. Dgraph would only run at localhost / 127.0.0.1 if you use bridge mode in Docker.

Actually we’re trying to asses both HTTP and gRPC functionalities in dgraph for our use case.

We’re running FASTApi application on 0.0.0.0:8000. This application first tries to connect with Dgraph database by creating a client to localhost:9080

Then this client queries Dgraph database using the following method from the Pydgraph library.

client.txn(read_only=True).query(query, variables=variables)

As I said, there’s no way to connect to Dgraph via Localhost from inside a container. Unless your application run in the Host itself, it will never work. Use alpha:8080 or alpha:9080 instead.

The error says

UNKNOWN: ipv4:127.0.0.1:9080: Failed to connect to remote 
host: Connection refused

So, the connection is refused cuz there’s nothing there in 127.0.0.1. As your application is in a container and in the same network of Dgraph’s container you should use the container name as URL. That’s created in Docker’s DNS resolution.

Cheers.

That makes sense, Michel. Replacing it with alpha:9080 worked. Thank you for the prompt response!

1 Like