gpt4 book ai didi

python - 无法连接到 Docker 容器上的 neo4j 数据库

转载 作者:太空宇宙 更新时间:2023-11-03 10:58:27 26 4
gpt4 key购买 nike

我在一台主机上使用两个 docker 容器。第一个基于带有一些转发端口 (7474) 的普通 neo4j:2.3 图像。第二个是基于python:2.7的容器。使用 curl 我可以从主机级别和第二台机器访问数据库(使用主机本身的 IP)。问题是我编写并移植到那个容器的那段代码将不再运行。我收到这样的错误:

Traceback (most recent call last):
File "/app/runserver.py", line 1, in <module>
from orangebox import app
File "/app/orangebox/__init__.py", line 5, in <module>
from orangebox.context import ob
File "/app/orangebox/context.py", line 2, in <module>
from orangebox.domain.factory import DomainFactory
File "/app/orangebox/domain/factory.py", line 4, in <module>
from orangebox.domain.boxes import Box
File "/app/orangebox/domain/boxes.py", line 3, in <module>
from orangebox.domain.users import User
File "/app/orangebox/domain/users.py", line 4, in <module>
class User(StructuredNode):
File "/app/src/neomodel/neomodel/core.py", line 65, in __new__
install_labels(inst)
File "/app/src/neomodel/neomodel/core.py", line 15, in install_labels
db.new_session()
File "/app/src/neomodel/neomodel/util.py", line 119, in new_session
if self.session.neo4j_version < (2, 0):
File "/usr/local/lib/python2.7/site-packages/py2neo/core.py", line 958, in neo4j_version
return version_tuple(self.resource.metadata["neo4j_version"])
File "/usr/local/lib/python2.7/site-packages/py2neo/core.py", line 213, in metadata
self.get()
File "/usr/local/lib/python2.7/site-packages/py2neo/core.py", line 258, in get
response = self.__base.get(headers=headers, redirect_limit=redirect_limit, **kwargs)
File "/usr/local/lib/python2.7/site-packages/py2neo/packages/httpstream/http.py", line 966, in get
return self.__get_or_head("GET", if_modified_since, headers, redirect_limit, **kwargs)
File "/usr/local/lib/python2.7/site-packages/py2neo/packages/httpstream/http.py", line 943, in __get_or_head
return rq.submit(redirect_limit=redirect_limit, **kwargs)
File "/usr/local/lib/python2.7/site-packages/py2neo/packages/httpstream/http.py", line 433, in submit
http, rs = submit(self.method, uri, self.body, self.headers)
File "/usr/local/lib/python2.7/site-packages/py2neo/packages/httpstream/http.py", line 362, in submit
raise SocketError(code, description, host_port=uri.host_port)
py2neo.packages.httpstream.http.SocketError: Connection refused

我也尝试过在端口 7473 上使用 https,但它也不起作用。从调用方开始,我使用以下调用:

from py2neo import authenticate, Graph
from py2neo.ext.ogm import Store
import os

class DatabaseConnection(object):

def __init__(self, address):
print "Connecting to Neo4j database using URL %s" % os.environ['DB_URL']
self.graph = Graph(address)
self.store = Store(self.graph)

def get_graph(self):
return self.graph

def get_cypher(self):
return self.graph.cypher

def get_store(self):
return self.store

authenticate("172.17.0.1:7474", "neo4j", "orangebox")
db_connection = DatabaseConnection(address = "http://172.17.0.1:7474/db/data")

用户名和密码正确。如果不是,我将获得未经授权(因此可以连接...)

Connecting to Neo4j database using URL http://172.17.0.1:7474/db/data/
Traceback (most recent call last):
File "/app/runserver.py", line 1, in <module>
from orangebox import app
File "/app/orangebox/__init__.py", line 5, in <module>
from orangebox.context import ob
File "/app/orangebox/context.py", line 2, in <module>
from orangebox.domain.factory import DomainFactory
File "/app/orangebox/domain/factory.py", line 3, in <module>
from orangebox.database.connection import db_connection
File "/app/orangebox/database/connection.py", line 22, in <module>
db_connection = DatabaseConnection(address = os.environ['DB_URL'])
File "/app/orangebox/database/connection.py", line 10, in __init__
self.store = Store(self.graph)
File "/usr/local/lib/python2.7/site-packages/py2neo/ext/ogm/store.py", line 42, in __init__
if self.graph.supports_optional_match:
File "/usr/local/lib/python2.7/site-packages/py2neo/core.py", line 1087, in supports_optional_match
return self.neo4j_version >= (2, 0)
File "/usr/local/lib/python2.7/site-packages/py2neo/core.py", line 958, in neo4j_version
return version_tuple(self.resource.metadata["neo4j_version"])
File "/usr/local/lib/python2.7/site-packages/py2neo/core.py", line 213, in metadata
self.get()
File "/usr/local/lib/python2.7/site-packages/py2neo/core.py", line 261, in get
raise Unauthorized(self.uri.string)
py2neo.error.Unauthorized: http://172.17.0.1:7474/db/data/

在 docker neo4j 容器上我只看到:

root@ubuntu-14:/home/vagrant# docker run --publish=7474:7474 --volume=$HOME/neo4j/data:/data kbhit/orangebox-db
Starting Neo4j Server console-mode...
2016-05-13 14:01:35.076+0000 INFO Successfully started database
2016-05-13 14:01:35.112+0000 INFO Starting HTTP on port 7474 (2 threads available)
2016-05-13 14:01:35.327+0000 INFO Enabling HTTPS on port 7473
2016-05-13 14:01:35.417+0000 INFO Mounting static content at /webadmin
2016-05-13 14:01:35.485+0000 INFO Mounting static content at /browser
2016-05-13 14:01:36.718+0000 INFO Remote interface ready and available at http://0.0.0.0:7474/
2016-05-13 14:02:17.825+0000 WARN Failed authentication attempt for 'neo4j' from 172.17.0.1

这怎么可能?

当我使用指南 https://www.digitalocean.com/community/tutorials/how-to-install-neo4j-on-an-ubuntu-vps 在同一个容器中安装 neo4j-service 时它有效

最佳答案

您不应将 neo4j-service 安装在与 python 应用程序相同的容器中。您应该使用 docker 链接容器。我认为您真的不应该使用固定 IP 进行 URL 预约。

docker-compose.yml

version: '2'

services:
data:
build: data

neo4j:
image: neo4j:3.0
networks:
- back
volumes_from:
- data
ports:
- "7474:7474"
- "7687:7687"
volumes:
- /var/neo4j/data:/data

neo4jtest:
image: neo4j:3.0
networks:
- back
volumes_from:
- data
ports:
- "7475:7474"
- "7688:7687"
volumes:
- /var/neo4j/test-data:/data

api:
build: api
networks:
- back
ports:
- "5002:5002"
volumes:
- /home/santiago/work/santiagocloud/api:/src/api
links:
- neo4j:neo4j
- neo4jtest:neo4jtest

networks:
back:

Python代码:

def connectGraph():
print("Start - Connect Graph")
# authenticate(""http://neo4j:7474", "neo4j", "test123")
# graph = Graph("http://neo4j:7474/db/data/")


authenticate(app.config['DB_URL'],app.config['DB_USERNAME'],app.config['DB_PASSWORD'])
graph = Graph(app.config['DB_URL'] + "/db/data/")

print("End - Connect Graph")
return graph
graph = connectGraph()

关于python - 无法连接到 Docker 容器上的 neo4j 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37211875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com