December 1, 2026
Practical guide to sandboxing AI agents using Docker, gVisor, and Linux namespaces.

Sandboxing AI agents on Linux requires understanding the isolation hierarchy: from basic user namespaces to full microVMs. This guide covers the practical implementation of each layer with working configurations.

Isolation Hierarchy

Level Technology Isolation Overhead Use Case
1 User Namespaces UID/GID mapping ~0% Rootless containers
2 Docker + Capabilities Process + FS + Network ~1% Baseline containers
3 seccomp Syscall filtering ~2% Syscall hardening
4 AppArmor/SELinux Mandatory Access Control ~3% Policy enforcement
5 gVisor Userspace kernel ~10-20% Untrusted code
6 Kata Containers Lightweight VM ~2-5% Strong isolation
7 Firecracker MicroVM ~1-2% Multi-tenant, serverless

Level 1: User Namespaces (Rootless)

Map container root to unprivileged host user:

# /etc/subuid /etc/subgid (auto-configured on modern distros)
# user:100000:65536

# Podman (rootless by default)
podman run --rm -it ubuntu:24.04

# Docker rootless
dockerd-rootless-setuptool.sh install
systemctl --user start docker
docker run --rm -it ubuntu:24.04

Level 2: Docker Capabilities & Security Options

Minimal Capability Set

# Drop ALL, add only required
docker run --rm 
  --cap-drop ALL 
  --cap-add=CAP_DAC_OVERRIDE 
  --cap-add=CAP_NET_BIND_SERVICE 
  my-agent:latest

Essential Security Options

docker run --rm 
  --user 1000:1000 
  --read-only 
  --tmpfs /tmp:noexec,nosuid,nodev,size=100m 
  --tmpfs /var/cache:noexec,nosuid,nodev,size=50m 
  --security-opt no-new-privileges:true 
  --security-opt seccomp=/path/to/profile.json 
  --security-opt apparmor=docker-ai-agent 
  --network none 
  --memory 1g --cpus 1 
  --pids-limit 100 
  my-agent:latest
Option Purpose
--read-only Root FS immutable
--tmpfs Writable dirs, no exec/suid
no-new-privileges Block setuid escalation
seccomp Syscall filter
apparmor MAC policy
--network none No network by default
--memory/--cpus Resource quotas
--pids-limit Prevent fork bombs

Level 3: seccomp Profiles

Default Docker Profile

Docker blocks ~44 syscalls by default. View with:

docker run --rm alpine:latest cat /proc/self/status | grep Seccomp
# Seccomp: 2 (filtering)

Custom Restrictive Profile

Create seccomp-ai-agent.json:

{
  "defaultAction": "SCMP_ACT_ERRNO",
  "defaultErrnoRet": 1,
  "architectures": ["SCMP_ARCH_X86_64", "SCMP_ARCH_X86", "SCMP_ARCH_X32"],
  "syscalls": [
    {"names": ["read", "write", "readv", "writev", "pread64", "pwrite64", "close", "openat", "newfstatat", "lseek", "mmap", "mprotect", "munmap", "brk", "rt_sigaction", "rt_sigprocmask", "rt_sigreturn", "exit_group", "exit", "getpid", "gettid", "getuid", "geteuid", "getgid", "getegid", "set_tid_address", "futex", "nanosleep", "clock_gettime", "sched_yield", "prlimit64", "getrandom", "arch_prctl", "madvise", "statx", "ioctl", "fcntl", "dup", "dup2", "pipe2", "socket", "connect", "sendto", "recvfrom", "sendmsg", "recvmsg", "shutdown", "getsockopt", "setsockopt", "getpeername", "getsockname", "bind", "listen", "accept4", "epoll_create1", "epoll_ctl", "epoll_pwait", "eventfd2", "timerfd_create", "timerfd_settime"], "action": "SCMP_ACT_ALLOW"},
    {"names": ["clone", "clone3", "fork", "vfork", "execve", "execveat", "kill", "tkill", "tgkill"], "action": "SCMP_ACT_ERRNO", "errnoRet": 1},
    {"names": ["ptrace", "process_vm_readv", "process_vm_writev", "kcmp", "finit_module", "delete_module", "init_module", "bpf", "perf_event_open", "userfaultfd", "io_uring_setup", "io_uring_enter", "io_uring_register", "landlock_create_ruleset", "landlock_add_rule", "landlock_restrict_self", "memfd_create", "memfd_secret", "pidfd_open", "pidfd_send_signal", "pidfd_getfd", "process_madvise", "process_mrelease"], "action": "SCMP_ACT_ERRNO", "errnoRet": 1}
  ]
}

Apply:

docker run --security-opt seccomp=/path/to/seccomp-ai-agent.json ...

Generating Profiles Automatically

# Install oci-seccomp-bpf-hook
sudo apt install oci-seccomp-bpf-hook

# Or use docker-slim to generate
docker-slim build --target my-agent:latest --generate-seccomp

# Or trace with strace
strace -f -e trace=all -o trace.log docker run my-agent:latest
# Convert trace to seccomp profile

Level 4: AppArmor Profiles

Ubuntu AppArmor for Docker

# Check status
sudo aa-status

# Create profile: /etc/apparmor.d/docker-ai-agent
#include 

profile docker-ai-agent flags=(attach_disconnected,mediate_deleted) {
  #include 
  #include 

  # Read-only config
  /etc/ai-agent/** r,
  /host/config/** r,

  # Read-only data
  /host/data/** r,

  # Writable logs
  /host/logs/** rw,
  /host/logs/**/ rw,

  # Temp
  /tmp/** rw,
  /tmp/**/ rw,
  /var/cache/** rw,

  # Deny exec
  deny /{,usr/}bin/** ix,
  deny /{,usr/}sbin/** ix,
  deny /{,usr/}local/** ix,

  # Network
  network inet stream,
  network inet6 stream,
  network inet dgram,
  network inet6 dgram,

  # Capabilities
  capability,

  # DBus (if needed)
  # dbus (send, receive) bus=(session),
}

Load and apply:

sudo apparmor_parser -r /etc/apparmor.d/docker-ai-agent
docker run --security-opt apparmor=docker-ai-agent ...

SELinux (RHEL/Fedora)

# Check status
sestatus

# Custom policy module
cat < ai_agent.te
module ai_agent 1.0;
require {
  type container_t;
  type svirt_lxc_net_t;
  class file { read write open execute };
  class netlink_audit_socket { create };
}
allow container_t self:capability { dac_override sys_admin };
allow container_t svirt_lxc_net_t:dir { read write };
allow container_t container_t:file { read write execute };
EOF

checkmodule -M -m -o ai_agent.mod ai_agent.te
semodule_package -o ai_agent.pp -m ai_agent.mod
sudo semodule -i ai_agent.pp

# Apply
docker run --security-opt label=type:container_t ...

Level 5: gVisor (runsc)

Installation

# Ubuntu/Debian
curl -fsSL https://gvisor.dev/archive.key | sudo gpg --dearmor -o /usr/share/keyrings/gvisor-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/gvisor-archive-keyring.gpg] https://storage.googleapis.com/gvisor/releases release main" | sudo tee /etc/apt/sources.list.d/gvisor.list
sudo apt update && sudo apt install runsc

# Configure Docker
cat <

Runtime Options

# PTrace platform (software, works everywhere)
runsc --platform=ptrace ...

# KVM platform (hardware virt, faster, needs nested virt)
runsc --platform=kvm ...

# Debug
runsc --strace --log-format=json ...

Trade-offs

Platform Pros Cons
ptrace Works everywhere, no nested virt needed ~15-20% syscall overhead
kvm ~5% overhead, better isolation Requires nested virtualization, KVM

Level 6: Kata Containers

Installation

# Ubuntu
sudo apt install -y kata-runtime kata-containers

# Configure Docker
cat <

Kata Configuration


# /opt/kata/share/defaults/kata-containers/configuration.toml
[hypervisor.qemu]
  kernel = "/usr/share/kata-containers/vmlinux.container"
  initrd = "/usr/share/kata-containers/kata-containers-initrd.img"
  machine_type = "pc"
  default_vcpus = 1
  default_memory = 512
  block_device_driver = "virtio-scsi"
  enable_debug = false

[proxy.kata]
  enable_debug = false

Level 7: Firecracker

Used by AWS Lambda/Fargate. MicroVMs with minimal device model.

# Install Firecracker
wget https://github.com/firecracker-microvm/firecracker/releases/latest/download/firecracker-v1.5.0-x86_64.tgz
tar -xzf firecracker-v1.5.0-x86_64.tgz
sudo mv release-x86_64/firecracker /usr/local/bin/
sudo mv release-x86_64/jailer /usr/local/bin/

# Use with containerd + firecracker-containerd
# Or use Fly.io's firecracker-containerd

Network Isolation

No Network (Default)

docker run --network none ...

Restricted Network (CNI)

# Create isolated network
docker network create --driver bridge --subnet 172.30.0.0/16 --gateway 172.30.0.1 ai-agent-net

# Run with network policy
docker run --network ai-agent-net 
  --dns 1.1.1.1 --dns 8.8.8.8 
  my-agent:latest

Egress Control (nftables)

# Allow only specific destinations
sudo nft add table inet ai-agent-egress
sudo nft add chain inet ai-agent-egress output { type filter hook output priority 0 ; }
sudo nft add rule inet ai-agent-egress output ip daddr 10.0.0.0/8 counter accept
sudo nft add rule inet ai-agent-egress output ip daddr 172.16.0.0/12 counter accept
sudo nft add rule inet ai-agent-egress output ip daddr 192.168.0.0/16 counter accept
# Allow DNS
sudo nft add rule inet ai-agent-egress output udp dport 53 counter accept
# Deny all else
sudo nft add rule inet ai-agent-egress output counter drop

Filesystem Isolation

Bind Mounts

# Read-only config
-v /host/config:/app/config:ro

# Read-only data
-v /host/data:/app/data:ro

# Writable logs (with size limit)
--tmpfs /app/logs:noexec,nosuid,size=100m

# Writable cache (with size limit)
--tmpfs /app/cache:noexec,nosuid,size=50m

# No access to host filesystem
# (no -v /:/host or similar)

OverlayFS for Layered FS

# Lower: read-only base image
# Upper: container-specific writes
# Work: overlay work directory
# Merged: unified view
docker run --storage-opt overlay.size=1G ...

Complete Compose Example

# docker-compose.yml
version: '3.8'

services:
  ai-agent:
    image: my-ai-agent:latest
    runtime: runsc  # or kata-runtime
    user: "1000:1000"
    read_only: true
    tmpfs:
      - /tmp:noexec,nosuid,nodev,size=100m
      - /var/cache:noexec,nosuid,nodev,size=50m
      - /app/logs:noexec,nosuid,size=100m
    cap_drop:
      - ALL
    security_opt:
      - no-new-privileges:true
      - seccomp:/etc/docker/seccomp-ai-agent.json
      - apparmor:docker-ai-agent
    network_mode: none
    deploy:
      resources:
        limits:
          memory: 1G
          cpus: '1.0'
        reservations:
          memory: 512M
          cpus: '0.5'
    pids_limit: 100
    volumes:
      - /host/config:/app/config:ro
      - /host/data:/app/data:ro
    restart: unless-stopped
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

Testing Your Sandbox

Escape Attempts

# Test 1: Can we see host processes?
docker exec -it agent ps aux

# Test 2: Can we access host filesystem?
docker exec -it agent ls /host

# Test 3: Can we escalate privileges?
docker exec -it agent whoami
docker exec -it agent id
docker exec -it agent cat /proc/self/status | grep Cap

# Test 4: Can we make network connections?
docker exec -it agent curl -I https://google.com

# Test 5: Can we execute arbitrary commands?
docker exec -it agent bash -c 'echo test'

# Test 6: Can we access Docker socket?
docker exec -it agent ls -la /var/run/docker.sock

# Test 7: Can we load kernel modules?
docker exec -it agent lsmod

Falco Validation

# Run Falco against container
falco -c /etc/falco/falco.yaml -r /etc/falco/rules.d/ai-agent.yaml

# Should alert on:
# - Shell execution
# - Network connections
# - File writes outside mounts
# - Privilege escalation attempts

Production Hardening Checklist

Control Verified
Rootless container runtime ☐
All capabilities dropped ☐
Custom seccomp profile applied ☐
AppArmor/SELinux profile loaded ☐
gVisor/Kata runtime configured ☐
Read-only root filesystem ☐
Tmpfs for writable dirs (noexec,nosuid) ☐
Network isolated or restricted ☐
Resource limits (memory, CPU, pids) ☐
Bind mounts read-only where possible ☐
Falco monitoring active ☐
auditd rules loaded ☐
Image signed and verified ☐
SBOM generated ☐
Escape tests passing ☐

Related Tools on DevStackIO

References

Leave a Reply

Your email address will not be published. Required fields are marked *