如何在启动neo4j docker容器时运行cypher脚本?

Sea*_*een 11 bash neo4j docker docker-compose

背景

我以前在Windows上安装了neo4j,并运行了PowerShell脚本来运行一些迁移.每次从导入文件夹中的这些迁移脚本和某些CSV重新创建数据库..NET WebAPI与neo4j db进行通信.

目标

我决定Dockerize这个设置,以便我可以与人们进行跨平台协作,他们不必直接安装/配置neo4j.

我已经设置了大部分neo4j docker容器 - 卷,适当的文件复制等等,它启动了.

问题

无论如何,我似乎找不到插入或执行将循环遍历文件夹并执行密码查询的脚本的好方法.我知道这可能是一个使用neo4j CLI的bash脚本,我对此很好,但我找不到一个让它成为现实的好地方.

我试过的

  • EXTENSION_SCRIPT环境变量.这在过程中过早地执行.
  • 使用我自己ENTRYPOINT- 发现这似乎取代了neo4j容器的入口点
  • 使用我自己CMD- 同样,这似乎取代了
  • 移动docker-composedockerfile和复制Neo4j的入口点文件来修改它.这似乎遇到了一个invalid optionn/bash: -我正在研究过程中出现错误的问题,但这是我的第一件事.

在Neo4j启动后如何运行一个或多个密码查询?在neo4j或docker中是否有规定允许这样做?我无法在文档中找到任何线索.

或者,这真的不是推荐的方式吗?我是否应该通过输入容器并手动运行与CLI一起使用的bash脚本来按需运行这些迁移?

脚本

Dockerfile:

FROM neo4j:3.3.1

COPY ./data/import/migrations/scripts /scripts

ENV NEO4J_AUTH=none

ENTRYPOINT ["/scripts/docker-neo4j-entrypoint.sh"]
CMD ["neo4j"]
Run Code Online (Sandbox Code Playgroud)

来自的相关摘录docker-compose:

  neo4j:
    container_name: 'app-db'
    build:
      context: .
      dockerfile: DOCKERFILE_DB
    volumes:
      - ./data/CSVs:/import
      - ./data/import/migrations:/import/migrations
    ports: 
      - "7687:7687" # bolt protocol
      - "7474:7474" # http protocol
      - "7473:7473" # https protocol
    networks:
      - app-network
Run Code Online (Sandbox Code Playgroud)

jav*_*y79 3

我遇到了同样的问题,我想在启动时创建一些索引,并且能够根据此处的文档解决它以创建一个包装器脚本,以及一个仅休眠直到 Neo4j 备份的索引脚本,如下所示:

Dockerfile

FROM neo4j:latest

ENV NEO4J_AUTH=neo4j/password

RUN apk add --no-cache --quiet procps

COPY create-indexes.sh create-indexes.sh
COPY wrapper.sh wrapper.sh

ENTRYPOINT ["./wrapper.sh"]
Run Code Online (Sandbox Code Playgroud)

包装器.sh:

#!/bin/bash

# turn on bash's job control
set -m

# Start the primary process and put it in the background
/docker-entrypoint.sh neo4j &

# Start the helper process
./create-indexes.sh

# the my_helper_process might need to know how to wait on the
# primary process to start before it does its work and returns


# now we bring the primary process back into the foreground
# and leave it there
fg %1
Run Code Online (Sandbox Code Playgroud)

创建索引.sh

#!/bin/bash

until cypher-shell -u neo4j -p shaun123 'CREATE INDEX ON :Page(url);'
do
  echo "create page index failed, sleeping"
  sleep 10
done

until cypher-shell -u neo4j -p shaun123 'CREATE INDEX ON :Visited(url);'
do
  echo "create visited index failed, sleeping"
  sleep 10
done

until cypher-shell -u neo4j -p shaun123 'CREATE INDEX ON :Anchor(url);'
do
  echo "create anchor index failed, sleeping"
  sleep 10
done
Run Code Online (Sandbox Code Playgroud)

我也将其作为问题在这里打开,我已将我的答案复制到其中并关闭。