简体   繁体   中英

Unable to connect to neo4j database on docker container with docker compose

Here is my docker-compose.yml used to create the database container.

version: '3.7'
services:

  application:
    build:
      context: ./app
      dockerfile: dockerfile #dockerfile-prod
    depends_on:
      - database_mongo
      - database_neo4j
      - etl_pipeline
    environment:
      - flask_env=dev #flask_env=prod
    volumes:
      - ./app:/app
    ports:
      - "8080:8080" #- 8080:8080

  database_mongo:
    image: "mongo:4.2"
    expose:
      - 27017
    volumes:
      - ./data/database/mongo:/data/db

  database_neo4j:
    image: neo4j:latest
    expose:
      - 27018
    volumes:
      - ./data/database/neo4j:/data
    ports: 
      - "7474:7474" # web client
      - "7687:7687" # DB default port 
    environment: 
      -  NEO4J_AUTH=none
    
  etl_pipeline:
    depends_on:
      - database_mongo
      - database_neo4j
    build:
      context: ./data/etl
      dockerfile: dockerfile #dockerfile-prod
    volumes:
    - ./data/:/data/
    - ./data/etl:/app/

I'm trying to connect to my neo4j database with python driver. I have already been able to connect to mongoDb with this line:

mongo_client = MongoClient(host="database_mongo")

I'm trying to do something similar to the mongoDb to connect to my neo4j with the GraphDatabase in neo4j like this:

url = "{scheme}://{host_name}:{port}".format(scheme = "bolt", host_name="database_neo4j", port = 7687)
baseNeo4j = GraphDatabase.driver(url, encrypted=False)

or with py2neo like this

neo_client = Graph(host="database_neo4j")

However, nothing of this has worked yet and so I'm not sure if I'm using the right syntax in order to use neo4j with docker. I've tried many things and looked around, but couldn't find the answer...

The whole error message is:

etl_pipeline_1    | MongoClient(host=['database_mongo:27017'], document_class=dict, tz_aware=False, connect=True)
etl_pipeline_1    | Traceback (most recent call last):
etl_pipeline_1    |   File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 929, in _connect
etl_pipeline_1    |     s.connect(resolved_address)
etl_pipeline_1    | ConnectionRefusedError: [Errno 111] Connection refused
etl_pipeline_1    | 
etl_pipeline_1    | During handling of the above exception, another exception occurred:
etl_pipeline_1    | 
etl_pipeline_1    | Traceback (most recent call last):
etl_pipeline_1    |   File "main.py", line 26, in <module>
etl_pipeline_1    |     baseNeo4j = GraphDatabase.driver(url, encrypted=False)
etl_pipeline_1    |   File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py", line 183, in driver
etl_pipeline_1    |     return cls.bolt_driver(parsed.netloc, auth=auth, **config)
etl_pipeline_1    |   File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py", line 196, in bolt_driver
etl_pipeline_1    |     return BoltDriver.open(target, auth=auth, **config)
etl_pipeline_1    |   File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py", line 359, in open
etl_pipeline_1    |     pool = BoltPool.open(address, auth=auth, pool_config=pool_config, workspace_config=default_workspace_config)
etl_pipeline_1    |   File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 531, in open
etl_pipeline_1    |     seeds = [pool.acquire() for _ in range(pool_config.init_size)]
etl_pipeline_1    |   File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 531, in <listcomp>
etl_pipeline_1    |     seeds = [pool.acquire() for _ in range(pool_config.init_size)]
etl_pipeline_1    |   File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 545, in acquire
etl_pipeline_1    |     return self._acquire(self.address, timeout)
etl_pipeline_1    |   File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 409, in _acquire
etl_pipeline_1    |     connection = self.opener(address, timeout)
etl_pipeline_1    |   File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 528, in opener
etl_pipeline_1    |     return Bolt.open(addr, auth=auth, timeout=timeout, routing_context=routing_context, **pool_config)
etl_pipeline_1    |   File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 198, in open
etl_pipeline_1    |     keep_alive=pool_config.keep_alive,
etl_pipeline_1    |   File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 1049, in connect
etl_pipeline_1    |     raise last_error
etl_pipeline_1    |   File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 1039, in connect
etl_pipeline_1    |     s = _connect(resolved_address, timeout, keep_alive)
etl_pipeline_1    |   File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 943, in _connect
etl_pipeline_1    |     raise ServiceUnavailable("Failed to establish connection to {!r} (reason {})".format(resolved_address, error))
etl_pipeline_1    | neo4j.exceptions.ServiceUnavailable: Failed to establish connection to IPv4Address(('172.29.0.2', 7687)) (reason [Errno 111] Connection refused)

好的,所以它可能不是最好的答案,但是对于遇到此问题的其他任何人,我能够通过在 main 的开头添加sleep(30)来解决它

You can try to create network and use it across your services. Something like this:

networks:
  neo4j_network:
    driver: bridge
services:
  neo4j:
    image: neo4j:latest
    expose:
      - 27018
    volumes:
      - ./data/database/neo4j:/data
    ports: 
      - "7474:7474" # web client
      - "7687:7687" # DB default port 
    environment: 
      -  NEO4J_AUTH=none
    networks:
      - neo4j_network
  application:
    build:
      context: ./app
      dockerfile: dockerfile #dockerfile-prod
    depends_on:
      - database_mongo
      - database_neo4j
      - etl_pipeline
    environment:
      - flask_env=dev #flask_env=prod
    volumes:
      - ./app:/app
    ports:
      - "8080:8080"
    networks:
      - neo4j_network

Then, for your neo4j driver url (in your code), make sure to use bolt://host.docker.internal:7687

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM