我在一个Docker容器中运行了Neo4j 4.4.7,在另一个容器中运行了一个node.js应用程序,加载了新4j驱动程序4.4.7版本。
在Docker容器中运行的应用程序无法连接到neo4j,但是当我从Docker之外的命令行运行它时,它就成功了。
app_test_neo4j.js基本上是从neo4j文档中复制的:
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如下:
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,没有问题:
$ node app_test_neo4j.js
Driver created
res records=[{"keys":["1"],"length":1,"_fields":[{"low":1,"high":0}],"_fieldLookup":{"1":0}}]如果我构建了dockerfile并运行了一个容器,它就会像这样失败:
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我现在对这件事没什么想法了。任何帮助都非常感谢!
发布于 2022-08-22 22:14:37
你复制了写着localhost的代码。
您的应用程序容器不是数据库。您需要在应用程序代码中使用另一个容器的地址。
https://docs.docker.com/network/network-tutorial-standalone/
它只能在Docker之外工作,因为您已经向主机公开了Neo4j容器的网络端口。
https://stackoverflow.com/questions/73451356
复制相似问题