I'm new to Docker Compose, and I'm trying to get two images (one a Node app, the other a Neo4j db) to work together locally. My OS is Ubuntu. While each image works fine separately, I can't get the Node app image to connect to the database image.
I'm almost certain is has to do with the port connections; to be precise, with the URL parameter I'm using in the driver connection:
var driver = neo4j.driver("bolt://neo4j:17687", neo4j.auth.basic("tester", "tester"));
Where the "17687" is the port I mapped Neo4j's Bolt port to in the docker-compose.yml, and "neo4j" is the name I assigned to the Neo4j image (see below).
I was following (using a different JS driver) the only Node+Neo4j+Docker tutorial I could find (https://medium.com/@slavahatnuke/neo4j-node-js-docker-docker-compose-fdc1cc9cf405), and the user comments there seem to confirm that the error is in my connection URL.
To try and summarize:
- The Node app is using the official neo4j-javascript-driver to connect, via Bolt, to the Neo4j database. The exact code is:
"use strict";
var express = require('express');
var neo4j = require('neo4j-driver').v1;
const PORT = 8080;
var app = express();
app.get('/', function (req, res) {
console.log("This is a Neo4j connection test.");
var driver = neo4j.driver("bolt://neo4j:17687", neo4j.auth.basic("tester", "tester"));
// Register a callback to know if driver creation was successful:
driver.onCompleted = function () {
console.log("driver creation successful");
var session = driver.session();
session.run('MATCH (n) RETURN COUNT(n) AS c')
.then(function(result) { console.log("in result"); console.log(result); session.close(); })
.catch(function(error) { console.log("query error: " + error); });
}
// Register a callback to know if driver creation failed.
// This could happen due to wrong credentials or database unavailability:
driver.onError = function (error) {
console.log('Driver instantiation failed', error);
};
});
app.listen(PORT);
console.log("Running on " + PORT);
module.exports = app;
-
The Neo4j image is Docker's official Neo4j image.
-
I am using the following docker-compose.yml file:
nodeapp: image: djesse/nodeapp volumes: - ./nodeapp:/nodeapp links: - neo4j ports: - "18080:8080" working_dir: /nodeapp entrypoint: node index.js neo4j: image: neo4j ports: - "17474:7474" - "17687:7687" volumes: - ./db/dbms:/data/dbms
-
When I run it (with "docker-compose up"), I can:
A. Access the Neo4j browser console fine, by pointing my browser to: "localhost:17474". I used the provided user credentials to sign in and can run Cypher queries fine.
B. If I don't try to create a Neo4j driver, I can run any regular GET function in the Node by pointing my browser to: "localhost:18080".
C. The output indicates the following: "Bolt enabled on 0.0.0.0:7687."
So I've been concentrating on the connection URL within the Node.
But it has not connected yet.Specifically, according to the log, the program just keeps trying and failing to create the driver (it keeps repeating the line "This is a Neo4j connection test").
I'm certain there's something simple to Docker Compose that I'm missing.
Without Docker, I used the URL "bolt://localhost:7687" to successfully connect the app to the database. Now that it's in Docker, I don't know what I should use.
Thank you for any guidance.
via Aphorism44
No comments:
Post a Comment