Docker 特定操作

当在 Docker 容器中运行 Neo4j 时,您可以使用 Neo4j 工具。

使用 Neo4j Admin

neo4j-admin CLI 可以使用以下命令在容器内本地运行

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

要确定容器 ID 或名称,请运行 docker ps 以列出当前正在运行的 Docker 容器。

有关 neo4j-admin 命令的更多信息,请参阅 Neo4j Admin 和 Neo4j CLI

使用 Neo4j Import

Neo4j Import 工具可以使用以下命令在容器内本地运行

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

以及

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

有关命令语法和选项的更多信息,请参阅 完整导入增量导入

先决条件

  • 请确认您已创建要作为卷挂载到 Neo4j Docker 容器中的文件夹。

  • 请确认要加载到 Neo4j 中的 CSV 文件已按照 CSV 标题格式进行格式化。

  • 请确认您已将 CSV 文件添加到将挂载到容器中 /import 的文件夹中。

使用 Neo4j Import 工具将 CSV 文件导入 Neo4j Docker 容器

这是一个如何启动带有挂载卷 /data/import 的容器的示例,以确保其中的数据持久化,并使用 neo4j-admin database import full 命令加载 CSV 文件。您可以添加 --rm 标志,以便在容器退出时自动删除容器的文件系统。

docker run --interactive --tty --rm \
    --publish=7474:7474 --publish=7687:7687 \
    --volume=$HOME/neo4j/data:/data \
    --volume=$HOME/neo4j/import:/import \
    neo4j:2026.03.1 \
neo4j-admin database import full --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 databasename

使用 Neo4j Admin 进行内存推荐

带有 --docker 参数的 neo4j-admin server memory-recommendation 命令会输出可传递给 Neo4j Docker 容器的环境变量。以下示例展示了 neo4j-admin server memory-recommendation --docker 如何以 Docker 友好的格式提供内存推荐。

示例 1. 调用 neo4j-admin server memory-recommendation --docker
bin/neo4j-admin server memory-recommendation --memory=16g --docker

...
...
...
# Based on the above, the following memory settings are recommended:
NEO4J_server_memory_heap_initial__size='5g'
NEO4J_server_memory_heap_max__size='5g'
NEO4J_server_memory_pagecache_size='7g'
#
# It is also recommended turning out-of-memory errors into full crashes,
# instead of allowing a partially crashed database to continue running:
NEO4J_server_jvm_additional='-XX:+ExitOnOutOfMemoryError'
#
...
...

使用 Neo4j Admin 报告

Neo4j Admin 报告工具生成正在运行的 Neo4j 数据库的状态报告。
在容器化环境中,其命令 neo4j-admin server report 必须使用脚本 neo4j-admin-report 来调用。这可确保报告程序在运行时拥有分析运行中的 Neo4j 进程所需的所有必要文件权限。该脚本接收 neo4j-admin server report 命令的所有参数。

可以使用 docker exec 在容器内直接运行 neo4j-admin server report,但访问正在运行的进程将文件写入挂载文件夹所需的文件权限可能会发生冲突。

neo4j-admin-report 脚本只是 neo4j-admin server report 的一个包装器,它会自动处理大多数权限问题。

示例 2. 从容器化 Neo4j 数据库获取 Neo4j 报告的示例

启动一个 Neo4j 容器并挂载一个文件夹用于存储报告。

docker run --interactive --tty --rm \
    --name=neo4j \
    --publish=7474:7474 --publish=7687:7687 \
    --volume=$HOME/neo4j/data:/data \
    --volume=$HOME/neo4j/reports:/reports \ (1)
    neo4j:2026.03.1
1 报告的输出文件夹。

然后,使用 docker exec 运行 neo4j-admin-report 包装脚本,指定报告的输出目录。

docker exec --interactive --tty <containerID/name> \
    neo4j-admin-report \
        --to-path=/reports \ (1)
1 如果未指定 --to-path 选项,报告将写入 /tmp/reports

$HOME/neo4j/reports 文件夹现在应该包含一个包含报告的 zip 文件。

使用 Cypher Shell

Cypher Shell 可以使用以下命令在容器内本地运行

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

有关 cypher-shell 语法和选项的更多信息,请参阅 Cypher Shell#cypher-shell-syntax[语法]。

从 Neo4j Docker 容器中的数据库检索数据

以下是如何使用 cypher-shell 命令从 neo4j 数据库检索数据的示例。

  1. 运行一个新容器,挂载与导入示例中相同的 /data 卷。

    docker run --interactive --tty --name <containerID/name> \
        --publish=7474:7474 --publish=7687:7687 \
        --volume=$HOME/neo4j/data:/data \
        neo4j:2026.03.1
  2. 使用容器 ID 或名称进入容器,然后运行 cypher-shell 命令并进行身份验证。

    docker exec --interactive --tty <containerID/name> cypher-shell -u neo4j -p <password>
  3. 检索一些数据。

    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

将 Cypher 脚本文件传递给 Neo4j Docker 容器

有多种方法可以将 Cypher 脚本文件传递给 Neo4j Docker 容器,所有这些方法都使用 Cypher Shell 工具。

  • 使用 cypher-shell 命令的 --file 选项,后跟文件名。语句执行后,cypher-shell 将关闭。

  • 在 Cypher 交互式 shell 中时,使用 :source 命令后跟文件名。

  • catcurl 命令与 cypher-shell 结合使用,将脚本文件的内容通过管道传输到容器中。

要使用 Cypher Shell 的 --file 选项或 :source 命令,Cypher 脚本文件必须在容器内可读,否则 cypher-shell 将无法打开该文件。启动容器时,必须将包含示例的文件夹挂载到容器中。

以下是使用这些命令的语法示例

example.cypher 脚本
match (n:Actors)-[r]->(m:Movies) return n.name AS Actors, m.title AS Movies, m.year AS MovieYear;
使用 --file 选项调用 cypher-shell
# Put the example.cypher file in the local folder ./examples.

# Start a Neo4j container and mount the ./examples folder inside the container:

docker run --rm \
--volume /path/to/local/examples:/examples \
--publish=7474:7474 \
--publish=7687:7687 \
--env NEO4J_AUTH=neo4j/your_password \
neo4j:2026.03.1

# Run the Cypher Shell tool with the --file option passing the example.cypher file:

docker exec --interactive --tty  cypher-shell -u neo4j -p  --file /examples/example.cypher
使用 :source 命令运行 Cypher 脚本文件
# Put the example.cypher file in the local folder ./examples.

# Start a Neo4j container and mount the ./examples folder inside the container:

docker run --rm \
--volume /path/to/local/examples:/examples \
--publish=7474:7474 \
--publish=7687:7687 \
--env NEO4J_AUTH=neo4j/your_password \
neo4j:2026.03.1

# Use the container ID or name to get into the container, and then, run the cypher-shell command and authenticate.

docker exec --interactive --tty  cypher-shell -u neo4j -p 

# Invoke the :source command followed by the file name.

neo4j@neo4j> :source example.cypher
使用 Cypher Shell 调用 curl
curl http://mysite.com/config/example.cypher | sudo docker exec --interactive <containerID/name> cypher-shell -u neo4j -p <password>
使用 Cypher Shell 调用 cat
cat example.cypher | sudo  docker exec --interactive  <containerID/name> cypher-shell -u neo4j -p <password>
输出示例
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

这些命令获取脚本文件的内容,并使用 Cypher Shell 将其传递到 Docker 容器中。然后,它们运行 Cypher 示例(LOAD CSV 数据集,可能托管在服务器上(使用 curl))、创建索引、约束或执行其他管理操作。