Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Build a Repeatable AI Agent Test Lab with Docker

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Testing AI agents reliably is hard: they depend on specific model versions, APIs, and environment variables, and a small change can break everything. This guide shows you how to build a repeatable AI agent test lab using Docker Compose, so you can spin up a consistent testing environment on any machine, run your agent against a controlled set of tools and mock services, and tear it down without leaving residue. You'll end up with a self-contained lab that you can run with a single command, making your tests reproducible and your debugging faster.

What You'll Need

  • Docker installed (Docker Desktop on Windows/macOS, or Docker Engine on Linux).
  • Docker Compose (usually included with Docker Desktop; on Linux you may need to install the plugin separately).
  • Basic familiarity with the command line.
  • Your AI agent's code, or a simple test agent you can write in a few minutes.
  • A text editor for writing Dockerfiles and compose files.

Step-by-Step

  1. Design your lab topology. Decide what your agent needs to talk to. For a typical test lab, you'll want: (1) the agent itself (a container running your code), (2) a mock API or service that simulates external dependencies, (3) a database or cache for state, and (4) a test runner that executes your test suite. Keep it minimal—you can always add more services later.
  2. Create a project directory. Make a folder for your lab, e.g., ai-agent-test-lab. Inside, create subdirectories: agent/, mock-service/, tests/, and config/. This keeps your files organized and makes the Docker build context clear.
  3. Write a Dockerfile for your agent. In agent/Dockerfile, start from a Python or Node image depending on your agent. Install dependencies, copy your code, and set the entrypoint to run your agent. For example:
    FROM python:3.12-slim
    WORKDIR /app
    COPY requirements.txt .
    RUN pip install --no-cache-dir -r requirements.txt
    COPY . .
    CMD ["python", "agent.py"]
    Make sure your agent reads configuration from environment variables (like API keys, model names, endpoints) so you can inject them via Compose.
  4. Create a mock service. If your agent calls an external API, build a simple mock server (e.g., using Flask or Express) that returns canned responses. Put its Dockerfile in mock-service/. For example, a Flask app that returns a JSON response for a /chat endpoint. This lets you test your agent without hitting real APIs, which is faster and more reliable.
  5. Set up a test runner. Create a separate container that runs your test suite (pytest, Jest, etc.). Its Dockerfile can be similar to the agent's, but with a different CMD that runs the tests. Mount your test files as a volume so you can edit tests without rebuilding.
  6. Write the docker-compose.yml. At the root, create a docker-compose.yml that defines all services. Here's a minimal example:
    version: '3.8'
    services:
      mock:
        build: ./mock-service
        ports:
          - "5000:5000"
      agent:
        build: ./agent
        environment:
          - MOCK_URL=http://mock:5000
          - OPENAI_API_KEY=${OPENAI_API_KEY}
        depends_on:
          - mock
      tests:
        build: ./tests
        volumes:
          - ./tests:/app/tests
        environment:
          - MOCK_URL=http://mock:5000
        depends_on:
          - mock
          - agent
    Use the service name as the hostname (e.g., mock) so containers can talk to each other on the internal network. Pass secrets via environment variables or a .env file.
  7. Add a .env file. Create a .env file in the root to store sensitive values like API keys. Compose automatically reads this file. For example: OPENAI_API_KEY=sk-.... Do not commit this file to version control.
  8. Build and run. Run docker compose up --build. This builds all images and starts the services. You should see logs from each container. If everything is correct, your agent will start, and the mock service will respond to its requests.
  9. Run your tests. To run the test suite, execute docker compose run --rm tests. This runs the tests container once and then removes it. The --rm flag keeps things clean.
  10. Make it repeatable. Add a Makefile or shell script with common commands: make up, make test, make down, make clean. This ensures everyone on your team uses the same commands, reducing friction.
  11. Version everything. Commit your Dockerfiles, compose file, and test code to version control. Use specific base image tags (e.g., python:3.12-slim instead of python:latest) to avoid surprises when images update.

Common Mistakes

  • Hardcoding URLs inside the agent. If your agent uses localhost to reach the mock service, it won't work inside Docker. Use the service name (http://mock:5000) instead.
  • Not using volumes for test code. Without a volume, every test change requires rebuilding the image. Mount your test directory to avoid this.
  • Ignoring dependency ordering. If your agent starts before the mock service is ready, it may crash. Use depends_on with health checks, or add retry logic in your agent.
  • Leaving secrets in the compose file. Never hardcode API keys in docker-compose.yml. Use environment variables or a .env file.
  • Forgetting to rebuild. After changing your agent's code, you must rebuild the image (docker compose build agent) or use a volume to sync code. Otherwise, you're testing stale code.

Tips

  • Use health checks in your compose file to ensure the mock service is ready before the agent starts. For example, healthcheck: test: ["CMD", "curl", "-f", "http://localhost:5000/health"].
  • Keep your mock service realistic: include latency, occasional errors, and rate limits to test how your agent handles failures.
  • Use a separate network for your lab to avoid conflicts with other Docker projects. Compose creates a default network, but you can define a custom one.
  • Tag your images with a version (e.g., myagent:1.0) to make it easier to roll back if something breaks.
  • For stateful tests (e.g., database), use a named volume so data persists between runs—but be aware this can make tests non-repeatable. For pure repeatability, use a fresh database each time.

Troubleshooting

  • Container cannot reach another service. Check that you're using the service name as the hostname, and that both services are on the same Docker network. Run docker compose ps to see the network.
  • Port conflicts. If port 5000 is already in use, change the host port mapping (e.g., "5001:5000").
  • Agent crashes on startup. Look at the logs with docker compose logs agent. Common causes: missing environment variables, wrong model name, or the mock service not ready. Add retry logic or a health check.
  • Tests fail due to stale data. If your tests depend on a database, use a fresh volume or run docker compose down -v to remove volumes before re-running.
  • Build is slow. Use Docker's layer caching by ordering your Dockerfile: copy requirements first, then run pip install, then copy the rest of the code. This way, dependency installation is cached unless requirements change.

Frequently Asked Questions

Do I need a GPU for this?

No. Most AI agent tests use API-based models (like OpenAI or Anthropic) or small local models that run on CPU. If you need a GPU for local models, you can add gpus: all to the agent service, but it's not required for a basic lab.

How do I test with different model versions?

Pass the model name as an environment variable (e.g., MODEL_NAME) and override it in your compose file or .env. You can run multiple test containers with different values to compare behavior.

Can I use this with LangChain or LlamaIndex?

Yes. The setup is framework-agnostic. Just install the framework in your agent's Dockerfile and configure it via environment variables.

How do I mock external APIs that require authentication?

Your mock service can check for a dummy API key in the request header. Set that key in the agent's environment variables. This ensures your agent is sending the right headers.

What if my agent uses a vector database?

Add a service for the vector DB (e.g., Chroma or Qdrant) in your compose file. Mount a volume for persistence, or use an in-memory mode for tests.

How do I clean up after testing?

Run docker compose down to stop and remove containers. Add -v to also remove volumes if you want a completely clean slate. This is essential for repeatability.