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
docs(docker): document running the image as a non-root user
Elian Doran5e2f79e3efa7
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Server Installation/1. Installing the server/Using Docker.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Server Installation/1. Installing the server/Using Docker.htmlindex bc48466c16..1a2ee3dc08 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Server Installation/1. Installing the server/Using Docker.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Server Installation/1. Installing the server/Using Docker.html@@ -5,9 +5,11 @@<p>Ensure Docker is installed on your system.</p><p>If you need help installing Docker, reference the <a href="https://docs.docker.com/engine/install/">Docker Installation Docs</a></p>-<p><strong>Note:</strong> Trilium's Docker container requires root privileges- to operate correctly.</p>-<aside class="admonition warning">+<p>By default, the container starts as root, prepares the data directory+ and then runs Trilium as an unprivileged user. It can also run entirely+ without root, as described in <em>Running as a non-root user</em> below.</p>+<aside+class="admonition warning"><p>If you're using a SMB/CIFS share or folder as your Trilium data directory, <ahref="https://github.com/TriliumNext/Notes/issues/415#issuecomment-2344824400">you'll need</a> toadd the mount options of <code>nobrl</code> and <code>noperm</code> when mounting@@ -72,118 +74,55 @@ docker inspect [container_name]</code></pre><li><a href="#root/_help_fDLvzOx29Pfg">Apache</a></li></ol>-<h3>Note on --user Directive</h3>-<p>The <code>--user</code> directive is unsupported. Instead, use the <code>USER_UID</code> and <code>USER_GID</code> environment- variables to set the appropriate user and group IDs.</p><h3>Note on timezones</h3><p>If you are having timezone issues and you are not using docker-compose,you may need to add a <code>TZ</code> environment variable with the <a href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones">TZ identifier</a> ofyour local timezone.</p>-<h2>Rootless Docker Image</h2>-<aside class="admonition note">- <p>Please keep in mind that the data directory is at <code>/home/trilium/trilium-data</code> instead- of the typical <code>/home/node/trilium-data</code>. This is because a new- user is created and used to run Trilium within the rootless containers.</p>-</aside>-<p>If you would prefer to run Trilium without having to run the Docker container- as <code>root</code>, you can use either of the provided Debian (default)- and Alpine-based images with the <code>rootless</code> tag. </p>-<p><em><strong>If you're unsure, stick to the “rootful” Docker image referenced above.</strong></em>-</p>-<p>Below are some commands to pull the rootless images:</p><pre><code class="language-text-x-trilium-auto"># For Debian-based image-docker pull triliumnext/trilium:rootless--# For Alpine-based image-docker pull triliumnext/trilium:rootless-alpine</code></pre>-<h3>Why Rootless?</h3>-<p>Running containers as non-root is a security best practice that reduces- the potential impact of container breakouts. If an attacker manages to- escape the container, they'll only have the permissions of the non-root- user instead of full root access to the host.</p>-<h3>How It Works</h3>-<p>The rootless Trilium image:</p>-<ol>- <li>Creates a non-root user (<code>trilium</code>) during build time</li>- <li>Configures the application to run as this non-root user</li>- <li>Allows runtime customization of the user's UID/GID via Docker's <code>--user</code> flag</li>- <li>Does not require a separate Docker <code>entrypoint</code> script</li>-</ol>-<h3>Usage</h3>-<h4><strong>Using docker-compose (Recommended)</strong></h4><pre><code class="language-text-x-trilium-auto"># Run with default UID/GID (1000:1000)-docker-compose -f docker-compose.rootless.yml up -d--# Run with custom UID/GID (e.g., match your host user)-TRILIUM_UID=$(id -u) TRILIUM_GID=$(id -g) docker-compose -f docker-compose.rootless.yml up -d-</code></pre>-<h4><strong>Using Docker CLI</strong></h4><pre><code class="language-text-x-trilium-auto"># Build the image-docker build -t triliumnext/trilium:rootless -f apps/server/Dockerfile.rootless .--# Run with default UID/GID (1000:1000)-docker run -d --name trilium -p 8080:8080 -v ~/trilium-data:/home/trilium/trilium-data triliumnext/trilium:rootless--# Run with custom UID/GID-docker run -d --name trilium -p 8080:8080 --user $(id -u):$(id -g) -v ~/trilium-data:/home/trilium/trilium-data triliumnext/trilium:rootless-</code></pre>-<h3>Environment Variables</h3>+<h3>Other environment variables</h3>+<p>For the complete list of environment variables Trilium reads (network+ settings, authentication, sync, etc.), see <a class="reference-link"+ href="#root/_help_Gzjqa934BdH4">Configuration (config.ini or environment variables)</a>.</p>+<h2>Running as a non-root user</h2>+<p>By default, the container starts as root, gives the data directory to+ the <code>node</code> user (UID and GID <code>1000</code>, or the values of <code>USER_UID</code> and <code>USER_GID</code>)+ and then runs Trilium as that user. Some environments do not allow a container+ to start as root at all, for example rootless Docker or Podman, or Kubernetes+ with <code>runAsNonRoot</code>. Starting with v0.107.0, the same image can+ run entirely as a non-root user instead.</p>+<p>To run Trilium as a non-root user, set the user with Docker's <code>--user</code> flag:</p><pre><code class="language-text-x-sh">docker run -d -p 8080:8080 --user 1000:1000 -v /srv/trilium-data:/home/node/trilium-data triliumnext/trilium:[VERSION]</code></pre>+<p>With Docker Compose, add <code>user:</code> to the service in <code>docker-compose.yml</code>:</p><pre><code class="language-text-x-yaml">services:+ trilium:+ user: "1000:1000"</code></pre>+<p>The image also runs with a read-only root filesystem and without any capabilities,+ for example with <code>--read-only --tmpfs /tmp --cap-drop ALL --security-opt no-new-privileges</code>.</p>+<p>Without root, the container cannot change the ownership of files, so:</p><ul>- <li><code>TRILIUM_UID</code>: UID to use for the container process (passed- to Docker's <code>--user</code> flag)</li>- <li><code>TRILIUM_GID</code>: GID to use for the container process (passed- to Docker's <code>--user</code> flag)</li>-</ul>-<p>For a complete list of configuration environment variables (network settings,- authentication, sync, etc.), see <a class="reference-link" href="#root/_help_Gzjqa934BdH4">Configuration (config.ini or environment variables)</a>.</p>-<h3>Volume Permissions</h3>-<p>If you encounter permission issues with the data volume, ensure that:</p>-<ol>- <li>The host directory has appropriate permissions for the UID/GID you're- using</li>- <li>You're setting both <code>TRILIUM_UID</code> and <code>TRILIUM_GID</code> to- match the owner of the host directory</li>-</ol><pre><code class="language-text-x-trilium-auto"># For example, if your data directory is owned by UID 1001 and GID 1001:-TRILIUM_UID=1001 TRILIUM_GID=1001 docker-compose -f docker-compose.rootless.yml up -d-</code></pre>-<h3>Considerations</h3>-<ul>- <li>The container starts with a specific UID/GID which can be customized at- runtime</li>- <li>Unlike the traditional setup, this approach does not use a separate entrypoint- script with <code>usermod</code>/<code>groupmod</code> commands</li>- <li>The container cannot modify its own UID/GID at runtime, which is a security- feature of rootless containers</li>-</ul>-<h3>Available Rootless Images</h3>-<p>Two rootless variants are provided:</p>-<ol>- <li><strong>Debian-based</strong> (default): Uses the Debian Bullseye Slim- base image- <ul>- <li>Dockerfile: <code>apps/server/Dockerfile.rootless</code>- </li>- <li>Recommended for most users</li>- </ul>+ <li>+ <p>The data directory on the host must exist and belong to that user before+ the container starts. If the directory is missing, Docker creates it as+ root, and Trilium cannot write to it. For example:</p><pre><code class="language-text-x-sh">mkdir -p /srv/trilium-data+sudo chown 1000:1000 /srv/trilium-data</code></pre></li>- <li><strong>Alpine-based</strong>: Uses the Alpine base image for smaller- size- <ul>- <li>Dockerfile: <code>apps/server/Dockerfile.alpine.rootless</code>- </li>- <li>Smaller image size, but may have compatibility issues with some systems</li>- </ul>+ <li>+ <p>A named volume instead of a host directory only works for UID <code>1000</code>:+ Docker gives a new volume the owner of the image's data directory, which+ is the <code>node</code> user.</p></li>-</ol>-<h3>Building Custom Rootless Images</h3>-<p>If you would prefer, you can also customize the UID/GID at build time:</p><pre><code class="language-text-x-trilium-auto"># For Debian-based image with custom UID/GID-docker build --build-arg USER=myuser --build-arg UID=1001 --build-arg GID=1001 \- -t triliumnext/trilium:rootless-custom -f apps/server/Dockerfile.rootless .--# For Alpine-based image with custom UID/GID-docker build --build-arg USER=myuser --build-arg UID=1001 --build-arg GID=1001 \- -t triliumnext/trilium:alpine-rootless-custom -f apps/server/Dockerfile.alpine.rootless .-</code></pre>-<p>Available build arguments:</p>-<ul>- <li><code>USER</code>: Username for the non-root user (default: trilium)</li>- <li><code>UID</code>: User ID for the non-root user (default: 1000)</li>- <li><code>GID</code>: Group ID for the non-root user (default: 1000)</li>-</ul>\ No newline at end of file+ <li>+ <p><code>USER_UID</code> and <code>USER_GID</code> are ignored. The user comes+ from <code>--user</code> or <code>user:</code> alone.</p>+ </li>+</ul>+<p>If Trilium cannot use its data directory, it stops at startup and prints+ which directory or files it cannot use, who owns them, and the command+ that fixes it.</p>+<aside class="admonition note">+ <p>An installation that ran as root leaves its files owned by UID <code>1000</code>,+ or by <code>USER_UID</code> if it was set. To switch it to <code>--user</code>,+ either use that same UID, or give the data directory to the new user first,+ for example with <code>sudo chown -R 1001:1001 /srv/trilium-data</code>.</p>+</aside>+<aside class="admonition note">+ <p>Earlier versions of this page described separate <code>rootless</code> images.+ Those images were never published; the standard image covers the same setups.</p>+</aside>\ No newline at end of filediff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Server Installation/1. Installing the server/Using Kubernetes.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Server Installation/1. Installing the server/Using Kubernetes.htmlindex 40625593b3..acc582e20e 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Server Installation/1. Installing the server/Using Kubernetes.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Server Installation/1. Installing the server/Using Kubernetes.html@@ -3,20 +3,23 @@own Kubernetes deployment.</p><p>The recommended way is to use a Helm chart.</p><h2>Root privileges</h2>-<aside class="admonition note">- <p>The Trilium container at this time needs to be run with root privileges.- It will swap to UID and GID <code>1000:1000</code> to run the <code>node</code> process- after execution though, so the main process doesn't run with root privileges.</p>-</aside>-<p>The Trilium docker container needs to be run with root privileges. The- node process inside the container will be started with reduced privileges- (uid:gid 1000:1000) after some initialization logic. Please make sure that- you don't use a security context (PodSecurityContext) which changes the- user ID. To use a different uid:gid for file storage and the application,- please use the <code>USER_UID</code> & <code>USER_GID</code> environment+<p>By default, the Trilium container starts as root, gives the data directory+ to UID and GID <code>1000:1000</code> and then runs Trilium with those reduced+ privileges, so no init container is needed to fix the permissions. To use+ a different UID and GID, set the <code>USER_UID</code> and <code>USER_GID</code> environmentvariables.</p>-<p>The docker image will also fix the permissions of <code>/home/node</code> so- you don't have to use an init container.</p>+<p>Starting with v0.107.0, the container can also run without root, which+ a <code>restricted</code> Pod Security Standard requires. Set the user in+ the pod's security context, and <code>fsGroup</code> so that Kubernetes gives+ the volume to that group:</p><pre><code class="language-text-x-yaml">securityContext:+ runAsUser: 1000+ runAsGroup: 1000+ runAsNonRoot: true+ fsGroup: 1000</code></pre>+<p>In that mode <code>USER_UID</code> and <code>USER_GID</code> are ignored.+ See the section on running as a non-root user in <a class="reference-link"+ href="#root/_help_rWX5eY045zbE">Using Docker</a> for what the data directory+ needs.</p><h2>Helm Charts</h2><p><a href="https://github.com/TriliumNext/helm-charts">Official Helm chart</a> fromTriliumNext Unofficial helm chart by <a href="https://github.com/ohdearaugustin">ohdearaugustin</a>: <adiff --git a/docs/User Guide/User Guide/Installation & Setup/Server Installation/1. Installing the server/Using Docker.md b/docs/User Guide/User Guide/Installation & Setup/Server Installation/1. Installing the server/Using Docker.mdindex de1eaf66b6..b073b6d325 100644--- a/docs/User Guide/User Guide/Installation & Setup/Server Installation/1. Installing the server/Using Docker.md+++ b/docs/User Guide/User Guide/Installation & Setup/Server Installation/1. Installing the server/Using Docker.md@@ -7,7 +7,7 @@ Ensure Docker is installed on your system.If you need help installing Docker, reference the [Docker Installation Docs](https://docs.docker.com/engine/install/)-**Note:** Trilium's Docker container requires root privileges to operate correctly.+By default, the container starts as root, prepares the data directory and then runs Trilium as an unprivileged user. It can also run entirely without root, as described in _Running as a non-root user_ below.> [!WARNING]> If you're using a SMB/CIFS share or folder as your Trilium data directory, [you'll need](https://github.com/TriliumNext/Notes/issues/415#issuecomment-2344824400) to add the mount options of `nobrl` and `noperm` when mounting your SMB share.@@ -113,127 +113,49 @@ If you want to run your instance in a non-default way, please use the volume swi1. [Nginx](../2.%20Reverse%20proxy/Nginx.md)2. [Apache](../2.%20Reverse%20proxy/Apache%20using%20Docker.md)-### Note on --user Directive--The `--user` directive is unsupported. Instead, use the `USER_UID` and `USER_GID` environment variables to set the appropriate user and group IDs.-### Note on timezonesIf you are having timezone issues and you are not using docker-compose, you may need to add a `TZ` environment variable with the [TZ identifier](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) of your local timezone.-## Rootless Docker Image--> [!NOTE]-> Please keep in mind that the data directory is at `/home/trilium/trilium-data` instead of the typical `/home/node/trilium-data`. This is because a new user is created and used to run Trilium within the rootless containers.+### Other environment variables-If you would prefer to run Trilium without having to run the Docker container as `root`, you can use either of the provided Debian (default) and Alpine-based images with the `rootless` tag.+For the complete list of environment variables Trilium reads (network settings, authentication, sync, etc.), see <a class="reference-link" href="../../../Advanced%20Usage/Configuration%20(config.ini%20or%20environment%20variables).md">Configuration (config.ini or environment variables)</a>.-_**If you're unsure, stick to the “rootful” Docker image referenced above.**_+## Running as a non-root user-Below are some commands to pull the rootless images:+By default, the container starts as root, gives the data directory to the `node` user (UID and GID `1000`, or the values of `USER_UID` and `USER_GID`) and then runs Trilium as that user. Some environments do not allow a container to start as root at all, for example rootless Docker or Podman, or Kubernetes with `runAsNonRoot`. Starting with v0.107.0, the same image can run entirely as a non-root user instead.-```-# For Debian-based image-docker pull triliumnext/trilium:rootless+To run Trilium as a non-root user, set the user with Docker's `--user` flag:-# For Alpine-based image-docker pull triliumnext/trilium:rootless-alpine+```sh+docker run -d -p 8080:8080 --user 1000:1000 -v /srv/trilium-data:/home/node/trilium-data triliumnext/trilium:[VERSION]```-### Why Rootless?--Running containers as non-root is a security best practice that reduces the potential impact of container breakouts. If an attacker manages to escape the container, they'll only have the permissions of the non-root user instead of full root access to the host.--### How It Works--The rootless Trilium image:--1. Creates a non-root user (`trilium`) during build time-2. Configures the application to run as this non-root user-3. Allows runtime customization of the user's UID/GID via Docker's `--user` flag-4. Does not require a separate Docker `entrypoint` script--### Usage--#### **Using docker-compose (Recommended)**--```-# Run with default UID/GID (1000:1000)-docker-compose -f docker-compose.rootless.yml up -d--# Run with custom UID/GID (e.g., match your host user)-TRILIUM_UID=$(id -u) TRILIUM_GID=$(id -g) docker-compose -f docker-compose.rootless.yml up -d--```--#### **Using Docker CLI**--```-# Build the image-docker build -t triliumnext/trilium:rootless -f apps/server/Dockerfile.rootless .--# Run with default UID/GID (1000:1000)-docker run -d --name trilium -p 8080:8080 -v ~/trilium-data:/home/trilium/trilium-data triliumnext/trilium:rootless--# Run with custom UID/GID-docker run -d --name trilium -p 8080:8080 --user $(id -u):$(id -g) -v ~/trilium-data:/home/trilium/trilium-data triliumnext/trilium:rootless+With Docker Compose, add `user:` to the service in `docker-compose.yml`:+```yaml+services:+ trilium:+ user: "1000:1000"```-### Environment Variables+The image also runs with a read-only root filesystem and without any capabilities, for example with `--read-only --tmpfs /tmp --cap-drop ALL --security-opt no-new-privileges`.-* `TRILIUM_UID`: UID to use for the container process (passed to Docker's `--user` flag)-* `TRILIUM_GID`: GID to use for the container process (passed to Docker's `--user` flag)+Without root, the container cannot change the ownership of files, so:-For a complete list of configuration environment variables (network settings, authentication, sync, etc.), see <a class="reference-link" href="../../../Advanced%20Usage/Configuration%20(config.ini%20or%20environment%20variables).md">Configuration (config.ini or environment variables)</a>.+* The data directory on the host must exist and belong to that user before the container starts. If the directory is missing, Docker creates it as root, and Trilium cannot write to it. For example:++ ```sh+ mkdir -p /srv/trilium-data+ sudo chown 1000:1000 /srv/trilium-data+ ```+* A named volume instead of a host directory only works for UID `1000`: Docker gives a new volume the owner of the image's data directory, which is the `node` user.+* `USER_UID` and `USER_GID` are ignored. The user comes from `--user` or `user:` alone.-### Volume Permissions+If Trilium cannot use its data directory, it stops at startup and prints which directory or files it cannot use, who owns them, and the command that fixes it.-If you encounter permission issues with the data volume, ensure that:--1. The host directory has appropriate permissions for the UID/GID you're using-2. You're setting both `TRILIUM_UID` and `TRILIUM_GID` to match the owner of the host directory--```-# For example, if your data directory is owned by UID 1001 and GID 1001:-TRILIUM_UID=1001 TRILIUM_GID=1001 docker-compose -f docker-compose.rootless.yml up -d--```--### Considerations--* The container starts with a specific UID/GID which can be customized at runtime-* Unlike the traditional setup, this approach does not use a separate entrypoint script with `usermod`/`groupmod` commands-* The container cannot modify its own UID/GID at runtime, which is a security feature of rootless containers--### Available Rootless Images--Two rootless variants are provided:--1. **Debian-based** (default): Uses the Debian Bullseye Slim base image- * Dockerfile: `apps/server/Dockerfile.rootless`- * Recommended for most users-2. **Alpine-based**: Uses the Alpine base image for smaller size- * Dockerfile: `apps/server/Dockerfile.alpine.rootless`- * Smaller image size, but may have compatibility issues with some systems--### Building Custom Rootless Images--If you would prefer, you can also customize the UID/GID at build time:--```-# For Debian-based image with custom UID/GID-docker build --build-arg USER=myuser --build-arg UID=1001 --build-arg GID=1001 \- -t triliumnext/trilium:rootless-custom -f apps/server/Dockerfile.rootless .--# For Alpine-based image with custom UID/GID-docker build --build-arg USER=myuser --build-arg UID=1001 --build-arg GID=1001 \- -t triliumnext/trilium:alpine-rootless-custom -f apps/server/Dockerfile.alpine.rootless .--```--Available build arguments:+> [!NOTE]+> An installation that ran as root leaves its files owned by UID `1000`, or by `USER_UID` if it was set. To switch it to `--user`, either use that same UID, or give the data directory to the new user first, for example with `sudo chown -R 1001:1001 /srv/trilium-data`.-* `USER`: Username for the non-root user (default: trilium)-* `UID`: User ID for the non-root user (default: 1000)-* `GID`: Group ID for the non-root user (default: 1000)\ No newline at end of file+> [!NOTE]+> Earlier versions of this page described separate `rootless` images. Those images were never published; the standard image covers the same setups.\ No newline at end of filediff --git a/docs/User Guide/User Guide/Installation & Setup/Server Installation/1. Installing the server/Using Kubernetes.md b/docs/User Guide/User Guide/Installation & Setup/Server Installation/1. Installing the server/Using Kubernetes.mdindex e6d34e9f82..6c6b8ceaad 100644--- a/docs/User Guide/User Guide/Installation & Setup/Server Installation/1. Installing the server/Using Kubernetes.md+++ b/docs/User Guide/User Guide/Installation & Setup/Server Installation/1. Installing the server/Using Kubernetes.md@@ -5,12 +5,19 @@ The recommended way is to use a Helm chart.## Root privileges-> [!NOTE]-> The Trilium container at this time needs to be run with root privileges. It will swap to UID and GID `1000:1000` to run the `node` process after execution though, so the main process doesn't run with root privileges.+By default, the Trilium container starts as root, gives the data directory to UID and GID `1000:1000` and then runs Trilium with those reduced privileges, so no init container is needed to fix the permissions. To use a different UID and GID, set the `USER_UID` and `USER_GID` environment variables.-The Trilium docker container needs to be run with root privileges. The node process inside the container will be started with reduced privileges (uid:gid 1000:1000) after some initialization logic. Please make sure that you don't use a security context (PodSecurityContext) which changes the user ID. To use a different uid:gid for file storage and the application, please use the `USER_UID` & `USER_GID` environment variables.+Starting with v0.107.0, the container can also run without root, which a `restricted` Pod Security Standard requires. Set the user in the pod's security context, and `fsGroup` so that Kubernetes gives the volume to that group:-The docker image will also fix the permissions of `/home/node` so you don't have to use an init container.+```yaml+securityContext:+ runAsUser: 1000+ runAsGroup: 1000+ runAsNonRoot: true+ fsGroup: 1000+```++In that mode `USER_UID` and `USER_GID` are ignored. See the section on running as a non-root user in <a class="reference-link" href="Using%20Docker.md">Using Docker</a> for what the data directory needs.## Helm Charts