dockerdocker-composeneo4jcartography

Neo4j - cartography ConnectionRefusedError?


I am getting the following error when running docker compose but when I go to bolt://localhost:7687 I do get the graph and all the labels and nodes. Dockerfile and Docker compose are attached.

ERROR:neo4j:Unable to retrieve routing information
cartography-cartography-1  | Traceback (most recent call last):
cartography-cartography-1  |   File "/usr/local/bin/cartography", line 33, in <module>
cartography-cartography-1  |     sys.exit(load_entry_point('cartography', 'console_scripts', 'cartography')())
cartography-cartography-1  |   File "/var/cartography/cartography/cli.py", line 553, in main
cartography-cartography-1  |     sys.exit(CLI(default_sync, prog='cartography').main(argv))
cartography-cartography-1  |   File "/var/cartography/cartography/cli.py", line 533, in main
cartography-cartography-1  |     return cartography.sync.run_with_config(self.sync, config)
cartography-cartography-1  |   File "/var/cartography/cartography/sync.py", line 163, in run_with_config
cartography-cartography-1  |     return sync.run(neo4j_driver, config)
cartography-cartography-1  |   File "/var/cartography/cartography/sync.py", line 81, in run
cartography-cartography-1  |     with neo4j_driver.session() as neo4j_session:
cartography-cartography-1  |   File "/var/cartography/cartography/experimental_neo4j_4x_support.py", line 167, in wrapper
cartography-cartography-1  |     patch_session_obj(neo4j_session)
cartography-cartography-1  |   File "/var/cartography/cartography/experimental_neo4j_4x_support.py", line 155, in patch_session_obj
cartography-cartography-1  |     detect_neo4j_version(neo4j_session)
cartography-cartography-1  |   File "/var/cartography/cartography/experimental_neo4j_4x_support.py", line 143, in detect_neo4j_version
cartography-cartography-1  |     result = neo4j_session.run(
cartography-cartography-1  |   File "/usr/local/lib/python3.9/site-packages/neo4j/work/simple.py", line 204, in run
cartography-cartography-1  |     self._connect(self._config.default_access_mode)
cartography-cartography-1  |   File "/usr/local/lib/python3.9/site-packages/neo4j/work/simple.py", line 108, in _connect
cartography-cartography-1  |     super()._connect(access_mode)
cartography-cartography-1  |   File "/usr/local/lib/python3.9/site-packages/neo4j/work/__init__.py", line 75, in _connect
cartography-cartography-1  |     self._pool.update_routing_table(
cartography-cartography-1  |   File "/usr/local/lib/python3.9/site-packages/neo4j/io/__init__.py", line 1213, in update_routing_table
cartography-cartography-1  |     raise ServiceUnavailable("Unable to retrieve routing information")
cartography-cartography-1  | neo4j.exceptions.ServiceUnavailable: Unable to retrieve routing information
cartography-cartography-1  | INFO:cartography.sync:Starting sync with update tag '1661424422'
cartography-cartography-1  | Traceback (most recent call last):
cartography-cartography-1  |   File "/usr/local/lib/python3.9/site-packages/neo4j/io/_socket.py", line 147, in _connect
cartography-cartography-1  |     s.connect(resolved_address)
cartography-cartography-1  | ConnectionRefusedError: [Errno 111] Connection refused
cartography-cartography-1  |
cartography-cartography-1  | During handling of the above exception, another exception occurred:
cartography-cartography-1  |
cartography-cartography-1  | Traceback (most recent call last):
cartography-cartography-1  |   File "/usr/local/lib/python3.9/site-packages/neo4j/io/_socket.py", line 284, in connect
cartography-cartography-1  |     s = BoltSocket._connect(resolved_address, timeout, keep_alive)
cartography-cartography-1  |   File "/usr/local/lib/python3.9/site-packages/neo4j/io/_socket.py", line 164, in _connect
cartography-cartography-1  |     raise ServiceUnavailable(
cartography-cartography-1  | neo4j.exceptions.ServiceUnavailable: Failed to establish connection to ResolvedIPv4Address(('127.0.0.1', 7687)) (reason [Errno 111] Connection refused)
cartography-cartography-1  |
cartography-cartography-1  | The above exception was the direct cause of the following exception:
cartography-cartography-1  |
cartography-cartography-1  | Traceback (most recent call last):
cartography-cartography-1  |   File "/usr/local/bin/cartography", line 33, in <module>
cartography-cartography-1  |     sys.exit(load_entry_point('cartography', 'console_scripts', 'cartography')())
cartography-cartography-1  |   File "/var/cartography/cartography/cli.py", line 553, in main
cartography-cartography-1  |     sys.exit(CLI(default_sync, prog='cartography').main(argv))
cartography-cartography-1  |   File "/var/cartography/cartography/cli.py", line 533, in main
cartography-cartography-1  |     return cartography.sync.run_with_config(self.sync, config)
cartography-cartography-1  |   File "/var/cartography/cartography/sync.py", line 163, in run_with_config
cartography-cartography-1  |     return sync.run(neo4j_driver, config)
cartography-cartography-1  |   File "/var/cartography/cartography/sync.py", line 81, in run
cartography-cartography-1  |     with neo4j_driver.session() as neo4j_session:
cartography-cartography-1  |   File "/var/cartography/cartography/experimental_neo4j_4x_support.py", line 167, in wrapper
cartography-cartography-1  |     patch_session_obj(neo4j_session)
cartography-cartography-1  |   File "/var/cartography/cartography/experimental_neo4j_4x_support.py", line 155, in patch_session_obj
cartography-cartography-1  |     detect_neo4j_version(neo4j_session)
cartography-cartography-1  |   File "/var/cartography/cartography/experimental_neo4j_4x_support.py", line 143, in detect_neo4j_version
cartography-cartography-1  |     result = neo4j_session.run(
cartography-cartography-1  |   File "/usr/local/lib/python3.9/site-packages/neo4j/work/simple.py", line 204, in run
cartography-cartography-1  |     self._connect(self._config.default_access_mode)
cartography-cartography-1  |   File "/usr/local/lib/python3.9/site-packages/neo4j/work/simple.py", line 108, in _connect
cartography-cartography-1  |     super()._connect(access_mode)
cartography-cartography-1  |   File "/usr/local/lib/python3.9/site-packages/neo4j/work/__init__.py", line 82, in _connect
cartography-cartography-1  |     self._connection = self._pool.acquire(
cartography-cartography-1  |   File "/usr/local/lib/python3.9/site-packages/neo4j/io/__init__.py", line 926, in acquire
cartography-cartography-1  |     return self._acquire(self.address, deadline)
cartography-cartography-1  |   File "/usr/local/lib/python3.9/site-packages/neo4j/io/__init__.py", line 801, in _acquire
cartography-cartography-1  |     return connection_creator()
cartography-cartography-1  |   File "/usr/local/lib/python3.9/site-packages/neo4j/io/__init__.py", line 729, in connection_creator
cartography-cartography-1  |     connection = self.opener(
cartography-cartography-1  |   File "/usr/local/lib/python3.9/site-packages/neo4j/io/__init__.py", line 907, in opener
cartography-cartography-1  |     return Bolt.open(
cartography-cartography-1  |   File "/usr/local/lib/python3.9/site-packages/neo4j/io/__init__.py", line 327, in open
cartography-cartography-1  |     s, pool_config.protocol_version, handshake, data = BoltSocket.connect(
cartography-cartography-1  |   File "/usr/local/lib/python3.9/site-packages/neo4j/io/_socket.py", line 311, in connect
cartography-cartography-1  |     raise ServiceUnavailable(
cartography-cartography-1  | neo4j.exceptions.ServiceUnavailable: Couldn't connect to localhost:7687 (resolved to ('127.0.0.1:7687', '[::1]:7687')):
cartography-cartography-1  | Failed to establish connection to ResolvedIPv4Address(('127.0.0.1', 7687)) (reason [Errno 111] Connection refused)
cartography-cartography-1  | Failed to establish connection to ResolvedIPv6Address(('::1', 7687, 0, 0)) (reason [Errno 99] Cannot assign requested address)
cartography-cartography-1 exited with code 1

Docker compose:

version: "3.7"
services:
  neo4j:
    image: neo4j:4.4.5-community
    restart: unless-stopped
    ports:
      - "7474:7474"
      - "7687:7687"
    volumes:
      - ./.compose/neo4j/conf:/conf
      - ./.compose/neo4j/data:/data
      - ./.compose/neo4j/import:/import
      - ./.compose/neo4j/logs:/logs
      - ./.compose/neo4j/plugins:/plugins
    environment:
      # Raise memory limits:
      - dbms.memory.pagecache.size=1G
      - dbms.memory.heap.initial_size=1G
      - dbms.memory.heap.max_size=1G
      # Auth:
      - NEO4J_AUTH=none
      # Add APOC and GDS:
      - apoc.export.file.enabled=true
      - apoc.import.file.enabled=true
      - apoc.import.file.use_neo4j_config=true
      - NEO4JLABS_PLUGINS=["apoc", "graph-data-science"]
      - dbms.security.procedures.allowlist=gds.*, apoc.*
      - dbms.security.procedures.unrestricted=gds.*, apoc.*
      # Networking:
      - dbms.default_listen_address=0.0.0.0
      - dbms.connector.bolt.listen_address=:7687

    healthcheck:
        test: ["CMD", "curl", "-f", "http://localhost:7474"]
        interval: 10s
        timeout: 10s
        retries: 10
  cartography:
    build:
      context: .
      dockerfile: dist.Dockerfile
    init: true
    restart: on-failure
    depends_on:
      - neo4j
    environment:
      - EXPERIMENTAL_NEO4J_4X_SUPPORT=True 
    volumes:
      - /Users/stevesolun/.aws:/root/.aws/

Dockerfile:

FROM python:3.9-slim

# the UID and GID to run cartography as
# (https://github.com/hexops/dockerfile#do-not-use-a-uid-below-10000).
ARG uid=0 #10001
ARG gid=0 #10001

COPY . /var/cartography
WORKDIR /var/cartography

RUN apt-get update --fix-missing
RUN apt --allow-unauthenticated  update -y
RUN apt-get install vim telnet curl gcc -y

RUN pip install -U -e .

USER ${uid}:${gid}


# verify that the binary at least runs
RUN cartography -h

ENTRYPOINT ["cartography"]

CMD ["-v", "--neo4j-uri=bolt://localhost:7687", "--aws-sync-all-profiles"]

Solution

  • I have run into this problem before. When you use localhost url inside a container, it doesn't treat it as the host localhost, but inside of the container AFAIK. There are two options you can go about. One is to add:

    network_mode: host
    

    to the cartography service.Then the localhost points to the host machine and it should find Neo4j at localhost:7474.

    Although I don't prefer this option. When using docker-compose, you can add the linking to your cartography service:

    links:
      - neo4j
    

    It seems that links are deprecated, so it probably works even without it. So essentially instead of specifying --neo4j-uri=bolt://localhost:7687 you can specify --neo4j-uri=bolt://neo4j:7687 to point at Neo4j instance container. The URL is identical to the service name