Container Limits: Restrict AI Agent Access
In this article
This guide shows you how to use container execution limits to restrict what an AI agent can see and do on your system. By defining resource caps, network policies, and filesystem access, you reduce the risk of an agent reading sensitive files, reaching external services, or consuming excessive CPU and memory. You'll need a working container runtime (Docker, Podman, or containerd) and basic familiarity with command-line tools.
What You'll Need
- A Linux server or workstation with a container runtime installed (Docker Engine 20.10+ or Podman 3.0+ recommended).
- Root or sudo access to create containers and manage network rules.
- The AI agent's code or image (e.g., a Docker image with your agent's dependencies).
- Basic knowledge of the command line and YAML/JSON syntax for configuration files.
Step-by-Step
- Define the agent's required resources. Determine the maximum CPU, memory, and disk the agent should use. For example, if the agent processes text, 1 CPU core and 512 MB RAM might be enough; if it runs a model, adjust accordingly. Write these down—you'll use them in the container run command.
- Create a non-root user inside the container. Modify the agent's Dockerfile to add a user with minimal privileges. For example:
RUN useradd -r -s /bin/false agentuser USER agentuser
This prevents the agent from running as root, limiting its ability to modify system files. - Run the container with resource limits. Use the
--cpus,--memory, and--pids-limitflags to cap CPU, RAM, and process count. Example:docker run --cpus="1.0" --memory="512m" --pids-limit=100 --network=none my-agent-image
The--network=nonedisables all network access—critical if the agent shouldn't reach the internet. - Mount only necessary directories as read-only. Instead of giving the agent access to the host filesystem, mount specific folders with
:ro(read-only). For example:docker run -v /path/to/input:/data:ro -v /path/to/output:/out my-agent-image
The agent can read from/dataand write to/out, but cannot modify anything else. - Set filesystem isolation with tmpfs. If the agent needs to write temporary files, mount a tmpfs with a size limit:
docker run --tmpfs /tmp:rw,size=100m,noexec my-agent-image
Thenoexecflag prevents executing binaries from /tmp, reducing attack surface. - Disable inter-container communication. By default, containers on the same Docker network can talk to each other. Use
--network=noneor create an isolated network with--internalif the agent needs to talk to a specific service. Example:docker network create --internal my-internal-net docker run --network=my-internal-net my-agent-image
This blocks all external traffic while allowing communication only within that network. - Apply security options. Add
--security-opt no-new-privilegesto prevent privilege escalation, and--cap-drop=ALLto drop all Linux capabilities. Example:docker run --security-opt no-new-privileges --cap-drop=ALL my-agent-image
This ensures the agent cannot perform privileged operations even if it finds a vulnerability. - Test the container. Run the container with your agent and verify it can complete its task while respecting the limits. Check logs for permission errors or resource exhaustion. Use
docker statsto monitor CPU and memory usage in real time.
Common Mistakes
- Forgetting to set a non-root user. If the Dockerfile doesn't specify
USER, the agent runs as root inside the container, which can lead to host access via volume mounts. - Mounting host directories with write access. A read-write mount allows the agent to delete or modify files on the host. Always use
:rounless you explicitly need writes. - Using
--network=host. This gives the container full access to the host's network stack, defeating network isolation. Avoid it unless absolutely necessary. - Setting memory limits too low. If the agent crashes with OOM (out of memory) errors, you might have set the limit too low. Monitor actual usage and adjust.
- Ignoring PID limits. Without
--pids-limit, a runaway agent can spawn unlimited processes, exhausting system resources.
Tips
- Use a read-only root filesystem. Add
--read-onlyto the run command to prevent the agent from writing to its own filesystem, forcing all writes to tmpfs or mounted volumes. - Combine with seccomp profiles. A custom seccomp profile can block specific syscalls. Docker's default profile is good, but you can tighten it for your agent's needs.
- Log everything. Use Docker's logging driver to capture all agent output. This helps audit what the agent did.
- Use environment variables for secrets. Pass API keys via
--envor an env file, not by baking them into the image. - Regularly update the base image. Keep the agent's base OS and dependencies patched to reduce known vulnerabilities.
Troubleshooting
- Agent cannot write to output directory. Check that the mounted volume exists and has the correct permissions. If using read-only mounts, ensure the agent writes only to allowed paths.
- Network access is blocked but agent needs it. If the agent requires internet, use a proxy or a whitelist firewall. For Docker, you can use
--network=bridgeand then apply iptables rules to restrict egress. - Container exits immediately. Check the logs with
docker logs <container-id>. Common causes: missing dependencies, permission errors, or resource limits too low. - Agent runs slowly. CPU or memory limits might be too restrictive. Monitor with
docker statsand increase limits gradually until performance is acceptable. - Cannot run Docker commands without sudo. Add your user to the
dockergroup (if using Docker) or use rootless Podman to avoid sudo requirements.
Frequently Asked Questions
Can I limit the agent's access to specific network ports?
Yes, you can use iptables or Docker's network policies to allow only specific ports. For example, to allow only port 443, you can set up a custom bridge network and add iptables rules to drop all other outbound traffic.
What is the difference between --memory and --memory-swap?
--memory sets the hard limit on RAM, while --memory-swap limits the total memory plus swap. If you set --memory=512m --memory-swap=1g, the container can use 512 MB RAM and up to 512 MB swap.
How do I prevent the agent from reading host files?
Do not mount host directories unless necessary. If you must mount, use read-only mode (:ro) and mount only specific subdirectories, not the entire filesystem.
Can I use these techniques with Kubernetes?
Yes, Kubernetes offers similar controls via resource limits (CPU/memory), securityContext (runAsNonRoot, readOnlyRootFilesystem), and network policies. The concepts translate directly.
Is it safe to give the agent access to a GPU?
GPU access increases risk. Use NVIDIA's container toolkit to limit GPU memory and compute, and ensure the agent doesn't have unnecessary host access. Only grant GPU if the agent's task requires it.
What if the agent needs to access a database?
Instead of giving the agent direct network access, expose the database on an internal network and connect the container to that network. Use credentials passed via environment variables, and restrict the database user's permissions to only what the agent needs.