Heuristic:ARCANGEL0 EVA Docker Group Sg Workaround
| Knowledge Sources | |
|---|---|
| Domains | Infrastructure, Optimization |
| Last Updated | 2026-10-29 12:00 GMT |
Overview
Use `sg docker -c "<command>"` to run Docker commands with newly granted docker group membership without restarting the Mitsu process.
Description
When a non-root user is added to the `docker` group during Docker installation, the new group membership is not active in the current process's session. Normally, the user would need to log out and log back in. To avoid this, Mitsu wraps Docker commands with `sg docker -c` when it detects that the user is a member of the docker group but the group is not yet active in the current process. This allows SearXNG container operations to proceed immediately after Docker installation without requiring a restart.
Usage
Apply this heuristic whenever Docker commands need to run immediately after the user has been added to the docker group. The `_runtime_command` function in `services/searxng.py` checks if the user is in the docker group (via `grp.getgrnam`) but the group GID is not in `os.getgroups()`, and if so, wraps the command with `sg docker -c`.
The Insight (Rule of Thumb)
- Action: Wrap Docker commands with `sg docker -c "<command>"` when group membership is pending.
- Value: `sg` executes the command in a new shell with the docker group active.
- Trade-off: Slightly more complex command execution, but avoids requiring a process restart or re-login.
- Compatibility: Only applies to non-root users on systems with the `sg` utility. Root users and users with an active docker group run commands directly.
Reasoning
Linux group memberships are evaluated at login time and cached in the process's group list (`os.getgroups()`). Adding a user to a new group doesn't affect already-running processes. The `sg` command starts a new shell with the specified group active, providing a clean workaround. This is critical for Mitsu's UX because requiring a restart after Docker installation would break the setup flow.
Code Evidence
`_runtime_command` from `services/searxng.py:34-56`:
def _runtime_command(runtime: str, args: list[str]) -> list[str]:
"""Use a newly granted docker group without restarting Mitsu's process."""
command = [runtime, *args]
if runtime != "docker" or not hasattr(os, "geteuid") or os.geteuid() == 0:
return command
try:
import grp
docker_group = grp.getgrnam("docker")
except (ImportError, KeyError):
return command
if docker_group.gr_gid in os.getgroups() or os.getegid() == docker_group.gr_gid:
return command
if getpass.getuser() not in docker_group.gr_mem:
return command
sg = shutil.which("sg")
if not sg:
return command
return [sg, "docker", "-c", shlex.join(command)]
Docker group creation from `services/docker_installer.py:123-143`:
def _ensure_docker_group(manager, password, sudo, environment) -> None:
import grp
try:
grp.getgrnam("docker")
except KeyError:
create_group = ["addgroup", "-S", "docker"] if manager.executable == "apk" else ["groupadd", "-f", "docker"]
_run_as_admin(create_group, manager, password, sudo, False, environment, timeout=60)