New Tool Radar

AI Agents & Automation

How to Run AG2 Code Execution Inside Docker

Run AG2 agent code execution inside Docker so generated code cannot touch your files directly.

Schematic with three steps: Turn on Docker code execution; Run a harmless code task and check; Confirm a file write stays inside the.

An AG2 user proxy agent executes code blocks it receives by default, and running that code on your own machine means agent-written code can read or change your files. Running code execution in Docker is safer than running it directly: the generated code runs inside a container, away from your own files. Set the code execution config to use Docker before the first run, and confirm the container is actually being used.

What you need

  • Python with the autogen package installed
  • Docker installed and running on the machine
  • An AG2 user proxy agent that executes code

Step 1: Turn on Docker code execution

Set the user proxy agent’s code execution config to use Docker, so generated code runs inside a container instead of on your machine.

Step 2: Run a harmless code task and check where it ran

Ask the agent to run a small code task that prints its working directory and lists its files, and confirm the paths are container paths, not your machine’s files.

Step 3: Confirm a file write stays inside the container

Ask the agent to write a test file, then check your own machine: the file should not appear outside the container. If it does, Docker execution is not actually on.

Worked example: synthetic code task

The table below is a synthetic example. It shows the expected result of container execution; no code was run.

Check Expected Result
Code runs in container Yes Pass
No file written to host 0 files Pass
Result returned to chat Yes Pass

Expected result (UNTESTED). the agent’s code runs inside Docker, returns its result to the chat, and writes nothing to the host machine. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • Code execution is on by default in the user proxy agent, so pass a disabled config unless the agent should run code.
  • Using Docker is safer than running generated code directly, but a container is not a guarantee against every risk.
  • Confirm the container is actually used, because a config that silently falls back to local execution looks the same in the chat.

Sources

See all