0

I have an "out of the box" Spring Data Neo4j application that works fine when talking to a Neo4j server running on my box. By "out of the box" I mean that the only config I have done in the app is specify the username and password for Neo4j.

The app (run with ./gradlew bootRun) works fine with a dockerised Neo4j server as well.

When I build a docker image of this app, it fails to connect to the Neo4j server, whether or not that is dockerised.

When it works, it says:

2019-01-18 12:58:49.311 INFO 18345 --- [ restartedMain] Driver : Direct driver instance 1080149308 created for server address localhost:7687

When it doesn't work, it says:

2019-01-18 02:27:53.760 INFO 1 --- [ main] Driver : Direct driver instance 707892422 created for server address localhost:7687

2019-01-18 02:27:54.100 INFO 1 --- [ main] ConnectionPool : Closing connection pool towards localhost:7687

[...]

Caused by: org.neo4j.driver.internal.shaded.io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/127.0.0.1:7687

I've tried linking the two dockers (app and Neo4j docker) with --link. Same result.

I've tried composing them:

version: '3'

services:
  docker-neo:
    image: neo4j:3.5
    ports:
      - 7687:7687
      - 7474:7474

  godojo:
    image: com.greenasjade.j01/j01
    depends_on:
      - docker-neo
    ports:
      - 8081:8081

No joy.

I need to end up with a dockerised solution.

GreenAsJade
  • 14,459
  • 11
  • 63
  • 98

1 Answers1

1

See this answer which refers to MySQL but explains the networking issue.

From inside of a Docker container, how do I connect to the localhost of the machine?

Bottom line though, if you're on linux add --network="host" and if you're on windows or mac change the neo4j host in your Spring Boot configuration to be host.docker.internal. To do taht edit your application.properties or application.yml file.

spring.data.neo4j.uri=bolt://host.docker.internal:7687

Documentation for Spring Boot's Neo4j support is here.

https://docs.spring.io/spring-boot/docs/2.1.2.RELEASE/reference/htmlsingle/#boot-features-neo4j

GreenAsJade
  • 14,459
  • 11
  • 63
  • 98
Mike
  • 164
  • 1
  • 4
  • How do I "change the neo4j host in your Spring Boot configuration to be host.docker.internal."? Thanks! – GreenAsJade Jan 18 '19 at 03:53
  • 1
    I've updated my answer to include an explanation of how to configure the neo4j connection. – Mike Jan 18 '19 at 04:28
  • Thanks, the link is a great help. I might be inching forwards. First it failed because you have to specify the protocol in the uri (`bolt://host.docker.internal:7687`). Once over that, now it fails during building the Docker image because the test fails with "cant connect to host". I'm guessing that's because the test is running as a local machine test, not a docker image? Can you help witih that? – GreenAsJade Jan 18 '19 at 04:55
  • Hah -x test for the build, and now I am in business! Thanks! – GreenAsJade Jan 18 '19 at 05:04
  • 1
    I've since discovered that host.docker.internal is an OS specific feature, which is obviously not ideal. Is there a more ideal solution, that ports from development on OSX (where it works) to some varieties of linux - eg Amazon EC2 - (where it doesn't)? – GreenAsJade Jan 28 '19 at 00:43