Anurag Sharma
Anurag Sharma

Reputation: 5039

Problem loading neo4j dump in docker container

I am using docker to launch multiple instances of neo4j instances for my project. I have a requirement that I have to load a graph dump to one of my neo4j docker container and I have to do it again an again (as the graph dump will be provided by a separate group of people).

These are my steps that I have followed in order to do the same -

    # docker run --publish=7474:7474 --publish=7687:7687 \
    --volume=/home/dimension/neo4j/container3/data:/data \
    --volume=/home/dimension/neo4j/container3/logs:/logs \
    --volume=/home/dimension/neo4j/container3/conf:/conf \
    --volume=/home/dimension/neo4j/container3/plugins:/plugins \
    neo4j:3.3.3

    # docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS                                                      NAMES
eee581b2f493        neo4j:3.3.3         "/docker-entrypoint.…"   2 hours ago         Up 2 seconds               0.0.0.0:7474->7474/tcp, 7473/tcp, 0.0.0.0:7687->7687/tcp   priceless_ride

This brings up the container, but the database is empty. Next I tried the following to load a neo4j dump to my newly built docker container

# docker stop priceless_ride
priceless_ride

# cp home/dimension/neo4j/dumps/2018-09-05.dump ~/neo4j/container3/data/

# docker run --publish=7474:7474 --publish=7687:7687 \
--volume=/home/dimension/neo4j/container3/data:/data \
--volume=/home/dimension/neo4j/container3/logs:/logs \
--volume=/home/dimension/neo4j/container3/conf:/conf \
--volume=/home/dimension/neo4j/container3/plugins:/plugins \
-i -t neo4j:3.3.3 /bin/bash

The above command creates a separate container and runs the container in interactive mode. Once I am inside the container I run -

bash-4.4# bin/neo4j-admin load --from=/data/2018-09-05.dump --database=graph.db --force
bash-4.4# exit

Now I execute docker ps -a command, I will see 2 container in the output

docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
073c31c75ff5        neo4j:3.3.3         "/docker-entrypoint.…"   8 seconds ago       Exited (0) 3 seconds ago                       focused_zhukovsky
eee581b2f493        neo4j:3.3.3         "/docker-entrypoint.…"   3 hours ago         Exited (0) 7 minutes ago                       priceless_ride

I have to remove the newly built container as it is no longer needed. by executing docker rm focused_zhukovsky

Every thing works fine, when I restarted my container by executing command

# docker start priceless_ride

and check the browser using http://127.0.0.1:7474/browser/, the container now has the loaded database form the dump.

Following the above steps for loading a graph dump is okay, but its cumbersome if I have to do it again and again. Is their a cleaner way to load neo4j dump without the need to start a separate container in interactive mode and then firing neo4j-admin load command.

Is it possible to start the docker container without running the neo4j inside it. If this is possible then I can go inside the container and fire the neo4j-admin load command as many time I want.

I am stuck at this problem from quite some time. I will deeply appreciate any help on this issue.

Upvotes: 3

Views: 2803

Answers (2)

Himanshu Jain
Himanshu Jain

Reputation: 1818

One simple and quick way I can think of right now is to override the default neo4j container command something like this:

docker run  -d --publish=7474:7474 --publish=7687:7687 --volume=$HOME/neo4j/datanew:/data neo4j /bin/bash -c "[ -f /data/db.dump ] && /var/lib/neo4j/bin/neo4j-admin load --from=/data/db.dump --database=graph.db --force; neo4j console"

You use the -c command with /bin/bash to execute multiple commands. The [ -f /data/db.dump ] checks if the db dump exists and if does it runs the neo4j dump import command. After this neo4j is started.

Considering that you have a database dump named db.dump in your $HOME/datanew directory neo4j-admin should be able to load it up before starting the neo4j server. Stopping it, replacing the dump with a new one and starting it back up again should bring up the new database.

Pretty sure the same thing can be done if you create your docker image based on the neo4j image and add the command in the end which will do the exact same thing. That should be much cleaner.

Upvotes: 2

Vikas Kumar
Vikas Kumar

Reputation: 159

It seems you are using Neo4j Community Edition rather than Enterprise Edition hence You are unable to load db dump at run time. This feature is exclusively in Enterprise Edition so in community edition you may need to tweak it.

So before starting neo4j container copy your dump in the path which you are going to mount on container and load it on docker run

# cp home/dimension/neo4j/dumps/2018-09-05.dump ~/neo4j/container3/data/

# docker run --publish=7474:7474 --publish=7687:7687 \
    --volume=/home/dimension/neo4j/container3/data:/data \
    --volume=/home/dimension/neo4j/container3/logs:/logs \
    --volume=/home/dimension/neo4j/container3/conf:/conf \
    --volume=/home/dimension/neo4j/container3/plugins:/plugins \
    -it neo4j:3.3.3 /bin/bash -c "[ -f /data/db.dump ] \
    && /var/lib/neo4j/bin/neo4j-admin load --from=/data/db.dump \
    --database=graph.db --force

Upvotes: 3

Related Questions