dockerneo4jdockerfileneo4j-driver

Neo4j-driver in Docker container fails to connect to Neo4j


I have Neo4j 4.4.7 running in one Docker container and a node.js app running in another container, loading neo4j-driver version 4.4.7.

The app running in a Docker container fails to connect to neo4j, but it succeeds when I run it from the command line outside of Docker.

The app_test_neo4j.js is basically copied from the neo4j docs:

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();

The dockerfile is this:

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"]

If I run node app_test_neo4j.js from the command line it connects to the neo4j no problem:

$ node app_test_neo4j.js
Driver created
res records=[{"keys":["1"],"length":1,"_fields":[{"low":1,"high":0}],"_fieldLookup":{"1":0}}]

If I build the dockerfile and run a container it fails like this:

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

I'm kind of out of ideas on this one at the moment. Any help greatly appreciated!!


Solution

  • You copied code that says localhost.

    Your app container is not the database. You need to use that other container's address in your app code.

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

    It works outside of Docker only because you've exposed the Neo4j container's network port to the host.