py2neo连接拒绝,而docker组成

我想运行一个Django的应用程序,它使用py2neo库连接到neo4j数据库。 它在我的本地机器上正常运行。 但是当我试图使用docker-compose得到它时,我一次又一次地出现了错误。

这是我的docker-compose.yml文件

version: '2' services: db: image: postgres neo4j: image: neo4j ports: - "7474:7474" - "7687:7687" volumes: - ./db/dbms:/data/dbms web: build: . command: python manage.py runserver 0.0.0.0:8000 volumes: - .:/code ports: - "8000:8000" depends_on: - db - neo4j links: - neo4j 

Dockerfile:

 FROM python:3 FROM neo4j:3.1 ENV PYTHONUNBUFFERED 1 RUN mkdir /code WORKDIR /code ADD requirements.txt /code/ RUN pip install -r requirements.txt ADD . /code/ ADD startneo.sh /startneo.sh CMD ["/start.sh"] 

views.py

 import os import json # Create your views here. from py2neo import Graph, authenticate from bottle import get,run,request,response,static_file graph = Graph(password='neo4j') @get("/") def get_index(): return static_file("index.html", root="static") @get("/graph") def get_graph(self): print("i was here" ) print("graph start") results = graph.run( "MATCH (m:Movie)<-[:ACTED_IN]-(a:Person) " "RETURN m.title as movie, collect(a.name) as cast " "LIMIT {limit}", {"limit": 10}) print("graph run the run") nodes = [] rels = [] i = 0 for movie, cast in results: #print("i am here") nodes.append({"title": movie, "label": "movie"}) target = i i += 1 for name in cast: print(name) actor = {"title": name, "label": "actor"} try: source = nodes.index(actor) except ValueError: nodes.append(actor) source = i i += 1 rels.append({"source": source, "target": target}) return {"nodes": nodes, "links": rels} @get("/search") def get_search(): try: q = request.query["q"] except KeyError: return [] else: results = graph.run( "MATCH (movie:Movie) " "WHERE movie.title =~ {title} " "RETURN movie", {"title": "(?i).*" + q + ".*"}) response.content_type = "application/json" return json.dumps([{"movie": dict(row["movie"])} for row in results]) @get("/movie/<title>") def get_movie(title): results = graph.run( "MATCH (movie:Movie {title:{title}}) " "OPTIONAL MATCH (movie)<-[r]-(person:Person) " "RETURN movie.title as title," "collect([person.name, head(split(lower(type(r)),'_')), r.roles]) as cast " "LIMIT 1", {"title": title}) row = results.next() return {"title": row["title"], "cast": [dict(zip(("name", "job", "role"), member)) for member in row["cast"]]} 

我无法把networking和运行

 docker-compose up 

总是抛出以下错误

 web_1 | Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f0f68b25158> web_1 | Traceback (most recent call last): web_1 | File "/usr/local/lib/python3.6/site-packages/py2neo/database/__init__.py", line 300, in __new__ web_1 | inst = cls.__instances[key] web_1 | KeyError: (<class 'py2neo.database.Graph'>, <ServerAddress settings={'host': 'localhost', 'http_port': 7474}>, 'data') web_1 | web_1 | During handling of the above exception, another exception occurred: web_1 | web_1 | Traceback (most recent call last): web_1 | File "/usr/local/lib/python3.6/site-packages/py2neo/packages/httpstream/http.py", line 322, in submit web_1 | response = send() web_1 | File "/usr/local/lib/python3.6/site-packages/py2neo/packages/httpstream/http.py", line 317, in send web_1 | http.request(xstr(method), xstr(uri.absolute_path_reference), body, headers) web_1 | File "/usr/local/lib/python3.6/http/client.py", line 1239, in request web_1 | self._send_request(method, url, body, headers, encode_chunked) web_1 | File "/usr/local/lib/python3.6/http/client.py", line 1285, in _send_request web_1 | self.endheaders(body, encode_chunked=encode_chunked) web_1 | File "/usr/local/lib/python3.6/http/client.py", line 1234, in endheaders web_1 | self._send_output(message_body, encode_chunked=encode_chunked) web_1 | File "/usr/local/lib/python3.6/http/client.py", line 1026, in _send_output web_1 | self.send(msg) web_1 | File "/usr/local/lib/python3.6/http/client.py", line 964, in send web_1 | self.connect() web_1 | File "/usr/local/lib/python3.6/site-packages/py2neo/packages/httpstream/http.py", line 80, in connect web_1 | self.source_address) web_1 | File "/usr/local/lib/python3.6/socket.py", line 722, in create_connection web_1 | raise err web_1 | File "/usr/local/lib/python3.6/socket.py", line 713, in create_connection web_1 | sock.connect(sa) web_1 | ConnectionRefusedError: [Errno 111] Connection refused web_1 | web_1 | During handling of the above exception, another exception occurred: web_1 | web_1 | Traceback (most recent call last): web_1 | File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 227, in wrapper web_1 | fn(*args, **kwargs) web_1 | File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run web_1 | self.check(display_num_errors=True) web_1 | File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 359, in check web_1 | include_deployment_checks=include_deployment_checks, web_1 | File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 346, in _run_checks web_1 | return checks.run_checks(**kwargs) web_1 | File "/usr/local/lib/python3.6/site-packages/django/core/checks/registry.py", line 81, in run_checks web_1 | new_errors = check(app_configs=app_configs) web_1 | File "/usr/local/lib/python3.6/site-packages/django/core/checks/urls.py", line 16, in check_url_config web_1 | return check_resolver(resolver) web_1 | File "/usr/local/lib/python3.6/site-packages/django/core/checks/urls.py", line 26, in check_resolver web_1 | return check_method() web_1 | File "/usr/local/lib/python3.6/site-packages/django/urls/resolvers.py", line 254, in check web_1 | for pattern in self.url_patterns: web_1 | File "/usr/local/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__ web_1 | res = instance.__dict__[self.name] = self.func(instance) web_1 | File "/usr/local/lib/python3.6/site-packages/django/urls/resolvers.py", line 405, in url_patterns web_1 | patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) web_1 | File "/usr/local/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__ web_1 | res = instance.__dict__[self.name] = self.func(instance) web_1 | File "/usr/local/lib/python3.6/site-packages/django/urls/resolvers.py", line 398, in urlconf_module web_1 | return import_module(self.urlconf_name) web_1 | File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module web_1 | return _bootstrap._gcd_import(name[level:], package, level) web_1 | File "<frozen importlib._bootstrap>", line 978, in _gcd_import web_1 | File "<frozen importlib._bootstrap>", line 961, in _find_and_load web_1 | File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked web_1 | File "<frozen importlib._bootstrap>", line 655, in _load_unlocked web_1 | File "<frozen importlib._bootstrap_external>", line 678, in exec_module web_1 | File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed web_1 | File "/code/cnews/urls.py", line 22, in <module> web_1 | url(r'^movies/',include('movie.urls')), web_1 | File "/usr/local/lib/python3.6/site-packages/django/conf/urls/__init__.py", line 50, in include web_1 | urlconf_module = import_module(urlconf_module) web_1 | File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module web_1 | return _bootstrap._gcd_import(name[level:], package, level) web_1 | File "<frozen importlib._bootstrap>", line 978, in _gcd_import web_1 | File "<frozen importlib._bootstrap>", line 961, in _find_and_load web_1 | File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked web_1 | File "<frozen importlib._bootstrap>", line 655, in _load_unlocked web_1 | File "<frozen importlib._bootstrap_external>", line 678, in exec_module web_1 | File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed web_1 | File "/code/movie/urls.py", line 2, in <module> web_1 | from . import views web_1 | File "/code/movie/views.py", line 18, in <module> web_1 | graph = Graph('http://localhost:7474/db/data/') web_1 | File "/usr/local/lib/python3.6/site-packages/py2neo/database/__init__.py", line 309, in __new__ web_1 | use_bolt = version_tuple(inst.__remote__.get().content["neo4j_version"]) >= (3,) web_1 | File "/usr/local/lib/python3.6/site-packages/py2neo/database/http.py", line 154, in get web_1 | response = self.__base.get(headers=headers, redirect_limit=redirect_limit, **kwargs) web_1 | File "/usr/local/lib/python3.6/site-packages/py2neo/packages/httpstream/http.py", line 966, in get web_1 | return self.__get_or_head("GET", if_modified_since, headers, redirect_limit, **kwargs) web_1 | File "/usr/local/lib/python3.6/site-packages/py2neo/packages/httpstream/http.py", line 943, in __get_or_head web_1 | return rq.submit(redirect_limit=redirect_limit, **kwargs) web_1 | File "/usr/local/lib/python3.6/site-packages/py2neo/packages/httpstream/http.py", line 433, in submit web_1 | http, rs = submit(self.method, uri, self.body, self.headers) web_1 | File "/usr/local/lib/python3.6/site-packages/py2neo/packages/httpstream/http.py", line 362, in submit web_1 | raise SocketError(code, description, host_port=uri.host_port) web_1 | py2neo.packages.httpstream.http.SocketError: Connection refused 

我不知道如何解决这个问题。