0

I have a neo4j database https://localhost:7474 (https://localhost:7474) which I connect via py2neo's Graph Object ("bolt://127.0.0.1:7687"). All works if not in Docker Environment. Now What i wanted to do is connect to my app (built with fastpi:https://fastapi.tiangolo.com/deployment/docker/).

My question is how do I connect to locally hosted (in my mac) neo4j db via docker image (fastapi) image hosted in docker.

However I get following error:

 docker run e7bf302e02ec


Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/py2neo/client/__init__.py", line 742, in acquire
    cx = self._free_list.popleft()
IndexError: pop from an empty deque

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/py2neo/wiring.py", line 162, in open
    s.connect(address)
ConnectionRefusedError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/py2neo/client/bolt.py", line 212, in open
    wire = cls._connect(profile, on_broken=on_broken)
  File "/usr/local/lib/python3.8/site-packages/py2neo/client/bolt.py", line 228, in _connect
    wire = Wire.open(profile.address, keep_alive=True, on_broken=on_broken)
  File "/usr/local/lib/python3.8/site-packages/py2neo/wiring.py", line 164, in open
    raise_from(WireError("Cannot connect to %r" % (address,)), error)
  File "<string>", line 3, in raise_from
py2neo.wiring.WireError: Cannot connect to IPv4Address(('127.0.0.1', 7687))

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/bin/uvicorn", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/uvicorn/main.py", line 362, in main
    run(**kwargs)
  File "/usr/local/lib/python3.8/site-packages/uvicorn/main.py", line 386, in run
    server.run()
  File "/usr/local/lib/python3.8/site-packages/uvicorn/server.py", line 49, in run
    loop.run_until_complete(self.serve(sockets=sockets))
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/usr/local/lib/python3.8/site-packages/uvicorn/server.py", line 56, in serve
    config.load()
  File "/usr/local/lib/python3.8/site-packages/uvicorn/config.py", line 308, in load
    self.loaded_app = import_from_string(self.app)
  File "/usr/local/lib/python3.8/site-packages/uvicorn/importer.py", line 20, in import_from_string
    module = importlib.import_module(module_str)
  File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "./app/main.py", line 8, in <module>
    from .database import Base
  File "./app/database.py", line 28, in <module>
    Base = Graph(DATABASE_URL, auth=(DATABASE_USERNAME, DATABASE_PASSWORD))
  File "/usr/local/lib/python3.8/site-packages/py2neo/database/__init__.py", line 358, in __init__
    self.service = GraphService(profile, **settings)
  File "/usr/local/lib/python3.8/site-packages/py2neo/database/__init__.py", line 189, in __init__
    self._connector = Connector(profile, **connector_settings)
  File "/usr/local/lib/python3.8/site-packages/py2neo/client/__init__.py", line 893, in __init__
    self.add_pools(self._profile)
  File "/usr/local/lib/python3.8/site-packages/py2neo/client/__init__.py", line 913, in add_pools
    pool = ConnectionPool.open(
  File "/usr/local/lib/python3.8/site-packages/py2neo/client/__init__.py", line 570, in open
    seeds = [pool.acquire() for _ in range(init_size or cls.default_init_size)]
  File "/usr/local/lib/python3.8/site-packages/py2neo/client/__init__.py", line 570, in <listcomp>
    seeds = [pool.acquire() for _ in range(init_size or cls.default_init_size)]
  File "/usr/local/lib/python3.8/site-packages/py2neo/client/__init__.py", line 749, in acquire
    cx = self.connect()
  File "/usr/local/lib/python3.8/site-packages/py2neo/client/__init__.py", line 706, in connect
    cx = Connection.open(self.profile, user_agent=self.user_agent,
  File "/usr/local/lib/python3.8/site-packages/py2neo/client/__init__.py", line 141, in open
    return Bolt.open(profile, user_agent=user_agent,
  File "/usr/local/lib/python3.8/site-packages/py2neo/client/bolt.py", line 223, in open
    raise_from(ConnectionUnavailable("Cannot open connection to %r" % profile), error)
  File "<string>", line 3, in raise_from
py2neo.client.ConnectionUnavailable: Cannot open connection to ConnectionProfile('bolt://neo4j@127.0.0.1:7687')
lpt
  • 931
  • 16
  • 35
  • Localhost and 127.0.0.1 refers to your host, or to your container if it is in a container. How do you launch the container(s)? Do you link ports ? – Faeeria Mar 18 '21 at 21:38
  • I issued docker run -d --name mycontainer -p 80:80 myimage – lpt Mar 18 '21 at 22:02
  • Does your app use the port 80 ? Of the port 7474? – Faeeria Mar 18 '21 at 22:07
  • i have tried with 80:7474, 7474:7474, 80:7687, 7687:7687. Cannot connect. Any recommendations? – lpt Mar 18 '21 at 22:20
  • Can you clarify, in you question, which service is launched from where and how it is launched ? Like, what service listenning on what port is on the host, etc. – Faeeria Mar 18 '21 at 22:23
  • sure, what I am doing is - I am testing with a fastapi app (mimics Swagger). When I run this locally, (meanign with no docker involvement), I can run webservice without issue. I can get all mehods suchs as get, post, put, delete. The database which is hosted locally (running in my mac) works fine. Now What I want to do is create a docker image of the app (bunch of python files) w/o database in docker. I want to connect the app to the db which I hosting locally (my mac) and do some testing. – lpt Mar 18 '21 at 22:27
  • Okay. Then you should use your host IP adress instead of localhost in your URL – Faeeria Mar 18 '21 at 22:31
  • oh, you meant something like generated from ifconfig ? (en, eg 19X.1XX.1.5). and this goes where? (sorry, trying to understand how to use docker). – lpt Mar 18 '21 at 22:35
  • Yes. On your host, get the IP adress. Then, in your container, whenever you call the database, you use this adress – Faeeria Mar 18 '21 at 23:22

1 Answers1

0

This seems to be a problem of docker and how it's built.

Basically, if you try to access localhost from a container, you'll access the container's localhost, not the one of your computer!

The idea is to get the ip address of the docker container of your network and use that one instead.

Here are some links that show how to do it, based on the platform you're on (I'm considering also other users may find this answer and rely on it)

localhost refuses connection with docker

How to access "http://localhost" from a Docker container in Windows?

lsabi
  • 3,641
  • 1
  • 14
  • 26
  • The problem with those answers is that it is hard to automate this process. It is better to leverage the built-in possibilities of docker, IMHO, like the embed dns or the port mapping. – Faeeria Mar 18 '21 at 22:08
  • That's true, but by experience, you set up on a server the tests and on rare occasions need to test everything on your local machine these days. Still, a small script can be used to detect the IP address and configure the docker container. Anyways, feel free to improve my answer by providing links to tutorials that support automating the process – lsabi Mar 18 '21 at 22:14