Docker specific operations

This chapter describes how to use Neo4j tools when running Neo4j in a Docker container.

This section covers the following:

1. Use Neo4j Admin

The Neo4j Admin tool can be run locally within a container using the following command:

docker exec --interactive --tty <containerID/name> neo4j-admin <command>

To determine the container ID or name, run docker ps to list the currently running Docker containers.

For more information about the neo4j-admin commands, see Neo4j Admin.

2. Use Neo4j Import

The Neo4j Import tool can be run locally within a container using the following command:

docker exec --interactive --tty <containerID/name> neo4j-admin import <options>

For more information about the neo4j-admin import syntax and options, see Syntax and Options.

Prerequisites

  • Verify that you have created the folders that you want to mount as volumes to the Neo4j docker container.

  • Verify that the CSV files that you want to load into Neo4j are formatted as per CSV file header format.

  • Verify that you have added the CSV files to the folder that will be mounted to /import in your container.

Import CSV files into the Neo4j Docker container using the Neo4j import tool

This is an example of how to start a container with mounted volumes /data and /import, to ensure the persistence of the data in them, and load the CSV files using the neo4j-admin import command. You can add the flag --rm to automatically remove the container’s file system when the container exits.

docker run --interactive --tty --rm \
    --publish=7474:7474 --publish=7687:7687 \
    --volume=$HOME/neo4j/data:/data \
    --volume=$HOME/neo4j/import:/import \
    --user="$(id -u):$(id -g)" \
    neo4j:{neo4j-version} \
neo4j-admin import --nodes=Movies=/import/movies_header.csv,/import/movies.csv \
--nodes=Actors=/import/actors_header.csv,/import/actors.csv \
--relationships=ACTED_IN=/import/roles_header.csv,/import/roles.csv

3. Use Cypher Shell

The Neo4j Cypher Shell tool can be run locally within a container using the following command:

docker exec --interactive --tty <containerID/name> cypher-shell <options>

For more information about the cypher-shell syntax and options, see Syntax.

3.1. Retrieve data from a database in a Neo4j Docker container

The following is an example of how to use the cypher-shell command to retrieve data from the neo4j database.

  1. Run a new container, mounting the same volume /data as in the import example.

    docker run --interactive --tty --name <container name> \
        --publish=7474:7474 --publish=7687:7687 \
        --volume=$HOME/neo4j/data:/data \
        --user="$(id -u):$(id -g)" \
        neo4j:{neo4j-version}
  2. Use the container ID or name to get into the container, and then, run the cypher-shell command and authenticate.

    docker exec --interactive --tty <containerID/name> cypher-shell -u neo4j -p neo4j
  3. Retrieve some data.

    neo4j@neo4j> match (n:Actors)-[r]->(m:Movies) return n.name AS Actors, m.title AS Movies, m.year AS MovieYear;
    +-------------------------------------------------------------+
    | Actors               | Movies                   | MovieYear |
    +-------------------------------------------------------------+
    | "Keanu Reeves"       | "The Matrix Revolutions" | 2003      |
    | "Keanu Reeves"       | "The Matrix Reloaded"    | 2003      |
    | "Keanu Reeves"       | "The Matrix"             | 1999      |
    | "Laurence Fishburne" | "The Matrix Revolutions" | 2003      |
    | "Laurence Fishburne" | "The Matrix Reloaded"    | 2003      |
    | "Laurence Fishburne" | "The Matrix"             | 1999      |
    | "Carrie-Anne Moss"   | "The Matrix Revolutions" | 2003      |
    | "Carrie-Anne Moss"   | "The Matrix Reloaded"    | 2003      |
    | "Carrie-Anne Moss"   | "The Matrix"             | 1999      |
    +-------------------------------------------------------------+
    
    9 rows available after 61 ms, consumed after another 7 ms

3.2. Pass a Cypher script file to a Neo4j Docker container

Because cypher-shell does not support file streams, such as cypher-shell -a localhost -u <username> -p <password> --file myscript.cypher, you can use the commands cat or curl to pipe the contents of your script file into your container. The following are syntax examples of how to use these commands:

Invoke curl with Cypher Shell
curl http://mysite.com/config/script.cypher | sudo docker exec --interactive <containerID/name> cypher-shell -u neo4j -p neo4j
Invoke cat with Cypher Shell
# Prepare the example.cypher script, for instance, containing the query:

match (n:Actors)-[r]->(m:Movies) return n.name AS Actors, m.title AS Movies, m.year AS MovieYear;

# Run the following command:

cat example.cypher | sudo  docker exec --interactive  <containerID/name> cypher-shell -u neo4j -p neo4j

# The command runs the script and returns the following result:

Actors, Movies, MovieYear
"Keanu Reeves", "The Matrix Revolutions", 2003
"Keanu Reeves", "The Matrix Reloaded", 2003
"Keanu Reeves", "The Matrix", 1999
"Laurence Fishburne", "The Matrix Revolutions", 2003
"Laurence Fishburne", "The Matrix Reloaded", 2003
"Laurence Fishburne", "The Matrix", 1999
"Carrie-Anne Moss", "The Matrix Revolutions", 2003
"Carrie-Anne Moss", "The Matrix Reloaded", 2003
"Carrie-Anne Moss", "The Matrix", 1999

These commands take the contents of the script file and pass it into the Docker container. Then, they run cypher-shell, authenticate with the provided <username> and <password>, and execute a cypher example, LOAD CSV dataset, which might be hosted somewhere on a server (with curl), create indexes, constraints, or do other administrative operations.

4. Install user-defined procedures

To install user-defined procedures, mount the /plugins volume containing the jars.

docker run --publish=7474:7474 --publish=7687:7687 --volume=$HOME/neo4j/plugins:/plugins neo4j:4.1

5. Configure Neo4j Labs plugins

The Neo4j Docker image includes a startup script which can automatically download and configure certain Neo4j plugins at runtime.

This feature is intended to facilitate using Neo4j Labs plugins in development environments, but it is not recommended for use in production environments.

To use plugins in production with Neo4j Docker containers, see Install user-defined procedures.

The NEO4JLABS_PLUGINS environment variable can be used to specify the plugins to install using this method. This should be set to a JSON-formatted list of supported plugins.

For example, to install both the APOC and GraphQL plugins, you can use the Docker argument --env NEO4JLABS_PLUGINS='["apoc","graphql"]' and run the following command:

docker run -it --rm \
  --publish=7474:7474 --publish=7687:7687 \
  --user="$(id -u):$(id -g)" \
  -e NEO4J_AUTH=none \
  --env NEO4JLABS_PLUGINS='["apoc","graphql"]' \
  neo4j:{neo4j-version}

If a single plugin is required, it must still be specified as a JSON-formatted list. For example, to install the Neo Semantics n10s plugin, you can use the following Docker argument:

--env NEO4JLABS_PLUGINS='["n10s"]'
Table 1. Supported Neo4j Labs plugins
Name Key Further information

APOC

apoc

Graph Data Science

graph-data-science

GraphQL

graphql

Neo Semantics

n10s

Streams

streams

Graph-algorithms

graph-algorithms