Docker for AI Coding Agents

A Beginner’s Guide to Safe, Reproducible Environments

Alexander Rieber (@AlexRieber) · Ulm University

2026-02-25

Workshop Overview

Part Topic Time
🐳 What is Docker? Why a Sandbox? 10 min
💻 Installing Docker Desktop 10 min
📦 Building Your First Image 15 min
🏃 Running Containers & Volumes 15 min
🤖 Setting Up Claude Code, Codex & Gemini CLI 15 min
🔒 Safety: Partitions & Disk Limits 10 min
🛠️ Hands-On: Run Your First Agent 15 min

Companion handout: Docker Setup Guide for AI Coding Agents

Part 1: What is Docker?

AI Agents Need to Run Code

An AI coding agent is not just a chatbot. It can:

  • Read & write any file it can access
  • Run arbitrary shell commands
  • Download data from the internet
  • Install software packages

Would you let a stranger do all this on your personal laptop?

The Solution: A Sandbox

😰

Without Docker

Agent runs on your system.
Can access everything.
Mistakes affect YOU.

😌

With Docker

Agent runs in a container.
Cannot escape the sandbox.
Your system stays safe.

Docker = an isolated mini-computer inside your real computer.

Three Concepts You Need

📄
Dockerfile = Recipe
"Install R, Python, Claude Code..."
docker buildx build (~20--30 min)
📦
Image = Frozen Meal
Complete environment snapshot, ready to use
docker compose up (~10 sec)
🏃
Container = Meal on Your Plate
A running instance where you work

Why Docker for AI Agents?

🔒 Safety
Agent cannot touch your files or system.
🔄 Reproducibility
Everyone gets the exact same environment.
🧹 Easy Cleanup
Delete container = clean system again.
📤 Portability
Share Dockerfile = share entire setup.

Part 2: Installing Docker

Docker Desktop is Free for Education

🎓 Good news: Docker Desktop is free for educational use.
Students and educators at universities can use it without any licensing concerns.

Two options for Windows users:

Docker Desktop Docker Engine in WSL 2
GUI ✅ Yes ❌ CLI only
Setup Easy (installer) Moderate (manual)
Best for Beginners Experienced users

Mac users: Docker Desktop is the standard choice.

Windows + Docker Desktop

Step 1: Install WSL 2 (PowerShell as Admin)

wsl --install

Restart. Set a Linux username and password.

Step 2: Download Docker Desktop from docker.com/products/docker-desktop

Run installer, keep defaults, restart if asked.

Step 3: Configure Docker Desktop

  • Settings → General: ✅ “Use WSL 2 based engine”
  • Settings → Resources → WSL Integration: ✅ Ubuntu

⏱ Total: ~15 min including restarts

Windows: Verify & Configure

Step 4: Open Ubuntu (NOT PowerShell!) and run:

docker run hello-world

You should see “Hello from Docker!”

Step 5: Adjust resources in Docker Desktop → Settings → Resources:

Setting Recommended
CPUs 4+
Memory 8–16 GB
Disk 60+ GB

These are upper limits — Docker only uses what it needs.

Mac: Step by Step

Step 1: Check your chip (Apple menu → About This Mac)

  • M1/M2/M3/M4 = Apple Silicon
  • Otherwise = Intel

Step 2: Download Docker Desktop from docker.com/products/docker-desktop

Choose your chip type. Open .dmg, drag to Applications.

Step 3: Open Docker, grant permissions, accept agreement.

Step 4: Open Terminal and verify:

docker run hello-world

⏱ Total: ~10 min

Alternative: Docker Engine in WSL 2 (No Desktop)

For Windows users who prefer a lightweight, CLI-only setup:

Step 1: Install WSL 2 (same as before: wsl --install, restart)

Step 2: Open Ubuntu terminal, install Docker Engine:

# Add Docker repo + install (full commands in handout)
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli \
  containerd.io docker-buildx-plugin docker-compose-plugin

Step 3: Start Docker manually each session:

sudo service docker start

⏱ ~15 min · Full step-by-step in the handout, Part 2 Option B

WSL 2 Docker Engine: Tips

Auto-start Docker daemon
echo 'sudo service docker start' >> ~/.bashrc
Run docker without sudo
sudo usermod -aG docker $USER
Then close and re-open your terminal.
Everything else stays the same
All docker build, docker compose, and docker run commands work identically — Docker Desktop or Engine in WSL 2.

Part 3: Building Your First Image

What Goes Into the Dockerfile

Layer 1: Ubuntu 24.04 base
+
Layer 2: System tools (curl, wget, git, LaTeX)
+
Layer 3: R + econometrics packages
+
Layer 4: Python + numpy, pandas, matplotlib
+
Layer 5: Node.js + Claude Code + Codex + Gemini CLI

Docker caches every layer. Change the last layer only? Rebuild in seconds.

Installing R

# Install R
RUN apt-get update && \
    apt-get install -y r-base r-base-dev && \
    rm -rf /var/lib/apt/lists/*

# Install R packages (~10-15 min)
RUN Rscript -e "install.packages(c(
    'tidyverse', 'fixest', 'modelsummary',
    'haven', 'data.table', 'ggplot2'
    ), repos='https://cloud.r-project.org', Ncpus=4)"

R packages are pre-installed in the Dockerfile — you wait once, not every time.

Installing Python, AI Agents, and Stata?

# Python
RUN apt-get install -y python3 python3-pip
RUN pip3 install numpy pandas matplotlib

# Node.js (needed for Codex and Gemini CLI)
RUN curl -fsSL https://deb.nodesource.com/setup_24.x | bash -
RUN apt-get install -y nodejs

# Claude Code
RUN curl -fsSL https://claude.ai/install.sh | bash

# OpenAI Codex
RUN npm install -g @openai/codex

# Google Gemini CLI
RUN npm install -g @google/gemini-cli

Stata is commercial software. You need a Linux license from your university, or use R equivalents (fixest, plm, rdrobust). See the handout.

Customize It — Ask an LLM!

The provided Dockerfile is a starting point. Want to add packages or tools?

Paste the Dockerfile into your favourite LLM and ask:

“I need to add the sf and terra R packages for geospatial analysis. How should I modify this Dockerfile?”

LLMs are excellent at writing and debugging Dockerfiles — they know the right system libraries, dependency chains, and best practices.

Great way to learn Docker while getting a setup tailored to your research.

Building the Image

cd ~/coding-agent-workshop
docker buildx build -t coding-agent .
First build
~20--30 min ☕
Rebuild (cached)
< 10 seconds ⚡

Part 4: Containers & Volumes

Starting a Container

# Start in background
docker compose up -d

# Enter the container
docker exec -it my-agent bash
you@laptop:~$ docker exec -it my-agent bash
root@a1b2c3d4:/home/agent# R --version | head -1
R version 4.4.1 (2024-06-14)
root@a1b2c3d4:/home/agent# claude --version
Claude Code v2.x.x
root@a1b2c3d4:/home/agent# gemini --version
Gemini CLI v0.x.x
root@a1b2c3d4:/home/agent# exit
you@laptop:~$ ← back on host

Volume Mounts: Host ↔︎ Container

💻 Your Computer
~/project/
  workspace/    ← shared
  output/       ← shared
  CLAUDE.md     ← shared (read-only)
  my-photos/    ← NOT shared
  passwords.txt ← NOT shared
🔗
🐳 Container
/home/agent/
  project/   ← agent works here
  output/    ← results go here
  CLAUDE.md  ← agent reads this

  (everything else is
   invisible to the agent)

Only explicitly mounted folders are visible. Everything else stays private.

The docker-compose.yml

services:
  agent:
    image: coding-agent
    container_name: my-agent
    stdin_open: true
    tty: true
    mem_limit: 16g            # Max RAM
    cpus: 4                   # Max CPU cores
    pids_limit: 512           # Max processes
    volumes:
      - ./workspace:/home/agent/project
      - ./output:/home/agent/output
      - ./CLAUDE.md:/home/agent/CLAUDE.md:ro
      - agent-auth:/root/.claude

volumes:
  agent-auth:

Part 5: AI Coding Agents (Three Options)

Claude Code: Option A — API Key

Pay per use. You control spending via the API dashboard.

  1. Go to platform.claude.com
  2. Create account → “API Keys” → “Create Key”
  3. Add $5–10 billing credit
# Inside the container:
export ANTHROPIC_API_KEY="sk-ant-your-key-here"
claude

The key starts with sk-ant-... — save it somewhere safe.

Claude Code: Option B — Max Subscription

Flat monthly fee. No per-token charges.

Subscribe at claude.ai (Pro $20/mo, Max $100–200/mo)

# Inside the container:
claude
⚠ Authentication required
To sign in, open this URL in your browser:
https://claude.ai/login?code=ABCD-1234-EFGH

Waiting for authentication...
✓ Authenticated successfully!

Copy URL → open in browser → log in → done. Saved in Docker volume.

OpenAI Codex

Open source (Apache 2.0) — github.com/openai/codex

# Inside the container:
export OPENAI_API_KEY="sk-your-key-here"
codex

Get a key at platform.openai.com → API Keys

For non-interactive use:

codex exec "fix the failing tests" --full-auto

Inside Docker, use --sandbox danger-full-access — the container IS your sandbox.

Google Gemini CLI

Open source (Apache 2.0) — github.com/google-gemini/gemini-cli

# Inside the container:
export GEMINI_API_KEY="your-key-here"
gemini

Get a key at aistudio.google.com → API Keys

Or use for free with your personal Google account (60 req/min):

gemini          # Browser login on first run

Verify Everything Works

root@container:~# R --version | head -1
R version 4.4.1 (2024-06-14)
root@container:~# python3 --version
Python 3.10.12
root@container:~# claude --version
Claude Code v2.x.x
root@container:~# codex --version
0.104.0
root@container:~# gemini --version
Gemini CLI v0.x.x

Now start working:

claude --verbose

Part 6: Safety

The Risk: Filling Up Your Disk

😱 Without limits

Agent downloads 100 GB.
System drive fills up.
Computer freezes. Bad day.

😌 With a disk limit

Agent hits 30 GB limit.
"No space left on device".
Your system is fine.

Solution: Give Docker its own storage area with a hard size cap.

Three Levels of Protection

Option A: Move Docker Storage to Another Drive
Settings → Resources → Disk image location (Mac) or WSL export/import (Win)
Easy
Option B: Dedicated Disk Image for Docker
Fixed-size virtual disk (e.g., 60 GB) — hard cap on all Docker storage
Advanced
Option C: Size-Limited Workspace Folder
Create a 30 GB disk image, mount as workspace volume
Medium

Workshop recommendation: Option A is sufficient. See handout Part 14 for Options B & C.

Option C: Size-Limited Workspace

Mac:

hdiutil create -size 30g -fs APFS -volname AgentWorkspace \
  -type SPARSE ~/agent_workspace.sparseimage
hdiutil attach ~/agent_workspace.sparseimage
# Use /Volumes/AgentWorkspace in docker-compose.yml

Windows (WSL):

dd if=/dev/zero of=~/agent_workspace.img bs=1M count=30720
mkfs.ext4 ~/agent_workspace.img
mkdir -p ~/project/workspace
sudo mount -o loop ~/agent_workspace.img ~/project/workspace

Agent fills up the 30 GB? It stops. Your system stays safe.

Option A: Move Docker to a Separate Drive

Mac: Docker Desktop → Settings → Resources → Disk image location → Browse → select external drive. Done.

Windows (PowerShell as Admin):

wsl --shutdown
wsl --export docker-desktop-data D:\DockerData\backup.tar
wsl --unregister docker-desktop-data
wsl --import docker-desktop-data D:\DockerData \
    D:\DockerData\backup.tar
del D:\DockerData\backup.tar

All Docker data now lives on D:\. Your C:\ drive is protected.

Safety Summary

Layer Protects Against Effort
Docker container Agent escaping to your system Built-in
Resource limits RAM/CPU exhaustion Easy
Volume mounts Unwanted file access Easy
Read-only (:ro) Config file modification Easy
Option A: Separate drive Filling your system drive Easy
Option B: Dedicated disk image All Docker storage Advanced
Option C: Limited workspace Data explosion Medium

Part 7: Hands-On

Quick Start: 9 Steps

  1. Install Docker Desktop (~10 min)
  2. Create project folder: mkdir -p ~/coding-agent-workshop
  3. Create Dockerfile (copy from handout Part 9)
  4. Create docker-compose.yml (copy from handout Part 5)
  5. Build the image: docker buildx build -t coding-agent .
  6. Start the container: docker compose up -d
  7. Enter the container: docker exec -it my-agent bash
  8. Authenticate your AI agent(s) (API key or subscription)
  9. Start working: claude, codex, or gemini

See the handout for detailed instructions at each step.

Try It: Give the Agent a Task

docker exec -it my-agent bash
claude --verbose

Then type:

Write an R script that loads the mtcars dataset, runs a regression of mpg on wt and hp, and saves a nice ggplot to output/regression_plot.pdf

Watch it plan, write code, run it, see errors, fix them, and produce output.

Compare All Three Agents

# Claude Code
claude -p "Analyze mtcars: regression of mpg~wt+hp, save plot"

# OpenAI Codex
codex exec "Analyze mtcars: regression of mpg~wt+hp, save plot"

# Google Gemini CLI
gemini -p "Analyze mtcars: regression of mpg~wt+hp, save plot"

Check output/ on your host for results!

All three agents can:

  • Read and write files
  • Run R, Python, bash commands
  • Install packages if needed
  • Iterate on errors automatically

Cheat Sheet

🐳 Docker
docker compose up -d          # Start
docker exec -it my-agent bash # Enter
docker compose down           # Stop
docker ps                     # List
docker stats my-agent         # Monitor
🤖 Inside Container
claude                  # Claude Code
claude --model opus     # Opus model
codex                   # OpenAI Codex
gemini                  # Gemini CLI
R / Rscript script.R    # Run R
python3                 # Run Python

Summary

🐳
Docker = Safety. Container is a sandbox. Agent cannot touch your system.
📄
Dockerfile = Recipe. R, Python, Claude Code, Codex, Gemini CLI --- all in one file.
🔗
Volumes = Bridges. Only mounted folders are visible. Everything else is private.
🔒
Limits protect you. RAM, CPU, disk caps keep your laptop responsive.
🤖
Three agents, one container. Claude Code + Codex + Gemini CLI side by side.

Resources

Resource Link
Workshop Materials github.com/AlexRieber/Workshops/Docker
Docker Desktop docker.com/products/docker-desktop
Claude Code Docs code.claude.com/docs
Anthropic API Keys platform.claude.com
OpenAI Codex github.com/openai/codex
OpenAI API Keys platform.openai.com
Google Gemini CLI github.com/google-gemini/gemini-cli
Google AI Studio aistudio.google.com

Questions? Let's set up your first container!