Create custom Neo4j Docker image with intial data

2019-07-15 11:53发布

问题:

I tried create an docker image of neo4j that already provide some data, when you start an container. For my approach I inherited from the neo4j docker image, added some data via the neo4j cypher shell. But when i build the image and run a container from it the data did not appear in the database but the custom password is set. This is my current dockerfile:

From neo4j:3.4
ENV NEO4J_AUTH=neo4j/password
COPY data.cypher /var/lib/neo4j/import/
USER neo4j
RUN bin/neo4j-admin set-initial-password password || true && \
    bin/neo4j start && sleep 5 && \
    cat /var/lib/neo4j/import/data.cypher | NEO4J_USERNAME=neo4j NEO4J_PASSWORD=password /var/lib/neo4j/bin/cypher-shell --fail-fast
CMD [neo4j]

I added also an match query to the data.cypher file to make sure that the shell added the data to neo4j. Maybe it has something to do that /data is defined as volume in the neo4j image?

回答1:

I was using Bierbarbar's approach. I got it working after getting over the following two pitfalls:

Firstly, $NEO4J_HOME/data was symlinked to /data, which seem to have permission issues. Changing the default data folder: by adding dbms.directories.data=mydata line to $NEO4J_HOME/conf/neo4j.conf fixed this.

Secondly, make sure data.cypher file contains correct format for cypher-shell: 1) Semicolon is needed at the end of each cypher statemnt; 2) there are :begin and :commit commands in some versions (or all versions?) of cypher-shell



回答2:

In your Dockerfile, these commands happen at Docker container build time. At that time, there is no neo4j docker container, and so calls to cypher-shell and data import have no chance to succeed. So this overall approach just won't work.

What I would recommend is you build your database locally with something like Neo4j Desktop, then load the data into that. Then take a copy of your graph.db folder that resulted from the loading. And attach that to a docker container, no new Dockerfile needed.

Additionally, there are configuration options to set the default password from the outside, so you also don't need the neo4j-admin part.