Bash 如何在启动neo4j docker容器时运行cypher脚本? 背景
我以前在Windows上安装了neo4j,并使用PowerShell脚本来运行一些迁移。每次都会从这些迁移脚本和导入文件夹中的一些CSV中重新创建数据库。NETWebAPI与Neo4JDB对话 目标 我决定将此设置归档,这样我就可以与其他人跨平台协作,而不必直接安装/配置neo4j 我已经设置了neo4j docker容器的大部分内容——卷、复制的适当文件等,然后它就启动了 问题 不管怎样,我似乎找不到一种插入或执行脚本的好方法,该脚本将在文件夹中循环并执行密码查询。我知道这可能必须是一个使用neo4j CLI的bash脚本,我对此很满意,但我找不到一个好的地方来实现它 我试过的Bash 如何在启动neo4j docker容器时运行cypher脚本? 背景,bash,docker,neo4j,docker-compose,Bash,Docker,Neo4j,Docker Compose,我以前在Windows上安装了neo4j,并使用PowerShell脚本来运行一些迁移。每次都会从这些迁移脚本和导入文件夹中的一些CSV中重新创建数据库。NETWebAPI与Neo4JDB对话 目标 我决定将此设置归档,这样我就可以与其他人跨平台协作,而不必直接安装/配置neo4j 我已经设置了neo4j docker容器的大部分内容——卷、复制的适当文件等,然后它就启动了 问题 不管怎样,我似乎找不到一种插入或执行脚本的好方法,该脚本将在文件夹中循环并执行密码查询。我知道这可能必须是一个使用n
env变量。这在过程中执行得太早了EXTENSION\u脚本
- 使用我自己的
——发现它似乎取代了neo4j容器的入口点入口点
- 使用我自己的
——类似地,这似乎取代了CMD
- 从
移动到docker compose
并复制neo4j入口点文件进行修改。这似乎遇到了一个问题,出现了一个我正在研究的错误dockerfile
,但这是我的第一步无效选项n/bash:-
FROM neo4j:3.3.1
COPY ./data/import/migrations/scripts /scripts
ENV NEO4J_AUTH=none
ENTRYPOINT ["/scripts/docker-neo4j-entrypoint.sh"]
CMD ["neo4j"]
来自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
我也遇到了同样的问题,我想在启动时创建一些索引,并能够根据文档解决它,创建一个包装器脚本和一个索引脚本,该脚本在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"]
wrapper.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
create-index.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
我还将此作为一个问题打开,并将我的答案复制到其中,然后关闭。意识到我已经解决了这个问题——某种程度上——并想在这里发布我当前的工作解决方案。我不打算将我自己的答案标记为解决方案,因为我相信这里的另一个解决方案也是可行的,我不想完全相信 总的组成部分是:
- docker使用环境变量(是否运行迁移以及等待多长时间)编写文件
- 包含Neo4j客户端的我的WebAPI项目的
Dockerfile
- 运行迁移的bash入口点脚本
version: '3'
services: # these are all the services that a docker app uses
api: # this is the name of the service we're creating; it's chosen by us. Here, we're calling it "api".
container_name: 'redacted-api' # this is the name of the container to us
depends_on:
- neo4j
build:
context: .
dockerfile: DOCKERFILE_API
environment:
- SECONDS_PAUSE_BEFORE_MIGRATION=15
- RUN_MIGRATIONS=true
ports:
- "5000:10901"
networks:
- app-network # this is a docker feature to allow you to place your various services within a virtual network so they can talk to each other. Note all the services we define here use the "app-network" network.
neo4j:
image: neo4j:3.3.1-enterprise
container_name: 'redacted-db'
environment:
- NEO4J_AUTH=none
- NEO4J_dbms_security_procedures_unrestricted=apoc.*
- NEO4J_ACCEPT_LICENSE_AGREEMENT=yes
volumes:
- ./data/db-files:/data
- ./data/CSVs:/import
- ./data/import/migrations:/import/migrations
- ./data/plugins:/plugins
ports:
#TODO: Remove these so that the API can get to them but not the host? (security)
- "7687:7687" # bolt protocol
- "7474:7474" # http protocol
- "7473:7473" # https protocol
networks:
- app-network
networks:
app-network:
driver: bridge
API Dockerfile
在安装my WebAPI的容器上安装cypher shell
neo4j实用程序:
FROM microsoft/dotnet:2.1-sdk
# Add the neo4j repo to the packages list
RUN wget -O - https://debian.neo4j.org/neotechnology.gpg.key | apt-key add -
RUN echo 'deb http://debian.neo4j.org/repo stable/' | tee -a /etc/apt/sources.list.d/neo4j.list
# Grab the packages list & install neo4j
RUN apt-get update && apt-get install -y \
cypher-shell
COPY . /app
WORKDIR /app/src/Redacted.API
RUN ["dotnet", "build"]
EXPOSE 10901/tcp
ENTRYPOINT ["sh", "/app/entrypoint.sh"]
入口点脚本
这在Web API的容器上运行,循环执行迁移文件夹中的任何cypher查询,并针对Neo4j容器执行它们
#/bin/bash
如果[“$RUN_MIGRATIONS”=“true”];然后
echo“暂停$SECONDS\u暂停\u然后再\u迁移秒,让Neo4j预热”
休眠$SECONDS\u在\u迁移之前暂停\u
回显“运行迁移”
用于/app/data/import/migrations/*.cypher中的文件名;做
echo“正在尝试运行密码:$filename”
内容=$(目录$filename)
密码壳-abolt://redacted-db:7687 “$contents”
完成
其他的
echo“正在跳过迁移,因为RUN_migrations未设置为true。”
fi
echo“启动.NET WebAPI”
网络运行
最终结果
- DB容器首先启动
- API依赖于Neo4j,因此它会等待容器启动配置的时间
- API然后运行所有迁移
最后我不得不把它放在一边,但接下来我要做的是找出如何将Dockerfile/migrations/entrypoint完全放在Neo4j容器中,我想,如果我没有找到这种对我来说足够有效的方法,我本可以做得更细致一些。我创建了一个docker映像,可以与这里和github问题中的包装器重构版本一起重用 图像
marcellodesales/neo4j,含香附种子
- Docker图像位于
- 使用协议
对数据进行种子设定bolt://
不工作cypher shell-f文件
- 任何包含cypher内容的文件,如
interviews.cql
- 将密码文件装入卷
/cyphers
- 让服务器加载密码
- 结果如下
仅供参考,我目前正在实施使用API入口点为数据库种子的策略。好吧,看看结果如何。可能是@SeanKilleen的复制品很高兴你喜欢它。。。我花了几个小时试图理解是什么导致了这些错误,直到我检查了您的包装器实现。。。我已经打开了在直接使用cyper_shell和-F文件时发现的bug。。在这种情况下,感谢您提出了第一个想法!
CREATE (facebook:Company {name:'Facebook'})
CREATE (clement:Candidate {name:'Clement'})
CREATE (antoine:Candidate {name:'Antoine'})
CREATE (simon:Candidate {name:'Simon'})
docker run --rm -ti \
-e "NEO4J_AUTH=none" \
-p "7474:7474" -p "7687:7687" \
-v $(pwd)/cypher_query.cql:/cyphers/interviews.cql \
marcellodesales/neo4j-with-cypher-seed
$ docker run --rm -ti \
-e "NEO4J_AUTH=none" \
-p "7474:7474" -p "7687:7687" \
-v $(pwd)/cypher_query.cql:/cyphers/interviews.cql \
marcellodesales/neo4j-with-cypher-seed
2020-09-27 16:54:00:486+0000 INFO Wrapper: Waiting until neo4j stats at :7474 ...
Directories in use:
home: /var/lib/neo4j
config: /var/lib/neo4j/conf
logs: /logs
plugins: /var/lib/neo4j/plugins
import: /var/lib/neo4j/import
data: /var/lib/neo4j/data
certificates: /var/lib/neo4j/certificates
run: /var/lib/neo4j/run
Starting Neo4j.
2020-09-27 16:54:01.394+0000 INFO Starting...
2020-09-27 16:54:03.064+0000 INFO ======== Neo4j 4.1.2 ========
2020-09-27 16:54:04.332+0000 INFO Initializing system graph model for component 'security-users' with version -1 and status UNINITIALIZED
2020-09-27 16:54:04.337+0000 INFO Setting up initial user from defaults: neo4j
2020-09-27 16:54:04.338+0000 INFO Creating new user 'neo4j' (passwordChangeRequired=true, suspended=false)
2020-09-27 16:54:04.345+0000 INFO Setting version for 'security-users' to 2
2020-09-27 16:54:04.349+0000 INFO After initialization of system graph model component 'security-users' have version 2 and status CURRENT
2020-09-27 16:54:04.354+0000 INFO Performing postInitialization step for component 'security-users' with version 2 and status CURRENT
2020-09-27 16:54:04.513+0000 INFO Bolt enabled on 0.0.0.0:7687.
2020-09-27 16:54:05.410+0000 INFO Remote interface available at http://localhost:7474/
2020-09-27 16:54:05.411+0000 INFO Started.
2020-09-27 16:54:05:619+0000 INFO Wrapper: Deleting all relations
2020-09-27 16:54:08:274+0000 INFO Wrapper: Wrapper: Loading cyphers from '/cyphers'
2020-09-27 16:54:08:275+0000 INFO Wrapper: Running cypher /cyphers/interviews.cql
0 rows available after 330 ms, consumed after another 0 ms
Added 17 nodes, Created 17 relationships, Set 34 properties, Added 17 labels
2020-09-27 16:54:09:695+0000 INFO Wrapper: Finished loading all cyphers from '/cyphers'
2020-09-27 16:54:10:842+0000 INFO Wrapper: Wrapper: Changes count 17
/docker-entrypoint.sh neo4j