Connect to Neo4j in Docker from python in a different Docker container

I am having difficulty connecting to Neo4j running in a Docker container from python in a different Docker container on Ubuntu 18.04. Here are some details:

Creating the container
$ sudo docker run
--name Neo4j.4C
-p27474:7474
-d
--env=NEO4J_AUTH=none
--memory=6G neo4j:4.0.3

TEST 1: With the proper python packages (neo4j/py2neo)- I am able to connect to Neo4j successfully from the host machine

>>> from py2neo import Graph
>>> graph = Graph('http://127.0.0.1:27474/') 
>>> graph.run('MATCH (x) RETURN COUNT(x)').data() # <- THIS WORKS

TEST 2: However, I am not able to make a similar connection from python within a separate Docker container:

>>> from py2neo import Graph
>>> graph = Graph('http://172.17.0.1:27474/')
>>> graph.run('MATCH (x) RETURN COUNT(x)') # <- HANGS

Within the Neo4j container, I do not see anything under /var/lib/neo4j/logs/debug.log. the last two entries are from startup:

2020-04-26 15:19:42.179+0000 INFO [o.n.s.AbstractNeoServer$ServerComponentsLifecycleAdapter] [system] Starting web server
2020-04-26 15:19:44.809+0000 INFO [o.n.s.AbstractNeoServer$ServerComponentsLifecycleAdapter] [system] Web server started.

Two questions:

  1. Has anybody else encountered this issue or have any ideas?
  2. Is there are way to enable more verbose logging within the Neo4j docker that would help troubleshooting?

Thanks in advance for any ideas here.

This isn't related to Neo4j, it's just how docker networking works. To fix this, you have to create a network and assign both of your containers to it, otherwise the containers can each talk to the host, but not to each other. This explains why it works from your host machine but not from the second docker container.

Here are the docker networking docs that will step you through how to do that.

1 Like