neo4j-javascript-driver icon indicating copy to clipboard operation
neo4j-javascript-driver copied to clipboard

Connection refused in Docker Compose network

Open JonasHiltl opened this issue 3 years ago • 7 comments

I have a Node.js application and Neo4j running through docker compose, they are in the same network so I thought I wouldn't encounter any issues. But the driver fails to connect to Neo4j. It works just fine without docker through localhost.

This is my bolt connection string: bolt://neo4j:7687. neo4j is the name of the docker compose service and it just uses the ip address of the docker service.

This is the error message

Neo4jError: Failed to connect to server. Please ensure that your database is listening on the correct host and port and that you have compatible encryption settings both on Neo4j server and driver. Note that the default encryption setting has changed in Neo4j 4.0. Caused by: connect ECONNREFUSED 172.21.0.4:7687

As you can see in the error message (ECONNREFUSED 172.21.0.4:7687), docker successfully routes to the ip of the Neo4j container by just specifying the container name. So I'm unsure what could cause this connection issue.

This is the Neo4j docker service:

neo4j:
    image: neo4j:latest
    container_name: neo4j
    networks:
      - basenet
    ports:
      - 7474:7474
      - 7687:7687
    restart: on-failure
    volumes:
      - ./neo4j/plugins:/plugins
    environment:
      NEO4J_AUTH: neo4j/test
      NEO4J_dbms_logs_debug_level: DEBUG
      NEO4J_dbms.default_advertised_address: "0.0.0.0"
      NEO4J_dbms_connector_bolt_advertised__address: "0.0.0.0"

JonasHiltl avatar Dec 08 '21 20:12 JonasHiltl

Hi, can you share the relevant Node.js code as well? I personally do not use Compose very often, the only difference I see with one sample I have around is that I configure the "lan" network, not "basenet".

fbiville avatar Dec 15 '21 09:12 fbiville

I'm using neogma as an orm which uses the js driver under the hood and I just pass the environment variables in the constructor of neogma.

import { Neogma } from 'neogma';

export const neogma = new Neogma({
  url: process.env.NEO4J_URL!,
  username: process.env.NEO4J_USER!,
  password: process.env.NEO4J_PASSWORD!,
});

JonasHiltl avatar Dec 15 '21 16:12 JonasHiltl

Hi @JonasHiltl,

  • which operational system are you using?
  • if you're in macos or windows, how you docker is setup? Are you using docker desktop, minikube or other solution?
  • Is your application running the docker-compose environment or in the localhost?

Sorry for the late reply with a lot questions, but I'd like to understand the network/docker setup.

bigmontz avatar Dec 28 '21 16:12 bigmontz

I'm using WSL 2 and using Docker Desktop. I have defined my applications in a docker-compose file. Below is the neo4j image and my Node application both running through docker-compose

version: "3.8"

services:
  profile:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        - BUILD_CONTEXT=profile-service
    container_name: profile
    networks:
      - basenet
    depends_on:
      - neo4j
    ports:
      - 3001:3000
    volumes:
      - ./apps/profile-service/dist:/base/apps/profile-service/dist
      - ./apps/profile-service/src:/base/apps/profile-service/src
      - /base/profile-service/node_modules
    command: yarn dev
    environment:
      FASTIFY_PORT: 3000
      NEO4J_URL: bolt://neo4j:7687
      NEO4J_USER: neo4j
      NEO4J_PASSWORD: test

  neo4j:
    image: neo4j:latest
    container_name: neo4j
    networks:
      - basenet
    ports:
      - 7474:7474
      - 7687:7687
    expose:
      - 7474:7474
      - 7687:7687
    restart: on-failure
    volumes:
      - ./docker-data/neo4j/plugins:/plugins
    environment:
      NEO4J_AUTH: neo4j/test
      NEO4J_dbms_logs_debug_level: DEBUG

networks:
  basenet:
    name: "sessions-network"

The Node app just can't connect to Neo4j through bolt or the normal neo4j connection prefix.

JonasHiltl avatar Dec 29 '21 21:12 JonasHiltl

@JonasHiltl,

I've tried your docker-compose configuration and I could not ping neo4j because it could not resolve the name neo4j. It seems to be an issue in the network/docker setup. Could you try to ping the neo4j container from your application container?

bigmontz avatar Jan 03 '22 09:01 bigmontz

I am able to ping the neo4j container with docker compose exec profile ping neo4j

JonasHiltl avatar Jan 04 '22 13:01 JonasHiltl

Are you able to telnet this neo4j using port 7687 from inside the app container? Try to wget/curl http://neo4j:7474 also could help.

The error seems to be in the socket level,

bigmontz avatar Jan 12 '22 13:01 bigmontz

@JonasHiltl,does this problem still happening?

bigmontz avatar Sep 15 '22 13:09 bigmontz

@bigmontz I'm currently not working with the client anymore. So you could close this.

JonasHiltl avatar Sep 15 '22 15:09 JonasHiltl