NodeJs connection to Neo4j error

I have set up a development environment with docker on an ubuntu v23.04 base image. I have installed neo4j v5.9 and neo4j-driver for NodeJs v5.9.2.

After starting the docker image, I can access the neo4j browser via:
http://be.waterways.local:7474/browser/
Here I get the message: You are connected as user neo4j to neo4j://be.waterways.local:7687. I have also set up the movie database.

In my NodeJs server I try to connect to the neo4j db server:

const user = 'neo4j';
const password = 'neo4j';
const url = 'neo4j://be.waterways.local:7687';
const driver = neo4j.driver(url, neo4j.auth.basic(user, password));
const serverInfo = await driver.getServerInfo();

I get the following error:

Error executing Neo4j query: Neo4jError: Could not perform discovery. No routing servers available. Known routing table: RoutingTable[database=default database, expirationTime=0, currentTime=1687888112016, routers=[], readers=[], writers=[]]
     at new Neo4jError (/app/node_modules/neo4j-driver-core/lib/error.js:77:16)
     at newError (/app/node_modules/neo4j-driver-core/lib/error.js:113:12)
     at RoutingConnectionProvider.<anonymous> (/app/node_modules/neo4j-driver-bolt-connection/lib/connection-provider/connection-provider-routing.js:698:68)
     at step (/app/node_modules/neo4j-driver-bolt-connection/lib/connection-provider/connection-provider-routing.js:100:23)
     at Object.next (/app/node_modules/neo4j-driver-bolt-connection/lib/connection-provider/connection-provider-routing.js:81:53)
     at /app/node_modules/neo4j-driver-bolt-connection/lib/connection-provider/connection-provider-routing.js:75:71
     at new Promise (<anonymous>)
     at __awaiter (/app/node_modules/neo4j-driver-bolt-connection/lib/connection-provider/connection-provider-routing.js:71:12)
     at RoutingConnectionProvider._applyRoutingTableIfPossible (/app/node_modules/neo4j-driver-bolt-connection/lib/connection-provider/connection-provider-routing.js:692:16)
     at RoutingConnectionProvider.<anonymous> (/app/node_modules/neo4j-driver-bolt-connection/lib/connection-provider/connection-provider-routing.js:502:55) {
   constructor: [Function: Neo4jError] { isRetriable: [Function (anonymous)] },
   code: 'ServiceUnavailable',
   retriable: true,
   [cause]: 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 new Neo4jError (/app/node_modules/neo4j-driver-core/lib/error.js:77:16)
       at newError (/app/node_modules/neo4j-driver-core/lib/error.js:113:12)
       at NodeChannel._handleConnectionError (/app/node_modules/neo4j-driver-bolt-connection/lib/channel/node/node-channel.js:227:56)
      at Socket.emit (node:events:511:28)
       at emitErrorNT (node:internal/streams/destroy:151:8)
       at emitErrorCloseNT (node:internal/streams/destroy:116:3)
       at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
     constructor: [Function: Neo4jError] { isRetriable: [Function (anonymous)] },
     code: 'SessionExpired',
     retriable: true
   }
 }

If I use the url: 'bolt://be.waterways.local:7687' to connect, I only get the

"Failed to connect to server..." error:
Error executing Neo4j query: 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 new Neo4jError (/app/node_modules/neo4j-driver-core/lib/error.js:77:16)
     at newError (/app/node_modules/neo4j-driver-core/lib/error.js:113:12)
     at NodeChannel._handleConnectionError (/app/node_modules/neo4j-driver-bolt-connection/lib/channel/node/node-channel.js:227:56)
     at Socket.emit (node:events:511:28)
     at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
   constructor: [Function: Neo4jError] { isRetriable: [Function (anonymous)] },
   code: 'ServiceUnavailable',
   retriable: true
 }

Please advise as I have been stuck with this for too long now. Thank you.

Most likely a firewall that blocks the port 7687?

If you try to run the code on the machine itself, does it work there?

@michael.hunger There is no firewall installed, but I'll check routing between the nodejs and neo4j docker container. Thank you for the tip. Ports 7474 and 7687 are exposed in the neo4j docker container, the browser is accessed through 7474.

I didn't understand this: how can I run the code on the machine itself?

Not sure if this helps, but when starting up the docker instance, I get these messages from neo4j:
2023-06-27 18:28:01.859+0000 INFO Starting...
2023-06-27 18:28:02.772+0000 INFO This instance is ServerId{288d842e} (288d842e-8e6d-4d3d-9140-9ec6455b5606)
2023-06-27 18:28:03.662+0000 INFO ======== Neo4j 5.9.0 ========
2023-06-27 18:28:05.925+0000 INFO Bolt enabled on 0.0.0.0:7687.
2023-06-27 18:28:06.890+0000 INFO Remote interface available at http://localhost:7474/
2023-06-27 18:28:06.894+0000 INFO id: 0EA26F343B40B2CFACB68A81A1DE16FFCE434E85B480CB5EC04EE1A2878457BE
2023-06-27 18:28:06.895+0000 INFO name: system
2023-06-27 18:28:06.895+0000 INFO creationDate: 2023-06-25T14:42:43.837Z
2023-06-27 18:28:06.895+0000 INFO Started.

This looks weird to me.

Figured it out after all.

In the URL instead of localhost or 127.0.0.1 or the local alias (defined in the hosts file), I had to use the name of the container like this:

const url = 'bolt://waterways_neo4j:7687';

Just for context, watereways_neo4j is defined in docker-compose.yml as follows:

waterways_neo4j:
image: neo4j:latest
container_name: waterways_neo4j
tty: true
volumes:
- ../neo4j-db:/data
- ./neo4j.conf:/etc/neo4j/neo4j.conf
ports:
- '7474:7474'
- '7687:7687'
networks:
- waterways_local

As a final reply to myself, can you please state somewhere in the Neo4j documentation:

If you use docker containers to deploy your project and you install neo4j in its own container as it is best practice to do, when referring to it from other containers you should use the url:

bolt://[name-of-neo4j-container]:7687