数据库
首页 > 数据库> > python – 无法连接到Docker容器上的neo4j数据库

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

作者:互联网

我在一台主机上使用两个docker容器.第一个是基于普通的neo4j:2.3图像和一些转发端口(7474).第二个是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预约.

泊坞窗,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,py2neo,neomodel
来源: https://codeday.me/bug/20190628/1311487.html