简体   繁体   English

Docker-compose:使用 bolt 从 web 容器到 neo4j 容器的数据库连接

[英]Docker-compose: db connection from web container to neo4j container using bolt

I'm working on django project with neo4j db using neomodel and django-neomodel.我正在使用neomodel和django-neomodel使用neo4j db处理django项目。 I'm trying to containerize it using docker-compose.我正在尝试使用 docker-compose 将其容器化。 when I build the images everything seems fine, but any connection from web container to db using bolt is refused.当我构建图像时,一切似乎都很好,但是使用 bolt 从 web 容器到 db 的任何连接都被拒绝。 although I can access the neo4j db from the browser on http, and even from local machine on bolt.虽然我可以从 http 上的浏览​​器访问 neo4j db,甚至可以从 Bolt 上的本地机器访问。 this is the error I get:这是我得到的错误:

neo4j.exceptions.ServiceUnavailable: Failed to establish connection to ('127.0.0.1', 7688) (reason 111)

I'm using the following configs:我正在使用以下配置:

<pre>Django == 3.1.1
neo4j==4.1.0
neomodel==3.3.0
neobolt==1.7.17 </pre>

this is my docker-compose file:这是我的 docker-compose 文件:

version: '3'

services:
  backend:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/app
    ports:
      - "8000:8000"
    depends_on:
      - neo4j_db
    networks:
      - mynetwork
    links:
      - neo4j_db


  neo4j_db:
    image: neo4j:3.5.17-enterprise
    ports:
      - "7474:7474"
      - "7688:7687"
    expose:
      - 7474
      - 7687
    volumes:
      - ./db/dbms:/data/dbms
    environment:
      - NEO4J_ACCEPT_LICENSE_AGREEMENT=yes
      - dbms.connector.bolt.listen_address=:7688
      - dbms.connector.bolt.advertised_address=:7688
    networks:
      - mynetwork
networks:
  mynetwork:
    driver: bridge

and here's connection configs in django settings:这是django设置中的连接配置:

NEOMODEL_NEO4J_BOLT_URL = os.environ.get('NEO4J_BOLT_URL', 'bolt://neo4j:pass@123@127.0.0.1:7688')

Thanks in advance..提前致谢..

  1. To connect from one container to another one (inside the same docker-compose project) you should use container name of the target container instead of the localhost (or 127.0.0.1).要从一个容器连接到另一个容器(在同一个 docker-compose 项目中),您应该使用目标容器的容器名称而不是 localhost(或 127.0.0.1)。 In your case it would be neo4j_db .在您的情况下,它将是neo4j_db

  2. When connecting from other container you should use the internal port, in your case 7687.从其他容器连接时,您应该使用内部端口,在您的情况下为 7687。

  3. In the neo4j service, the bolt.listen_address should be 7687 instead of 7688 (honestly, I'm not sure why you are changing the default port).在 neo4j 服务中, bolt.listen_address应该是 7687 而不是 7688(老实说,我不确定您为什么要更改默认端口)。

To wrap up, the connection url should be:总结一下,连接 url 应该是:

bolt://neo4j:pass@neo4j_db:7687

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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