Cannot connect to neo4j database on Docker container
Asked Answered
M

3

7

I'm using two docker containers on one host machine. The first one is based on ordinary neo4j:2.3 image with some forwarded ports (7474). The second one is python:2.7-based container. Using curl I can access db from both host machine level and second machine (using the IP of the host itself). The problem is the piece of code I've written and ported to that container won't run anymore. I get such errors:

Traceback (most recent call last):
  File "/app/runserver.py", line 1, in <module>
    from orangebox import app
  File "/app/orangebox/__init__.py", line 5, in <module>
    from orangebox.context import ob
  File "/app/orangebox/context.py", line 2, in <module>
    from orangebox.domain.factory import DomainFactory
  File "/app/orangebox/domain/factory.py", line 4, in <module>
    from orangebox.domain.boxes import Box
  File "/app/orangebox/domain/boxes.py", line 3, in <module>
    from orangebox.domain.users import User
  File "/app/orangebox/domain/users.py", line 4, in <module>
    class User(StructuredNode):
  File "/app/src/neomodel/neomodel/core.py", line 65, in __new__
    install_labels(inst)
  File "/app/src/neomodel/neomodel/core.py", line 15, in install_labels
    db.new_session()
  File "/app/src/neomodel/neomodel/util.py", line 119, in new_session
    if self.session.neo4j_version < (2, 0):
  File "/usr/local/lib/python2.7/site-packages/py2neo/core.py", line 958, in neo4j_version
    return version_tuple(self.resource.metadata["neo4j_version"])
  File "/usr/local/lib/python2.7/site-packages/py2neo/core.py", line 213, in metadata
    self.get()
  File "/usr/local/lib/python2.7/site-packages/py2neo/core.py", line 258, in get
    response = self.__base.get(headers=headers, redirect_limit=redirect_limit, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/py2neo/packages/httpstream/http.py", line 966, in get
    return self.__get_or_head("GET", if_modified_since, headers, redirect_limit, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/py2neo/packages/httpstream/http.py", line 943, in __get_or_head
    return rq.submit(redirect_limit=redirect_limit, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/py2neo/packages/httpstream/http.py", line 433, in submit
    http, rs = submit(self.method, uri, self.body, self.headers)
  File "/usr/local/lib/python2.7/site-packages/py2neo/packages/httpstream/http.py", line 362, in submit
    raise SocketError(code, description, host_port=uri.host_port)
py2neo.packages.httpstream.http.SocketError: Connection refused

I have also tried to use https at port 7473 but it won't work either. As from the caller side I'm using the following invocations:

from py2neo import authenticate, Graph
from py2neo.ext.ogm import Store
import os

class DatabaseConnection(object):

    def __init__(self, address):
        print "Connecting to Neo4j database using URL %s" % os.environ['DB_URL']
        self.graph = Graph(address)
        self.store = Store(self.graph)

    def get_graph(self):
        return self.graph

    def get_cypher(self):
        return self.graph.cypher

    def get_store(self):
        return self.store

authenticate("172.17.0.1:7474", "neo4j", "orangebox")
db_connection = DatabaseConnection(address = "http://172.17.0.1:7474/db/data")

Username and password is correct. When it's not I get unauthorized (so connection is possible...)

Connecting to Neo4j database using URL http://172.17.0.1:7474/db/data/
Traceback (most recent call last):
  File "/app/runserver.py", line 1, in <module>
    from orangebox import app
  File "/app/orangebox/__init__.py", line 5, in <module>
    from orangebox.context import ob
  File "/app/orangebox/context.py", line 2, in <module>
    from orangebox.domain.factory import DomainFactory
  File "/app/orangebox/domain/factory.py", line 3, in <module>
    from orangebox.database.connection import db_connection
  File "/app/orangebox/database/connection.py", line 22, in <module>
    db_connection = DatabaseConnection(address = os.environ['DB_URL'])
  File "/app/orangebox/database/connection.py", line 10, in __init__
    self.store = Store(self.graph)
  File "/usr/local/lib/python2.7/site-packages/py2neo/ext/ogm/store.py", line 42, in __init__
    if self.graph.supports_optional_match:
  File "/usr/local/lib/python2.7/site-packages/py2neo/core.py", line 1087, in supports_optional_match
    return self.neo4j_version >= (2, 0)
  File "/usr/local/lib/python2.7/site-packages/py2neo/core.py", line 958, in neo4j_version
    return version_tuple(self.resource.metadata["neo4j_version"])
  File "/usr/local/lib/python2.7/site-packages/py2neo/core.py", line 213, in metadata
    self.get()
  File "/usr/local/lib/python2.7/site-packages/py2neo/core.py", line 261, in get
    raise Unauthorized(self.uri.string)
py2neo.error.Unauthorized: http://172.17.0.1:7474/db/data/

On the docker neo4j container I see only:

root@ubuntu-14:/home/vagrant# docker run --publish=7474:7474 --volume=$HOME/neo4j/data:/data kbhit/orangebox-db
Starting Neo4j Server console-mode...
2016-05-13 14:01:35.076+0000 INFO  Successfully started database
2016-05-13 14:01:35.112+0000 INFO  Starting HTTP on port 7474 (2 threads available)
2016-05-13 14:01:35.327+0000 INFO  Enabling HTTPS on port 7473
2016-05-13 14:01:35.417+0000 INFO  Mounting static content at /webadmin
2016-05-13 14:01:35.485+0000 INFO  Mounting static content at /browser
2016-05-13 14:01:36.718+0000 INFO  Remote interface ready and available at http://0.0.0.0:7474/
2016-05-13 14:02:17.825+0000 WARN  Failed authentication attempt for 'neo4j' from 172.17.0.1

How is it possible?

When I install neo4j-service in the same container using guide https://www.digitalocean.com/community/tutorials/how-to-install-neo4j-on-an-ubuntu-vps it works

Matrimony answered 13/5, 2016 at 13:54 Comment(0)
B
6

You shouldn't install the neo4j-service in the same container as your python applications. You should link the containers using docker. I think you really shouldn't do that URL appointment with fixed IP.

docker-compose.yml

version: '2'

services:
  data:
    build: data

  neo4j:
    image: neo4j:3.0
    networks: 
      - back 
    volumes_from: 
      - data
    ports: 
      - "7474:7474"
      - "7687:7687"
    volumes:
      - /var/neo4j/data:/data  

  neo4jtest:
    image: neo4j:3.0
    networks: 
      - back 
    volumes_from: 
      - data
    ports: 
      - "7475:7474"
      - "7688:7687"
    volumes:
      - /var/neo4j/test-data:/data

  api:
    build: api
    networks:
      - back
    ports:
      - "5002:5002"
    volumes:
      - /home/santiago/work/santiagocloud/api:/src/api   
    links:
      - neo4j:neo4j
      - neo4jtest:neo4jtest

networks:
  back:

Python code:

def connectGraph():
    print("Start - Connect Graph")
    # authenticate(""http://neo4j:7474", "neo4j", "test123")
    # graph = Graph("http://neo4j:7474/db/data/")


    authenticate(app.config['DB_URL'],app.config['DB_USERNAME'],app.config['DB_PASSWORD'])
    graph = Graph(app.config['DB_URL'] + "/db/data/")

    print("End - Connect Graph")
    return graph
graph = connectGraph()
Boger answered 11/10, 2016 at 17:59 Comment(2)
I did, and I did read it again. Sorry for the long time to answer. You can curl, to the docker machine, but are you sure that you this machines have the link builded in the right way? When I say that I am using an docker-compose.yml file, it is because of the network thing. the link that actually make the machines together. I think your code is right, because it works when you run it under the same containers, but your link may be wrong, because it doesn't when you are in different containers.Boger
I'm just trying to help, I'm also kind new to Docker and Neo4j. But a few problems that I had before, was because of links in Dockers... That's why, I'm also saying that you shouldn't link with IP.Boger
G
1

You should start neo4j container first in the background. Use

$ sudo docker-compose up neo4j

then start your app

$ sudo docker-compose up your_app

you can access the database via terminal. write following command:

$ sudo docker exec -it neo4j bash

then start the neo4j shell with this command:

# cypher-shell -u neo4j -p test

you can now use your database with neo4j shell for all queries. dont forget to type a semicolon at the end of each query:

> match(n) return(n);

Grigsby answered 21/5, 2019 at 10:31 Comment(0)
I
0

Make sure you have the latest Py2neo driver installed, and you must delay your base image to allow the Neo4j db to come up.

Ichthyology answered 7/4, 2017 at 3:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.