Jack Daniel
Jack Daniel

Reputation: 2611

Neo4j Docker Connection failure

I am facing the neo4j connection issue while trying the run the neo4j code; the same working fine outside docker but failing to establish the connection with neo4j database while running inside docker.

Error message:

    Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/neobolt/direct.py", line 835, in _connect
    s.connect(resolved_address)
OSError: [Errno 99] Cannot assign requested address

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    driver = GraphDatabase.driver(uri, auth=("neo4j", "admin"))
  File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py", line 116, in driver
    return Driver(uri, **config)
  File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py", line 157, in __new__
    return subclass(uri, **config)
  File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py", line 231, in __new__
    pool.release(pool.acquire())
  File "/usr/local/lib/python3.7/site-packages/neobolt/direct.py", line 719, in acquire
    return self.acquire_direct(self.address)
  File "/usr/local/lib/python3.7/site-packages/neobolt/direct.py", line 612, in acquire_direct
    connection = self.connector(address, error_handler=self.connection_error_handler)
  File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py", line 228, in connector
    return connect(address, **dict(config, **kwargs))
  File "/usr/local/lib/python3.7/site-packages/neobolt/direct.py", line 976, in connect
    raise last_error
  File "/usr/local/lib/python3.7/site-packages/neobolt/direct.py", line 966, in connect
    s = _connect(resolved_address, **config)
  File "/usr/local/lib/python3.7/site-packages/neobolt/direct.py", line 847, in _connect
    raise ServiceUnavailable("Failed to establish connection to {!r} (reason {})".format(resolved_address, error))
neobolt.exceptions.ServiceUnavailable: Failed to establish connection to ('::1', 11010, 0, 0) (reason [Errno 99] Cannot assign requested address)

The code I used is as follows:

from neo4j import GraphDatabase
uri = "bolt://localhost:11010/"
driver = GraphDatabase.driver(uri, auth=("neo4j", "admin"))
with driver.session() as session:
    result = session.run("MATCH (n) RETURN count(n)")
    print(result)
    session.close()

My bolt port is 11010 as in code. Am I missing anything here? I tried the below things but no luck
1) used 127.0.0.1 instead of localhost
2) closed the sessions

And my docker file is as follows:

FROM python:3
ADD test.py /
RUN pip install pandas
RUN pip install pymysql
RUN pip install sqlalchemy
RUN pip install neo4j
CMD ["python","test.py"]

Upvotes: 4

Views: 1823

Answers (2)

CleverChuk
CleverChuk

Reputation: 141

if you're running python inside a container use:

    GraphDatabase.driver(host="<contaniner>", auth=basic_auth("neo4j", "myPass"))

Upvotes: 2

Adiii
Adiii

Reputation: 60074

From the comment, I assume the neo4j Database is running in other containers.

You can not connect from one container to another container using URL localhost.

Localhost always refers to the localhost of the current container not the other container call it B.

So possible alternate and solution

  • IF both are running in two different container use Docker-compose and you will able to access new4j database from container name like neo4j:port thats it.
  • If new4j running on the host, then you need to access new4j using the HOST ip inside your python container.
  • If you you do not want to use docker-compose, you can use lagacy linking
 docker run -d -P --name web --link newdb:db training/webapp python app.py

Now you refer it from inside python container like db as a host.

Upvotes: 2

Related Questions