Container Permission Pollution Fix: Deploying AI Assistant Containers with an Explicit UID/GID on NAS Devices

    This article explains why permission pollution occurs when AI assistant containers run as root on private storage devices, and how it causes recurring failures in configuration, logs, scheduled tasks, and credentials. It walks through a self-managed container deployment that explicitly sets a runtime UID/GID, including data backup, ownership correction, container creation, verification, cleanup, and a rollback plan, plus prevention practices for stable long-term operation.

    1. Background: Why “One-Click Install” Plants Recurring Failures

    When deploying a composite AI assistant container on a private storage device—for example, a service that combines a local large language model, smart home integration, an instant messaging gateway, and scheduled automation—most users rely on the device’s built-in app center. This avoids the cost of pulling images, planning directories, and configuring parameters. However, it usually adopts the most convenient permission model: the container process starts as a high-privilege identity (root, UID 0).

    The problem is that every file created by processes inside the container inherits that identity. When the host exposes configuration, log, and task definition directories to the container through bind mounts, those files end up owned by a high-privilege account on the host side. This produces a typical class of hard-to-diagnose failures:

    • After changing model or conversation parameters and restarting, the configuration is overwritten by the old version, appearing as “changes don’t take effect.”
    • The messaging gateway becomes unresponsive, and the kernel denies the process when it tries to read session credentials.
    • Scheduled tasks silently skip at the planned time because the scheduler cannot write state files.
    • Log output reports permission denied, cutting off troubleshooting clues.
    • The service keeps drifting between local inference and cloud APIs, and configuration items fail to take effect consistently.

    All of these symptoms share a single root cause: a mismatch between the owner of the data directories and the runtime identity. This is not an occasional bug but an inevitable result of a flawed permission model. That is why “restart it” or “reinstall it” only provides temporary relief, and the problem is bound to return.

    2. Root Cause Analysis: Three Layers of Permission Pollution

    The first layer is identity. Containers run as UID 0 by default, and the “privilege” inside the container maps directly onto host filesystem semantics, so every newly created file carries a 0:0 owner.

    The second layer is files. Persistent artifacts—configuration files, token caches, session databases, task definitions, skill scripts—land in bind-mounted directories, and their ownership is fixed to the high-privilege account. A normal runtime identity subsequently has only read permission, not write permission.

    The third layer is behavior. When a service starts and finds the configuration unwritable, it typically follows a fallback path to built-in defaults. This triggers a chain reaction: configuration reverts automatically, the model switches to a remote API, and tasks are deemed unnecessary to run. Understanding this chain is key to seeing that the fix must be applied at the identity layer, rather than repeatedly cleaning files.

    3. Solution Overview: Replace App Center Deployment with a Self-Managed Container and an Explicit Runtime Identity

    The core idea is to move away from the device app center’s managed installation, create the container yourself, and explicitly specify the runtime user and group (UID/GID) at creation time, so the container process accesses data directories as a normal identity. The approach has three key properties:

    1. Full data inheritance. The old and new containers mount the same data directory, so configuration, login tokens, session records, task definitions, and custom skills are all preserved, with no need to log in again or re-tune parameters.
    2. Controllable and rollback-friendly risk. If the new container behaves abnormally, simply stop the self-managed container and restart the original managed container to return to the pre-migration state, with rollback taking seconds.
    3. A one-time permanent fix for permissions. From then on, all newly created files belong to a normal user, no high-privilege ownership is produced, and the source of pollution is cut off.

    4. Implementation Steps

    Step 1: Inventory the Current State and Back Up Data

    First confirm the real path and ownership of the data directory, then make an offline copy of three key data types: configuration, tokens, and task definitions. Example commands:

    1. # Check data directory ownership and permissions
    2. ls -ln /vol1/appdata/assistant
    3. # Create a timestamped backup that preserves ownership information
    4. tar --numeric-owner -czf assistant_$(date +%F).tgz /vol1/appdata/assistant

    The backup is the safety foundation of the entire approach. Even if a later judgment is wrong, you can fully restore from the copy.

    Step 2: Determine the Runtime Identity and Correct Ownership

    Choose an unused normal UID/GID (for example, 1000:1000), and recursively assign the data directory to that identity:

    1. chown -R 1000:1000 /vol1/appdata/assistant
    2. chmod -R u+rwX,g+rX /vol1/appdata/assistant

    Note that uppercase X only adds the execute bit to directories and already-executable files, avoiding accidentally marking ordinary data files as executable.

    Step 3: Create a Self-Managed Container with an Explicit Identity

    When creating the container, constrain the runtime identity, resources, and restart policy at the same time:

    1. create_container \
    2. --user 1000:1000 \
    3. --network bridge \
    4. --memory 4g \
    5. --restart unless-stopped \
    6. --volume /vol1/appdata/assistant:/data \
    7. --env TZ=Asia/Shanghai \
    8. assistant:local

    Three parameters matter most: --user determines file ownership; --restart ensures the container is automatically brought back up after an abnormal exit; the data volume points to the same directory backed up in Step 1, achieving lossless configuration inheritance.

    Step 4: Verification and Observation

    After startup, check the following in order: whether configuration changes persist after a restart; whether logs remain continuously writable; whether scheduled tasks leave execution records in the state file on schedule; and whether the choice between local inference and remote APIs is stable. It is recommended to verify with one minimal closed-loop test case:

    1. # Write a probe file to confirm the runtime identity is in effect
    2. docker exec assistant sh -c 'id; touch /data/.perm_probe && ls -ln /data/.perm_probe'

    If the probe file’s owner shows as 1000, the identity constraint is in effect.

    Step 5: Cleanup and Consolidation

    Once stability is confirmed, disable the original managed container to avoid two instances writing to the same directory and causing data races. At the same time, record the UID/GID, data paths, and resource limits in internal documentation to form a deployment baseline.

    5. Rollback Plan

    Rollback takes only three steps: stop the self-managed container; confirm the data directory is not held by the self-managed instance; restart the original managed container. Because the data is the same, rollback does not involve data migration. Note that if the self-managed instance has already generated new files owned by a normal user, the original instance may face write restrictions after rollback. A single chown to correct ownership is enough, at very low cost.

    6. Advanced Recommendations: From “Fix” to “Prevention”

    • Standardize identity: fix all self-managed containers to the same UID/GID to avoid cross-service pollution.
    • Separate directory responsibilities: mount configuration, logs, tasks, and cache in separate directories so permission policies can be tightened individually.
    • Prioritize least privilege: if a normal identity can solve the problem, do not enable privileged mode; when privilege is truly required, limit it to specific capabilities.
    • Complete observability: bring log persistence and state file writes into monitoring and alerting so permission errors surface immediately.
    • Keep change records: put container creation parameters under version control so any adjustment is auditable and reproducible.

    7. When This Applies

    If your deployment involves two or more of local large model inference, home device integration, instant messaging remote control, and scheduled automation, and the data directory resides on a bind-mounted path, permission pollution is almost unavoidable. In that case, create the container yourself with a normal identity from the first launch, rather than migrating after failures recur. Conversely, if the service has no persistent write needs, the risk of managed installation is acceptable and no additional changes are needed.

    8. Conclusion

    The essence of permission pollution is leaving a basic engineering issue—the runtime identity—to default configuration. Explicitly specifying a UID/GID is not complex, yet it eliminates an entire class of intermittent failures that are hard to locate. Setting identity, directories, and observability at the start of a project significantly reduces later troubleshooting costs.