Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(docker): provide a data directory that non-root users can reach
Elian Dorandcf9b33d1daa
diff --git a/apps/server/Dockerfile b/apps/server/Dockerfileindex 1d085fd863..22ed472df2 100644--- a/apps/server/Dockerfile+++ b/apps/server/Dockerfile@@ -19,6 +19,13 @@ WORKDIR /usr/src/appCOPY ./dist /usr/src/appCOPY ./start-docker.sh /usr/src/app+# A new named volume takes its owner from the mount point, so a container started with --user+# can write to it. Other UIDs need to pass through /home/node to reach a volume of their own.+RUN mkdir -p /home/node/trilium-data && \+ chown node:node /home/node/trilium-data && \+ chmod 700 /home/node/trilium-data && \+ chmod 711 /home/node+# The healthcheck reads the listening address the server records here.ENV TRILIUM_DATA_DIR=/home/node/trilium-datadiff --git a/apps/server/Dockerfile.alpine b/apps/server/Dockerfile.alpineindex 09dc16582a..9bcf795fdf 100644--- a/apps/server/Dockerfile.alpine+++ b/apps/server/Dockerfile.alpine@@ -17,6 +17,13 @@ COPY ./start-docker.sh /usr/src/app# Add application userRUN adduser -s /bin/false node; exit 0+# A new named volume takes its owner from the mount point, so a container started with --user+# can write to it. Other UIDs need to pass through /home/node to reach a volume of their own.+RUN mkdir -p /home/node/trilium-data && \+ chown node:node /home/node/trilium-data && \+ chmod 700 /home/node/trilium-data && \+ chmod 711 /home/node+# The healthcheck reads the listening address the server records here.ENV TRILIUM_DATA_DIR=/home/node/trilium-datadiff --git a/apps/server/Dockerfile.legacy b/apps/server/Dockerfile.legacyindex a483bde766..cf14fe5daa 100644--- a/apps/server/Dockerfile.legacy+++ b/apps/server/Dockerfile.legacy@@ -41,6 +41,13 @@ RUN rm -rf /usr/src/app/node_modules/better-sqlite3COPY --from=builder /usr/src/app/node_modules/better-sqlite3 /usr/src/app/node_modules/better-sqlite3COPY ./start-docker.sh /usr/src/app+# A new named volume takes its owner from the mount point, so a container started with --user+# can write to it. Other UIDs need to pass through /home/node to reach a volume of their own.+RUN mkdir -p /home/node/trilium-data && \+ chown node:node /home/node/trilium-data && \+ chmod 700 /home/node/trilium-data && \+ chmod 711 /home/node+# The healthcheck reads the listening address the server records here.ENV TRILIUM_DATA_DIR=/home/node/trilium-datadiff --git a/apps/server/scripts/check-docker-non-root.sh b/apps/server/scripts/check-docker-non-root.shindex e06ab0ac41..bfea4536b0 100644--- a/apps/server/scripts/check-docker-non-root.sh+++ b/apps/server/scripts/check-docker-non-root.sh@@ -8,8 +8,9 @@# owns the data directory:# - UID 1000 (`node`) on a fresh named volume, which Docker creates from the image's# mount point, so the image must provide one that `node` can write to.-# - An arbitrary UID on a volume it owns, as when matching a host user, so the path to-# the data directory must be traversable by users other than `node`.+# - An arbitrary UID on a directory it owns, as when mounting a host directory to match a+# host user, so the path to the data directory must be traversable by users other than+# `node`.set -eu@@ -33,15 +34,19 @@ start() {user="$2"owner="${3:-}"containers="$containers $name"+ mount="type=volume,src=$name-data,dst=$data_dir"docker volume create "$name-data" >/dev/nullif [ -n "$owner" ]; thendocker run --rm --user 0:0 --entrypoint chown -v "$name-data:/data" "$image" "$owner" /data+ # Docker fills an empty volume from the image's mount point, owner included, but never+ # a host directory mounted with -v, which this volume stands in for.+ mount="$mount,volume-nocopy=true"fidocker run -d --name "$name" \--user "$user" \--read-only --tmpfs /tmp \--cap-drop ALL --security-opt no-new-privileges \- -v "$name-data:$data_dir" \+ --mount "$mount" \"$image" >/dev/null}diff --git a/apps/server/src/services/data_dir.spec.ts b/apps/server/src/services/data_dir.spec.tsindex 2fdf7ae91b..2f55e63c2e 100644--- a/apps/server/src/services/data_dir.spec.ts+++ b/apps/server/src/services/data_dir.spec.ts@@ -518,6 +518,25 @@ describe("data_dir.ts unit tests", () => {expect(message).not.toContain("mkdir");});+ it("EACCES when the data directory itself cannot be entered – suggests a chown of it and exits", () => {+ stubUser(1234);+ delete process.env.TRILIUM_TMP_DIR;+ mockFn.pathJoinMock.mockImplementation((...parts: string[]) => parts.join("/"));+ mkdirThrows("EACCES");+ mockFn.accessSyncMock.mockImplementation((path: string) => {+ if (path === "/data") {+ throw new Error("EACCES");+ }+ });+ mockFn.statSyncMock.mockImplementation(() => ({ uid: 1000, gid: 1000, mode: 0o40700, isFile: () => false }));++ const message = runUntilExit(() => getDataDirs("/data"));+ expect(message).toContain("Trilium cannot start because it cannot create /data/tmp.");+ expect(message).toContain("Trilium runs as UID:GID 1234:1234, which cannot use its data directory, /data: it is owned by UID:GID 1000:1000 with permissions 700.");+ expect(message).toContain(" sudo chown -R 1234:1234 /data");+ expect(message).not.toContain("chmod");+ });+it("EACCES – names the ancestor that cannot be entered and exits", () => {stubUser(1234);mkdirThrows("EACCES");diff --git a/apps/server/src/services/data_dir.ts b/apps/server/src/services/data_dir.tsindex b2051f525e..e2e8b2fe6a 100644--- a/apps/server/src/services/data_dir.ts+++ b/apps/server/src/services/data_dir.ts@@ -201,7 +201,7 @@ function describePermissionError(path: string, dataDir: string | undefined) {const subject = user ? `Trilium runs as UID:GID ${user}, which` : "The user running Trilium";let lines: string[];- if (blockedDir) {+ if (blockedDir && blockedDir !== dataDir) {lines = [`Trilium cannot start because it cannot reach ${path}.`,"",@@ -212,14 +212,16 @@ function describePermissionError(path: string, dataDir: string | undefined) {: [ `To fix this, let the user running Trilium enter ${blockedDir}.` ]),"Or set TRILIUM_DATA_DIR to a directory that user can reach."];- } else if (parent === dataDir) {+ } else if (dataDir && (blockedDir === dataDir || parent === dataDir)) {+ // A data directory the user cannot enter needs the same fix as one it cannot write to.+ const access = blockedDir ? "use" : "write to";lines = [`Trilium cannot start because it cannot create ${path}.`,"",- `${subject} cannot write to its data directory, ${parent}: it ${describeOwnership(parent)}.`,+ `${subject} cannot ${access} its data directory, ${dataDir}: it ${describeOwnership(dataDir)}.`,"",...(user- ? [ "To fix this, give the data directory and everything in it to that user:", ` sudo chown -R ${user} ${parent}` ]+ ? [ "To fix this, give the data directory and everything in it to that user:", ` sudo chown -R ${user} ${dataDir}` ]: [ "To fix this, give the user running Trilium full control of the data directory." ]),"Or set TRILIUM_DATA_DIR to a directory that user owns."];