|
| 1 | +:description: How to use persistent storage when using Neo4j in Docker. |
| 2 | +[[docker-volumes]] |
| 3 | += Persist data with Docker volumes |
| 4 | + |
| 5 | +Docker containers are ephemeral. |
| 6 | +When a container is stopped, any data written to it is lost. |
| 7 | +Therefore, if you want to persist data when using Neo4j in Docker, you must mount storage to the container. |
| 8 | +Storages also allow you to get data in and out of the container. |
| 9 | + |
| 10 | +Storage can be mounted to a container in two ways: |
| 11 | + |
| 12 | +* A folder on the host file system. |
| 13 | +* A Docker volume -- a named storage location that is managed by Docker. |
| 14 | +
|
| 15 | +For instructions on _how_ to mount storage to a Docker container, refer to the official Docker documentation link:https://docs.docker.com/storage/bind-mounts/[Bind mounts^] and link:https://docs.docker.com/storage/volumes/[Volumes^]. |
| 16 | + |
| 17 | +Neo4j provides several mount points for storage to simplify using Neo4j in Docker. |
| 18 | +The following sections describe the mount points and how to use them. |
| 19 | + |
| 20 | +[[docker-volumes-mount-points]] |
| 21 | +== Neo4j mount points and permissions |
| 22 | + |
| 23 | +The following table is a complete reference of the mount points recognized by the Neo4j Docker image, and file permissions. |
| 24 | + |
| 25 | +All the listed mount points are *optional*. |
| 26 | +Neo4j can run in Docker without any volumes mounted at all. |
| 27 | +However, mounting storage to `/data` is considered essential for all but the most basic use cases. |
| 28 | + |
| 29 | +[WARNING] |
| 30 | +==== |
| 31 | +Running containerized Neo4j without a `/data` mount results in *unrecoverable data loss* if anything happens to the container. |
| 32 | +==== |
| 33 | + |
| 34 | +.Mount points for the Neo4j container |
| 35 | +[options="header", cols="1m,1,4"] |
| 36 | +|=== |
| 37 | +| Mount point |
| 38 | +| Permissions required |
| 39 | +| Description |
| 40 | + |
| 41 | +| /data |
| 42 | +| read, write |
| 43 | +| The data store for the Neo4j database. See xref:#docker-volumes-data[]. |
| 44 | + |
| 45 | +| /logs |
| 46 | +| read, write |
| 47 | +| Output directory for Neo4j logs. See xref:#docker-volumes-logs[]. |
| 48 | + |
| 49 | +| /conf |
| 50 | +| readfootnote:[Write permissions are required when using the xref:docker/configuration.adoc#docker-conf-volume[`dump-config`] feature.] |
| 51 | +| Pass configuration files to Neo4j on startup. + |
| 52 | +See xref:docker/configuration.adoc[]. |
| 53 | + |
| 54 | +| /plugins |
| 55 | +| readfootnote:[Write permissions are required when using the xref:docker/plugins.adoc#docker-plugins-caching[`NEO4J_PLUGINS` feature] to download and store plugins.] |
| 56 | +| Allows you to install plugins in containerized Neo4j. + |
| 57 | +See xref:docker/plugins.adoc[]. |
| 58 | + |
| 59 | +| /licenses |
| 60 | +| read |
| 61 | +| Provide licenses for Neo4j and any plugins by mounting the license folder. + |
| 62 | +See xref:docker/plugins.adoc#docker-plugins-licenses[Installing Plugin Licenses]. |
| 63 | + |
| 64 | +| /import |
| 65 | +| read |
| 66 | +| Make _csv_ and other importable files available to xref:docker/operations.adoc#docker-neo4j-import[neo4j-admin import]. |
| 67 | + |
| 68 | +| /ssl |
| 69 | +| read |
| 70 | +| Provide SSL certificates to Neo4j for message encryption. + |
| 71 | +See xref:docker/security.adoc[] |
| 72 | + |
| 73 | +| /metrics |
| 74 | +| write |
| 75 | +| label:enterprise[Enterprise Edition] Output directory for metrics files. |
| 76 | +See xref:monitoring/metrics/index.adoc[Metrics]. |
| 77 | +|=== |
| 78 | + |
| 79 | +[[docker-volumes-data]] |
| 80 | +=== Mounting storage to `/data` |
| 81 | + |
| 82 | +Neo4j inside Docker stores database files in the `/data` folder. |
| 83 | +By mounting storage to `/data`, any data written to Neo4j will persist after the container is stopped. |
| 84 | + |
| 85 | +Stopping the container and then restarting with the same folder mounted to `/data` starts a new containerized Neo4j instance with the same data. |
| 86 | + |
| 87 | +[CAUTION] |
| 88 | +==== |
| 89 | +If Neo4j could not properly close down, it may have left data in a bad state and is likely to fail on startup. |
| 90 | +This is the same as if Neo4j is run outside a container and not closed properly. |
| 91 | +==== |
| 92 | + |
| 93 | +.Two ways to mount storage to the `/data` mount point |
| 94 | +==== |
| 95 | +.Mounting a folder to `/data` |
| 96 | +[source, shell, subs="attributes"] |
| 97 | +---- |
| 98 | +docker run -it --rm \ |
| 99 | + --volume $HOME/neo4j/data:/data \ |
| 100 | + neo4j:{neo4j-version-exact} |
| 101 | +---- |
| 102 | +
|
| 103 | +.Creating a named volume and mounting it to `/data` |
| 104 | +[source, shell, subs="attributes+,+macros"] |
| 105 | +---- |
| 106 | +docker volume create neo4jdata # <1> |
| 107 | +docker run -it --rm \ |
| 108 | + --volume neo4jdata:/data \ # <2> |
| 109 | + neo4j:{neo4j-version-exact} |
| 110 | +---- |
| 111 | +<1> Create a Docker volume named `neo4jdata`. |
| 112 | +<2> Mount the volume name `neo4jdata` to `/data`. |
| 113 | +==== |
| 114 | + |
| 115 | +[[docker-volumes-logs]] |
| 116 | +=== Mounting storage to `/logs` |
| 117 | + |
| 118 | +Neo4j logging output is written to files in the _/logs_ directory. |
| 119 | +This directory is mounted as a _/logs_ volume. |
| 120 | +By mounting storage to `/logs`, the log files become available outside the container. + |
| 121 | + |
| 122 | +[TIP] |
| 123 | +==== |
| 124 | +For more information about configuring Neo4j, see xref:docker/configuration.adoc[Configuration]. + |
| 125 | +For more information about the Neo4j log files, see xref:monitoring/logging.adoc[Logging]. |
| 126 | +==== |
| 127 | + |
| 128 | + |
| 129 | +[[docker-volumes-file-permissions]] |
| 130 | +== File permissions |
| 131 | + |
| 132 | +For security reasons, by default, Neo4j runs as the `neo4j` user inside the container. |
| 133 | +This user has user ID `7474`. |
| 134 | +If `neo4j` needs read or write access to a mounted folder, but does not have it, the folder will be automatically re-owned to `7474`. |
| 135 | + |
| 136 | +[NOTE] |
| 137 | +==== |
| 138 | +This is a convenient feature, so you do not have to worry about the finer details of file permissions in Docker and can get started more easily. |
| 139 | +It does however mean that mounted folders change ownership, and you may find you can no longer read your files without root access. |
| 140 | +==== |
| 141 | + |
| 142 | +=== Docker `run` with `--user` flag |
| 143 | + |
| 144 | +The `--user` flag to `docker run` forces Docker to run as the provided user. |
| 145 | +In this situation, if that user does not have the required read or write access to any mounted folders, Neo4j will fail to start. |
0 commit comments