How do you perform a dump of a Neo4j database within a Docker container?

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?

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

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.

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/

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