首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Docker容器中的Neo4j驱动程序无法连接到Neo4j

Docker容器中的Neo4j驱动程序无法连接到Neo4j
EN

Stack Overflow用户
提问于 2022-08-22 21:52:45
回答 1查看 88关注 0票数 0

我在一个Docker容器中运行了Neo4j 4.4.7,在另一个容器中运行了一个node.js应用程序,加载了新4j驱动程序4.4.7版本。

在Docker容器中运行的应用程序无法连接到neo4j,但是当我从Docker之外的命令行运行它时,它就成功了。

app_test_neo4j.js基本上是从neo4j文档中复制的:

代码语言:javascript
复制
const http     = require('http');
const neo4j    = require('neo4j-driver');  // neo4j-driver version 4.7.7
const koa      = require('koa');
        
const driver = neo4j.driver('bolt://localhost:7687', neo4j.auth.basic('neo4j','pwd'))
        
const test = async ()=>{
            try {
              await driver.verifyConnectivity()
              console.log('Driver created')
            } catch (error) {
              console.log(`connectivity verification failed. ${error}`)
            }
        
            const session = driver.session()
            try {
              let res = await session.run('RETURN 1',{})
              console.log('res records='+JSON.stringify(res.records));
            } catch (error) {
              console.log(`unable to execute query. ${error}`)
            } finally {
              await session.close()
            }
        
            // ... on application exit:
            await driver.close()
}
    
test();

dockerfile如下:

代码语言:javascript
复制
FROM node:18.4-alpine3.16 AS package

RUN addgroup geo    && \
    adduser -D -G geo -h /home/geo -s /bin/ash geo     && \
    chmod 755 /home/geo    && \
    echo 'geo ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers

# Set the working directory to geo' user home directory
WORKDIR /home/geo

# Specify the user to execute all commands below
USER geo
ADD . .
EXPOSE 8080 7867
CMD ["node", "app_test_neo4j.js"]

如果我从命令行运行节点app_test_neo4j.js,它就连接到neo4j,没有问题:

代码语言:javascript
复制
$ node app_test_neo4j.js
Driver created
res records=[{"keys":["1"],"length":1,"_fields":[{"low":1,"high":0}],"_fieldLookup":{"1":0}}]

如果我构建了dockerfile并运行了一个容器,它就会像这样失败:

代码语言:javascript
复制
Successfully built e125b52e147c

$ docker run -it -d -p 8080:8080 e125b52e147c
c74971794be9c26af6e4d491d76b99a249a718b3b2c268a6499c98b574fe4178

$ docker logs c74971794be9c26af6e4d491d76b99a249a718b3b2c268a6499c98b574fe4178
connectivity verification failed. 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

我现在对这件事没什么想法了。任何帮助都非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-22 22:14:37

你复制了写着localhost的代码。

您的应用程序容器不是数据库。您需要在应用程序代码中使用另一个容器的地址。

https://docs.docker.com/network/network-tutorial-standalone/

它只能在Docker之外工作,因为您已经向主机公开了Neo4j容器的网络端口。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73451356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档