无法从Docker实例连接到neo4j
我有一个连接到neo4j的Node.js应用程序。运行它通常可以正常运行,我可以连接。但是,当我在Docker中运行它时,会遇到此错误:
Neo4jError: Failed to connect to server. Please ensure that your database is listening on the correct host and port and that you have compatible encryption settings both on Neo4j server and driver. Note that the default encryption setting has changed in Neo4j 4.0. Caused by: connect ECONNREFUSED 127.0.0.1:7687
at newError (/usr/src/app/node_modules/neo4j-driver/lib/error.js:75:10)
at NodeChannel._handleConnectionError (/usr/src/app/node_modules/neo4j-driver/lib/internal/node/node-channel.js:229:41)
at Socket.emit (events.js:310:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
我正在使用neo4j-driver v4.0.2和Neo4j 4.0.3。
我创建了一个仓库来重现该问题:
我是否缺少有关Docker网络的内容?
我发现了与python驱动程序相关的问题:
感谢您的帮助!
回答如下:您的Docker映像在隔离的网络中运行,因此它无法访问localhost:7687
处的neo4j
[在您的javascript文件中,尝试更改要连接到主机IP的URL,而不是localhost
。您可以通过运行ip addr show
找到它。
更好的是,您可以使用--add-host
标志-add host to container example将主机映射传递到您的容器中>
docker run -it --add-host=neo4j:[your-host-ip] user/test-neo4j:latest
然后您可以使用
neo4j
代替index.js中的localhost
进行连接