Skip to content

_is_inside_container() fails to detect Kubernetes (K3s) pods falls through to Docker mode #1

@Tomenatore

Description

@Tomenatore

Description

When running Agent Zero as a Kubernetes Deployment (K3s with containerd runtime), the Context Engine plugin's _is_inside_container() function in execute.py returns False. This causes the setup to fall into the "Host (Docker mode)" branch, which then fails with:

Environment:  Host (Docker mode)
Checking Docker...
✗ Docker is not available.
  Install Docker Desktop: https://www.docker.com/products/docker-desktop/

The embedded mode (which would work fine without Docker) is never reached.

Environment

  • Agent Zero: agent0ai/agent-zero:v1.3
  • Plugin version: commit 187b169
  • Runtime: K3s v1.31 single-node cluster (RHEL 9, containerd runtime)
  • Container runtime: containerd (not Docker)
  • cgroup version: cgroupv2

Root Cause

_is_inside_container() checks three signals, all of which fail in a K3s/containerd environment:

  1. /.dockerenv — Does not exist (containerd doesn't create it)
  2. DOCKER_CONTAINER / container env vars — Not set (only KUBERNETES_SERVICE_HOST etc. are present)
  3. /proc/1/cgroup — Contains only 0::/ (cgroupv2 format), so neither "docker" nor "kubepods" is found

Additionally, there's a Python bug in the cgroup check:

with open("/proc/1/cgroup", "r") as f:
    return "docker" in f.read() or "kubepods" in f.read()

f.read() is called twice — the first call consumes the entire file, so the second f.read() always returns "". Even on cgroupv1 systems where the file contains "kubepods", the check would still fail.

Suggested Fix

def _is_inside_container() -> bool:
    """Detect if we're running inside a Docker/Kubernetes container."""
    if os.path.exists("/.dockerenv"):
        return True
    if os.environ.get("DOCKER_CONTAINER") or os.environ.get("container"):
        return True
    # Kubernetes detection
    if os.environ.get("KUBERNETES_SERVICE_HOST"):
        return True
    try:
        with open("/proc/1/cgroup", "r") as f:
            content = f.read()
            return "docker" in content or "kubepods" in content
    except (FileNotFoundError, PermissionError):
        pass
    return False

Key changes:

  • Add KUBERNETES_SERVICE_HOST check — This env var is always injected by Kubernetes into every pod
  • Fix double f.read() bug — Read file content once into a variable

Workaround

Setting the container environment variable in the Kubernetes Deployment spec works as a temporary workaround:

env:
  - name: container
    value: "kubernetes"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions