Repository navigation
fix(docker): catch a volume mounted on the old data path - #12890
Conversation
A user upgrading the Docker image from v0.105 got the setup screen instead of their notes. Their volume was mounted at /home/node/.local/share/trilium-data. Until v0.106 the image left TRILIUM_DATA_DIR unset, so getTriliumDataDir() fell through to the platform app data directory and found that mount. Since 8e05dba the image sets TRILIUM_DATA_DIR=/home/node/trilium-data (and since dcf9b33 it also creates that directory), so Trilium created a new document in the container's own, unmounted directory. Notes written there were lost the next time the container was recreated. getDataDirs() now checks, before anything is created, whether the data directory lacks document.db while ~/trilium-data or the platform app data directory holds one. If so, startup stops with a message naming both paths, the mount that fixes it and the TRILIUM_DATA_DIR that keeps the old path. The check runs only in a container (/.dockerenv or /run/.containerenv, as printListeningUrls() detects it). Outside one, pointing TRILIUM_DATA_DIR at an empty directory is the documented way to run a second instance, so checking there would block that setup. Removing the ENV from the image was rejected: the image also creates ~/trilium-data now, which wins over ~/.local/share in the same way. A container that already ran a newer image with the old mount has a document in /home/node/trilium-data, so the check lets it start; it recovers once the container is recreated. The spec that expects the stop failed with the call disabled and passes with it. Assisted-by: Claude Code
|
📚 Documentation preview is ready! 🔗 Preview URL: https://pr-12890.trilium-docs.pages.dev ✅ All checks passed This preview will be updated automatically with new commits. |
[Medium impact] The PR appears safe to merge; the revised recovery advice addresses the remaining backup-format finding. SummaryThis PR catches misplaced container volumes before Trilium creates an empty document.
perfectra1n intentionally left early startup messages in English because no database locale is available. Greptile accepted that exception. Reviews (7) · Last reviewed commit: "fix(docker): restore tmpfs backups throu..." · Reviewed by Greptile |
| `Trilium cannot start because its data directory, ${dataDir}, holds no document, but ${otherDir} does.`, | ||
| "", | ||
| "Starting would create a new, empty document instead of opening that one. Docker images up to", | ||
| `v0.105 used ${otherDir} when a volume was mounted there; newer images use ${dataDir}.`, |
There was a problem hiding this comment.
Recovery guidance stays English
The new startup explanation is hardcoded English. CLAUDE.md requires user-facing text, including server messages, to use translations. Put this explanation in the server catalogue while keeping it available before normal translation setup runs. This repository requirement must be satisfied before merging.
Context Used: CLAUDE.md (source)
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
I don't think this one can be translated, so I've left it in English on purpose:
- The server's language is the
localeoption, which lives indocument.db. This message runs exactly when the data directory has nodocument.db, so there is no language to read, and i18next would fall back to English anyway. getDataDirs()runs synchronously whendata_dir.tsis first loaded, before the database is opened. Setting up i18next is asynchronous and reads the locale from that database, so it can't run first.- The other startup errors in this file, the permission messages from
checkExistingEntries()anddescribePermissionError(), are English for the same reasons.
Leaving this thread open for a maintainer to decide.
There was a problem hiding this comment.
You’re right. This check runs before there’s a database locale to use or i18next is initialized, so requiring translation here would not make the message meaningfully translatable. The existing English-only startup errors in this file support treating this as the same constraint. I agree this isn’t an actionable finding for this change.
A rule I could remember is: “Startup errors emitted before the database and i18next are initialized may remain in English when no locale is available.” Would you like me to save that for future reviews?
Bundle ReportChanges will increase total bundle size by 2.48kB (0.0%) ⬆️. This is within the configured threshold ✅ Detailed changes
Affected Assets, Files, and Routes:view changes for bundle: standalone-esmAssets Changed:
view changes for bundle: client-esmAssets Changed:
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Images v0.106 to v0.108 ignored a volume mounted at /home/node/.local/share/trilium-data and stored new notes in /home/node/trilium-data inside the container. Recreating the container, which pulling a new image does, deletes them. The startup check alone cannot reach those users: by the time they run it, the upgrade has already recreated the container. So the warning goes where they will see it before upgrading: - A `type: warning` changelog entry, which renders as an admonition at the top of the release note and the nightly release body, with the `docker cp` that saves the notes. - The Docker installation page, as a warning instead of a note. In the container logs, checkForDocumentElsewhere() now prints both messages as banners. The stop also says that notes written on v0.106 to v0.108 can still be in the old container and how to copy them out. When both directories hold a document and the data directory is not a mount point (its device equals its parent's), startup goes on but warns that the open document is deleted with the container. The mount point test keeps the warning off a setup that copied its data to the new mount and left the old one in place. The warning spec failed with the warning disabled and passes with it. Assisted-by: Claude Code (Claude Opus 5.5)
The stop banner told users who want a new document to move document.db out of the old directory. After a crash or a forced stop, saved changes can still be in document.db-wal, so moving the database alone leaves them behind and the moved copy opens without them. The banner now says to stop Trilium and move every file in the directory together. The `docker cp` advice in both banners, the release note warning and the Docker page copied the data directory of a container that could be running, so document.db and document.db-wal could come from different moments. Each now stops the container first, without removing it, and copies the whole directory. The updated specs failed against the previous banners on the missing `docker stop` and pass with them. Assisted-by: Claude Code (Claude Opus 5.5)
The warning that the open document is deleted with the container decided that by comparing the data directory's device with its parent's, which only detects a volume mounted at the data directory itself. With a volume at /home/node, which holds both default data directories, the two share a device, so Trilium warned that safe notes would be lost and told the user to change a mount that works. isOnVolume() now compares the data directory's device with the root filesystem's instead, which tells the container's own files from a volume mounted at the directory or any of its ancestors. It compares directories because overlayfs can report the device of its underlying layer for a regular file, so stat on document.db would not answer the same question. The spec's new case, a volume at /home/node, failed against the parent comparison and passes with this. Assisted-by: Claude Code (Claude Opus 5.5)
The warning that the open document is deleted with the container only fired when another default data directory also held a document. A new user who mounts a volume at the old ~/.local/share/trilium-data path gets no document there, so Trilium created one in the container's own /home/node/trilium-data and stayed silent at every later start, until an image update deleted it. The same goes for a container started with no volume at all. checkContainerDataDir() (renamed from checkForDocumentElsewhere(), as it now covers more than a second document) warns at every start whenever the data directory is not on a volume. The banner names what to fix: the volume that holds an older document, a volume mounted on a path Trilium does not use, or no volume at all, with the -v that fixes it. The stop for a data directory with no document while the old path has one is unchanged. isInContainer() also accepts KUBERNETES_SERVICE_HOST. containerd, the runtime of most Kubernetes clusters, creates neither /.dockerenv nor /run/.containerenv, so neither check ran in a pod. Kubernetes sets the variable in every pod, and nothing sets it outside one. CI's Docker smoke test runs the image without a volume, so it now prints the warning; it only checks the health status, so it still passes. The new specs, Kubernetes detection and the warning for a fresh install with the volume on the old path or with none, failed against the previous checks and pass with these. Assisted-by: Claude Code (Claude Opus 5.5)
The server suite failed its 100% coverage threshold on this branch: no spec reached the catch in isOnVolume(), which treats a data directory that cannot be inspected as being on a volume, so Trilium stays quiet rather than warn on a guess. Run alone, the spec also missed the branch for a platform with no app data directory, where only ~/trilium-data is checked. The quiet-start spec now covers both, and data_dir.spec.ts alone brings data_dir.ts to 100% of lines, statements, functions and branches. The other gap in the same run, lines 52-53 of sqlite_binding.ts, comes from 9c19cb6 on main, whose own Dev run fails on it, and is not part of this branch. Assisted-by: Claude Code (Claude Opus 5.5)
The container check judged TRILIUM_DATA_DIR and compared device numbers, which got several setups wrong: - TRILIUM_DOCUMENT_PATH can put document.db outside the data directory, so a document off every volume went unreported, a safe one was reported as lost, and the advice copied a directory without it. The check now judges and copies the directory holding DOCUMENT_PATH, symlinks resolved, and suggests TRILIUM_DOCUMENT_PATH when that is what the user set. - A toolbox or distrobox container (Podman, so /run/.containerenv) bind-mounts the home directory. A second instance or a dev server there, with TRILIUM_DATA_DIR on a new directory while ~/.local/share/trilium-data holds the desktop document, could not start. The stop now requires the other document to be on another mount than the one the new document would go on, which is the reporter's case (container storage against a volume) and not this. - Docker's vfs storage driver, or a bind mount from the root's own filesystem, gives a volume the root's device number, so a correct mount drew a warning that the notes will be lost. Mounts are now read from /proc/self/mountinfo, which names every bind mount; the device comparison remains only for when it cannot be read. - A volume mounted at a path Trilium never looks at (/data, a copied path from another image) holding document.db led to the setup screen. Mount points and their trilium-data subdirectories are now searched too, and the v0.105 history is given only for the old path, where it is true. - A tmpfs data directory counted as a volume. It is now reported as lost when the container stops, with the copy taken before stopping, since stopping clears it. - document.db bind-mounted as a single file was reported as lost. The file survives, but SQLite keeps document.db-wal beside it, inside the container, so it now gets its own warning: mount the directory. Unchanged and covered by the spec: fresh installs, the documented mount, TRILIUM_DATA_DIR=/data with a volume there as Kubernetes charts set it, a read-only root with a tmpfs /tmp, a volume at /home/node, and anything outside a container. Against the previous check, 8 of the 10 new container specs fail, among them the toolbox case, which exited. The two that pass guard behaviour the previous check already had. data_dir.spec.ts alone covers 100% of data_dir.ts. Assisted-by: Claude Code (Claude Opus 5.5)
The warning for a document on tmpfs said to copy the directory with docker cp before the container stops. Run against a real container, docker cp copied an empty directory: it cannot read a tmpfs mount, which Docker documents. Pausing first, as review suggested to keep Trilium from writing during the copy, does not change that. The warning now says to make a backup in Trilium (Options > Backup > Backup now), which writes one consistent file through SQLite's backup API, and to copy the backup directory out with docker exec and tar, then mount a volume with that backup in it as document.db. Done by hand on a container with a tmpfs data directory, the backup came out intact (integrity_check ok, 334 notes) and Trilium started on it with every note and its consistency checks passing. findOtherDocument() returned the first directory holding a document, so one on the same mount as the new document, such as ~/trilium-data on a volume at /home/node, hid a separate volume at the old path, and Trilium created an empty document instead of stopping. A document on another mount now comes first. Both new specs failed before the change. The other scenarios were run against an image built from this branch: the documented mount (fresh, existing, --user 1000:1000), a named volume at /home/node, TRILIUM_DATA_DIR=/data and a symlinked data directory start quietly; the old path, a volume at /data, an unmounted TRILIUM_DOCUMENT_PATH and Kubernetes without /.dockerenv stop, leaving the volume byte for byte unchanged; no volume, an empty volume at the old path, both documents, tmpfs and a single-file mount warn as intended. Assisted-by: Claude Code (Claude Opus 5.5)
The tmpfs warning said to put the backup on a volume as document.db. With backupEnableCompression on (or a backup password set), Backup now writes a .tnbackup container instead of a SQLite file, and a container started on that file, renamed, exits with "SqliteError: file is not a database". The warning now says to mount an empty volume and restore the file from Trilium's setup screen, which takes .db and .tnbackup alike and asks for the password of an encrypted one, and to unpack the tar it was copied out in first. Checked against an image built from this branch: with compression on, Backup now wrote backup-now.tnbackup; renamed to document.db, the container exited with that error; restored through the setup screen into a fresh volume, it opened all 334 notes with the consistency checks passing. The updated spec failed against the previous wording. Assisted-by: Claude Code
A user on Matrix upgraded the Docker image past v0.105 and got the language selection setup screen instead of their notes. They had to go back to v0.105, and after a second attempt they lost about ten days of notes.
This only affects Docker and Kubernetes containers whose volume is mounted at
/home/node/.local/share/trilium-datainstead of the documented/home/node/trilium-data, or that have no volume at all. A fresh install with the documented mount starts exactly as before, as does anything outside a container.What was wrong
How Trilium picks its data folder
When the server starts,
getTriliumDataDir()picks the data folder by trying four options in order and taking the first one that applies:Trilium/apps/server/src/services/data_dir.ts
Lines 115 to 355 in 4e2cab6
TRILIUM_DATA_DIR, if the variable is set.~/trilium-data, if that folder exists.~/.local/share/trilium-dataon Linux, if~/.local/shareexists. The folder is created if needed.~/trilium-dataotherwise, created if needed.Whatever folder it picks, a missing
document.dbthere means a new installation: Trilium creates an empty database and shows the setup screen. Nothing checks whether a database exists somewhere else.Why the reporter's setup worked up to v0.105
Up to v0.105.0, the standard images (
Dockerfile,Dockerfile.alpine,Dockerfile.legacy) did not setTRILIUM_DATA_DIRand did not create any data folder. The docs have always said to mount the volume at/home/node/trilium-data, and that is what most people do. Then step 1 doesn't apply, the mount makes step 2 apply, and Trilium uses the volume.The reporter mounted their volume at
/home/node/.local/share/trilium-datainstead. Then step 1 doesn't apply,~/trilium-datadoesn't exist so step 2 doesn't apply either, and Docker creates~/.local/shareas the mount's parent folder, so step 3 applies and picks their volume. That isn't a documented setup, but it worked for years. Anyone who copied a compose file from a desktop install (where the data folder really is~/.local/share/trilium-data) or from an old guide could have ended up with it.(The rootless images have set
TRILIUM_DATA_DIRsince before v0.105, so their users were never affected.)What changed in v0.106.0 (released 2026-09-25)
8e05dba, from #11164 (merged 2026-08-23), replaced the Node.js healthcheck with a shell script to cut the CPU spike of starting Node.js every minute (#4576). The old Node.js probe found the data folder by copying the same four steps above,
~/.local/shareincluded, so it followed the reporter's volume too. A shell script can't easily do that, so the commit gave it a fixed path by adding this line to all three standard images:Trilium/apps/server/Dockerfile
Lines 22 to 23 in c0fdceb
The comment shows the variable was added for the healthcheck. But an
ENVline in a Dockerfile applies to every process in the container, the server included. For the server, it is step 1, which overrides the other three. Anyone mounting at/home/node/trilium-datasaw no change, because that was already their folder. For the reporter, the server stopped looking at~/.local/shareand used/home/node/trilium-data, which wasn't mounted. That folder had nodocument.db, so Trilium created a new database there and showed the setup screen.What changed in v0.107.0 (released 2026-11-08)
dcf9b33, from #11772 (merged 2026-09-29), made the images work under
--userby creating the data folder in the image with the right owner:Trilium/apps/server/Dockerfile
Lines 22 to 27 in d83d715
That broke the same setup a second way. With
~/trilium-datanow in every image, step 2 applies even ifTRILIUM_DATA_DIRis unset. Removing theENVline would no longer bring the old behaviour back.Why it caused data loss, not just a confusing screen
The new, empty database lived in
/home/node/trilium-data, inside the container's own storage, not on the volume. Anyone who set Trilium up again on that screen and kept working put their new notes there. That storage belongs to the container and is deleted whenever the container is recreated.docker compose pull && docker compose up -d, Watchtower and most NAS update buttons all recreate the container. The original notes stayed untouched on the volume, while everything written after the upgrade was wiped at the next update. Nothing in the logs said anything was wrong.The reporter went back to v0.105.0 (which uses the volume again), upgraded again later and got the empty database again, then tried to recover with backups. They finally found the mismatch by opening a shell in the container, after losing about ten days of notes.
The fix
All of this lives in
checkContainerDocument(), whichgetDataDirs()calls before creating anything. It only runs inside a container:/.dockerenvor/run/.containerenvexists (the same testprintListeningUrls()inwww.tsuses), orKUBERNETES_SERVICE_HOSTis set. Kubernetes needs the variable because containerd, the runtime most clusters use, creates neither file, while Kubernetes sets the variable in every pod. Outside a container, pointingTRILIUM_DATA_DIRat an empty folder is how the "Multiple server instances" docs page tells people to run a second instance, and nothing here should get in the way of that.Trilium/apps/server/src/services/data_dir.ts
Lines 115 to 355 in 4e2cab6
What it looks at
TRILIUM_DATA_DIR. That is the parent ofDOCUMENT_PATHwith symlinks resolved, soTRILIUM_DOCUMENT_PATHand a symlinked data folder are judged by where the database really is./proc/self/mountinfo, which lists every bind mount even when it comes from the same filesystem as the container's root (Docker's vfs storage driver does that). Each mount is one of three kinds: the container's own root filesystem (deleted with the container),tmpfs/ramfs(lost when it stops), or a volume. If/proccan't be read, it falls back to comparing device numbers with/. Folders are compared, not files, because overlayfs can report the device of its underlying layer for a regular file. When it can't tell at all, it stays quiet.When it stops startup
There is no document at
DOCUMENT_PATH, another folder holds one, and that folder is on a different mount from where the new document would go. The other folders it checks are~/trilium-data,~/.local/share/trilium-data, and every mount point plus itstrilium-datasubfolder, so a volume holding a document at any path is found. A document on a different mount comes first, so one on the same mount (such as~/trilium-dataon a volume at/home/node) can't hide a separate volume at the old path.The "different mount" condition is what separates the reporter's case (new document in container storage, old one on a volume) from a deliberate setup. In toolbox or distrobox (Podman, so
/run/.containerenvexists), the home folder is one bind mount, and a second instance or a dev server withTRILIUM_DATA_DIRpointing at a new folder has to keep starting even though~/.local/share/trilium-dataholds the desktop document.The message names both paths, the volume mount that fixes it, and the variable that keeps the current layout working:
TRILIUM_DOCUMENT_PATHif the user set that,TRILIUM_DATA_DIRotherwise. Only for the old~/.local/sharepath does it add the v0.105 history and thedocker stopthendocker cpsteps for notes written on v0.106 to v0.108. For anyone who really wants a new document, it says to move every file in the other folder together, because after a crashdocument.db-walcan still hold saved changes thatdocument.dbdoesn't have yet.When it warns (at every start)
docker stopanddocker cpof that folder, and the-vthat fixes it.tmpfsdocker cpcan't read a tmpfs mount, so it says to make a backup in Trilium (Options > Backup > Backup now, one consistent file through SQLite's backup API), copy the backup directory out withdocker exec … tar, mount an empty volume, and restore the file from the setup screen's Restore from backup, which takes.dband.tnbackupfiles (compressed or encrypted backups are.tnbackup, which renaming todocument.dbwould not open).document.dbis bind-mounted as a single filedocument.db-walnext to it, inside the container, along with everything else in that folder. Mount the folder instead.Stays quiet
Fresh installs with the documented mount,
TRILIUM_DATA_DIR=/datawith a volume at/data(as the Kubernetes charts on kubesearch.dev set it, including asubPathmount andreadOnlyRootFilesystemwith a memory-backed/tmp), a volume at/home/node, the documented mount with the old path still mounted alongside, toolbox and distrobox, a symlinked data folder, anything it can't inspect, and anything outside a container. The spec covers each of these.CI's Docker smoke test runs the image without a volume, so it now prints the warning, but it only checks the health status and still passes.
I considered removing the
ENVline from the images instead. That isn't enough on its own, because the image now also creates~/trilium-data, which takes priority over~/.local/sharefor the same reason. The healthcheck also relies on the variable.Warning users before they upgrade
The log messages can't reach the users who are most at risk. Upgrading to the release that has this fix means pulling a new image, which recreates the container and deletes the notes stored in it before the new code ever runs. So the warning also goes where they will see it before upgrading:
type: warningchangelog entry. It renders as a[!WARNING]admonition at the top of the release note, and of the nightly release body until then, telling users with the old mount path to rundocker cpbefore pulling.There are also two regular changelog entries: a fix for the startup check, and an improvement for the warning about notes that aren't on a volume.