9

A docker container for Neo4j is started as per the documentation and working properly using the following command:

$ docker run \
    --detach \
    --publish=7474:7474 \
    --publish=7473:7473 \
    --publish=7687:7687 \
    --volume=$HOME/neo4j/data:/data \
    --volume=$HOME/neo4j/logs:/logs \
    --volume=$HOME/neo4j/ssl:/ssl \
    --ulimit=nofile=40000:40000 \
    --name=myname-neo4j \
    neo4j:3.1.1

When I attempt to perform a neo4j-admin dump of the database I get an error:

$ docker exec -ti myname-neo4j bin/neo4j-admin dump --database=graph.db --to=/home/name/myname.dump
command failed: the database is in use -- stop Neo4j and try again

However, if the Neo4j process is stopped, which seems to be the only way to free the database, the container closes. This appears to be the expected behavior from Docker. Therefore, it appears to be impossible to call neo4j-admin dump from within the container without the database being in use.

How can this be resolved while still using Docker?

0

4 Answers 4

7

1: Stop the container.

docker stop myname-neo4j

2: Remove the container

docker rm myname-neo4j

3: Run the container as interactive mode (-it) without the option (detach) and executing the shell ( /bin/bash ).

docker run \
--publish=7474:7474 \
--publish=7473:7473 \
--publish=7687:7687 \
--volume=$HOME/neo4j/data:/data \
--volume=$HOME/neo4j/logs:/logs \
--volume=$HOME/neo4j/ssl:/ssl \
--ulimit=nofile=40000:40000 \
--name=myname-neo4j \
-it \
neo4j:3.1.1 \
-c /bin/bash

Now you are inside the neo4j container without running Neo.

4: Check that neo is not up by visiting the URI endpoint in (http://yourhost:7474). You should see the "Unable to connect" message.

5: Dump your database

docker exec -ti myname-neo4j bin/neo4j-admin dump --database=graph.db --to=/home/name/myname.dump
3
  • For step 5, one can either perform then command as you stated it from a 2nd session outside the container. Or one can simply omit docker exec -ti myname-neo4j and run it from the bash prompt inside the container.
    – Gregyski
    Commented Mar 17, 2017 at 18:37
  • 1
    what if container is already running? I will lose all data I want to backup if I stop it
    – vladkras
    Commented Jul 14, 2017 at 10:24
  • It is strongly recommended to mount a volume for your data so that it can be persistent and independent of your neo4j container lifespan. (see: docs.docker.com/engine/tutorials/dockervolumes). If you didn't do that then do not use the -rm flag in the "docker run " command. Stop the container, commit the changes with docker commit and proceed to step 4. Or simply commit your docker container in an external shell before stopping it.
    – Juan
    Commented Jul 14, 2017 at 11:04
4

I did this:

docker stop [neo4j container]    
docker run --name dump --entrypoint="/bin/bash" -it -v $HOME/neo4j/data:/data neo4j:3.1.1 -c "neo4j-admin dump --to=/data/db.dump"    
docker start [neo4j container]

Then you can either keep the "dump" container and reuse it, or just remove it. Same concept with the entrypoint and -c parameter would apply for the load procedure.

1

By coincidence it's now much easier to do this in Neo4j 4.0 because you can stop and start databases without having to shut down the whole Docker container.

So if we wanted to take a dump of a database called 'foo', we'd do the following:

STOP DATABASE foo

docker exec -it our-neo4j-container neo4j-admin dump --database=foo --to=/tmp/foo.db.dump

docker cp our-neo4j-container:/tmp/foo.db.dump .

START DATABASE foo

Also written up as a blog post - https://markhneedham.com/blog/2020/01/28/neo4j-database-dump-docker-container/

1
  • 1
    Note: does not work for Community edition.
    – m4rlo
    Commented Apr 14, 2021 at 19:38
0

I had the same issue before, so I wrote this workaround to dump neo4j data and pull it outside the container to the host machine.

docker rm --force neo4j-dump

docker run \
--name neo4j-dump \
--env-file /storage/bin/.neo4j.env \
--mount type=bind,source=<neo4j_data_folder>,target=/data \
neo4j:3.1.1 bin/neo4j-admin dump --database=graph.db --to=/graph.db.dump

docker cp `docker ps -aqf "name=neo4j-dump"`:/graph.db.dump <target_dump_file>

docker rm --force neo4j-dump

This will create a new container and dump data instead of starting neo4j service, then copy the dump to the host, just update and to yours

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .