Neo4j + Node.js + docker + docker-compose
This is a very simple post about how to use Neo4j + Node.js with docker.
I will try to explain why we use docker. I like the principle when you take the thing and use it.
Store
For example take the store. When you come to the store and buy something for example a T-shirt. You probably do not think:
- who stroked the T-shirt?
- how it was brought to the store?
You just take this thing and use. This principle is simple and therefore I like it.
Docker + Docker Compose
Docker — in simple words it is a store, you take a product or a thing and use it. Only goods in this docker store you can take for free. Docker Compose — it is a tool to simplify the work with docker.
Application
In order to build the app, we take images from the docker (docker hub):
- Neo4j
- Node.js
They are different images and virtual machines (containers).
Now our task is to connect them together. But first, let’s setup them separately!
Setup Neo4j
To configure virtual machines (containers) describe it in the docker-compose.yml file.
##docker-compose.ymlneo4j:
image: neo4j:3.0
ports:
- "17474:7474"
run it
$ docker-compose up neo4j
Neo4j is ready to work. It looks like we have just set up a new Neo4j server. The important point here
....
ports:
- "17474:7474"
Neo4j database uses port 7474 inside of the container. In the configuration we published port 7474 -> 17474. This means that the port 17474 is available from the outside now.
If you use a docker-machine. Find out the IP of your docker instance
$ docker-machine ip
192.168.99.100
It is Neo4j web interface.
Setup Node.js
docker-compose.yml looks like
app:
image: node:4
entrypoint: node -e "console.log('Hello node.js')"
Connect both sides
Let’s configure our application so that neo4j was available for node.js container. Important point here
....
links: ## connects neo4j to app container
- neo4j
....
docker-compose.yml for this:
##docker-compose.ymlapp:
image: node:4 ## node.js v4
volumes: ## adds app folder to the container
- ./app:/app
links: ## connects neo4j to app container
- neo4j
ports: ## publish ports
- "13000:3000"
working_dir: /app ## uses work dir /app
entrypoint: node app.js ## starts application
neo4j:
image: neo4j:3.0 ## we use neo4j v3
ports: ## publish ports to have Web UI
- "17474:7474"
volumes: ## adds folder with login/password
- ./db/dbms:/data/dbms
Here is an example using Neo4j in app.js.
var neo4j = require('node-neo4j');
var db = new neo4j('http://neo4j:test@neo4j:7474');db.insertNode({
name: 'Darth Vader #' + parseInt(Math.random() * 100),
sex: 'male'
}, ['Person'], function (err, node) {
if (err) return next(err);
console.log(node);
});
Follow link below to see this example with express.js api on github:
https://github.com/slavahatnuke/neo4j-docker-nodejs
How to run? — easy:
$ git clone https://github.com/slavahatnuke/neo4j-docker-nodejs.git
$ cd neo4j-docker-nodejs
$ make
If you use docker-machine. Make sure that 192.168.99.100 is `docker-machine ip`, or put your docker machine IP to the URL.
Here is the example where you can load or delete the Neo4j data.
Docker documentation:
- docker machine (OSX) — https://docs.docker.com/machine/
- docker compose — https://docs.docker.com/compose/
- docker docs — https://docs.docker.com/
Best regards +1G Team