7

我在一台主机上使用两个 docker 容器。第一个是基于带有一些转发端口(7474)的普通 neo4j:2.3 映像。第二个是基于python:2.7的容器。使用 curl 我可以从主机级别和第二台机器访问 db(使用主机本身的 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 时,它​​可以工作

4

3 回答 3

6

您不应该将 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()
于 2016-10-11T17:59:36.543 回答
1

您应该首先在后台启动 neo4j 容器。利用

$ sudo docker-compose up neo4j

然后启动您的应用程序

$ sudo docker-compose up your_app

您可以通过终端访问数据库。编写以下命令:

$ sudo docker exec -it neo4j bash

然后使用以下命令启动 neo4j shell:

# cypher-shell -u neo4j -p test

您现在可以将您的数据库与 neo4j shell 一起用于所有查询。不要忘记在每个查询的末尾输入分号:

> match(n) return(n);

于 2019-05-21T10:31:38.350 回答
0

确保您安装了最新的 Py2neo 驱动程序,并且您必须延迟基本映像以允许 Neo4j 数据库启动。

于 2017-04-07T03:34:06.550 回答