无法将BOLT连接到docker Neo4j

时间:2017-01-20 13:52:30

标签: python docker neo4j

我运行Neo4j的docker实例:

docker run -d \
--publish=7474:7474 \
--publish=7687:7687 \
--volume=/home/jeff/tmp/neo4j:/data neo4j

与#34; docker一起检查的IP"是:172.17.0.2。我可以将Neo4j连接到浏览器,并更改密码。

然后,在Docker中启动一个bash实例:

docker run -it ubuntu:16.04 bash
apt-get update
apt-get install python3
apt-get install python3-pip
pip3 install neo4j-driver
apt-get install nano
nano test.py

python脚本" test.py"是:

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

from neo4j.v1 import GraphDatabase, basic_auth

session = GraphDatabase.driver(
    'bolt://172.17.0.2',
    auth=basic_auth('neo4j', '******')
    ).session()

session.run('MATCH (n) RETURN n')

然后:

chmod +x test.py
./test.py

我明白了:

Traceback (most recent call last):
File "./test.py", line 11, in <module>
session.run('MATCH (n) RETURN n')
File "/usr/local/lib/python3.5/dist-packages/neo4j/v1/bolt.py", line 145, in run
self.connection.send()
File "/usr/local/lib/python3.5/dist-packages/neo4j/bolt/connection.py", line 344, in send
raise ServiceUnavailable("Failed to write to closed connection %r" % (self.server.address,))
neo4j.bolt.connection.ServiceUnavailable: Failed to write to closed connection Address(host='172.17.0.2', port=7687)

在容器之外,我的意思是在主机上,test.py脚本正常工作。有什么想法吗?

- 编辑 -

在容器内,&#39; ip a&#39;给出:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
18: eth0@if19: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.4/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:4/64 scope link 
       valid_lft forever preferred_lft forever

1 个答案:

答案 0 :(得分:2)

容器内的IP&#34; 172.17.0.4&#34;,与您在脚本中指定的IP不匹配,&#34; 172.17.0.2&#34;。我怀疑Python试图绑定到后者,失败,并返回一个封闭的套接字。然后当Neo4j去发送时,它在一个封闭的插座上出错了。搜索&#34;未能写&#34;在this page

尝试将脚本设置为使用其中一个作为地址:

  • &#34; 172.17.0.4&#34; - 根据&#39; ip a&#39;
  • 的结果绑定到界面
  • &#34; 0.0.0.0&#34; - 绑定到所有可用的接口。