liveBook · Manning

Container

As explained in AI Agents in Action, Second Edition

An isolated software execution environment that packages an application with its dependencies while sharing the host operating system kernel.

A container is an isolated environment that packages an application with its dependencies while sharing the host operating system kernel. Instead of embedding every agent in one application, you can run an agent API in a container and let other components call it. section 8.1 section 8.2

Containers support separation of concerns: a focused agent service can be upgraded, swapped, or scaled without changing every other component. A container often hosts one agent, though it can also host a multiple-agent workflow. A containerized microservice does not necessarily provide its own front end. section 8.1 section 8.2

Run an image-generator agent locally

Start with the agent’s Python web API and a Dockerfile in the same folder. With Docker Desktop running, build an image using `docker build -t image-generator:latest .`. Run it with `docker run --rm -p 8000:8000 -e OPENAI_API_KEY="your_key_here" image-generator:latest`. The image is the built package; the container is its running instance. The port mapping makes the service available locally on port 8000. Test the API with a request and verify the generated image. Manage API keys carefully. section 8.2.1 section 8.5 section 8.6

Listing 8.4 Dockerfile and generation prompt
{copy the Python web API or MCP code above}
Prompt: Generate a Dockerfile that containerizes the above application. The above application is called 02_app.py.    #1

FROM python:3.11-slim    #2

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PORT=8000

WORKDIR /app

RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential curl ca-certificates && \
    rm -rf /var/lib/apt/lists/*    #3

COPY requirements.txt /app/requirements.txt    #4
RUN if [ -f /app/requirements.txt ]; then \
    pip install --no-cache-dir -r /app/requirements.txt; \
    else \
    pip install --no-cache-dir "uvicorn[standard]" fastapi; \
    fi

COPY . /app

EXPOSE ${PORT}    #5
CMD ["sh", "-c", "uvicorn 02_app:app --host 0.0.0.0 --port ${PORT}"]

The example Dockerfile selects a Python base image, installs dependencies, copies the application, and starts its API server. section 8.2.1

For a container that remains available for management after stopping, run it with a name instead of `--rm`. For example, `--name image-gen -p 9000:8000` names the container and maps local port 9000 to its port 8000. Docker Desktop can then be used to start, stop, and inspect it. Before production, evaluate and modify the image as needed. section 8.2.1 section 8.5

When an application needs several services, Docker Compose defines them together in a YAML file. One local stack can run a voice web application alongside image and web agents, with separate port mappings and environment settings. Compose provides a single way to start and stop the stack rather than wiring up each container manually. section 8.2.2

Listing 8.5 docker-compose.yaml
version: "3.9"    #1
services:
  web:    #2
    build: ./web
    container_name: realtime-voice-web
    ports: ["8000:8000"]
    environment:    #3
      OPENAI_API_KEY: ${OPENAI_API_KEY}
      DEFAULT_MODEL: ${DEFAULT_MODEL:-gpt-4o-realtime-preview-2025-06-03}
      DEFAULT_VOICE: ${DEFAULT_VOICE:-alloy}
      ALLOWED_ORIGINS: ${ALLOWED_ORIGINS:-*}
    depends_on:
      - image_agent
      - web_agent

  image_agent:    #4
    build: ./image_agent
    container_name: realtime-image-agent
    ports: ["8001:8000"]
    environment:
      OPENAI_API_KEY: ${OPENAI_API_KEY}

  web_agent:    #5
    build: ./web_agent
    container_name: realtime-web-agent
    ports: ["8002:8000"]
    environment:
      OPENAI_API_KEY: ${OPENAI_API_KEY}
      WEB_AGENT_MODEL: ${WEB_AGENT_MODEL:-gpt-5-mini}

Compare the three service definitions, their port mappings, environment settings, and dependencies. section 8.2.2

Choose a container when a self-contained agent needs a separately managed service. For customer-facing work with cost or security implications, keep the agent behind your own authenticated backend API. Local Docker deployment is a starting point; larger deployments may use Kubernetes or cloud container hosting. section 8.1 section 8.2

How AI Agents in Action, Second Edition covers it

How deep it goes
Works through Docker image creation, local container management, and Compose orchestration, while deferring Kubernetes and cloud deployment.
Examples
Uses an image-generator API and a three-service voice, image, and web agent stack.
What it assumes
Assumes an existing agent API and requires Docker Desktop for the local build and run workflow.
What you can do after
Apply it via Docker and Docker Compose to build, run, test, and manage local agent services.
How it connects
Connects containerization with microservices, APIs, agent orchestration, and separation of concerns.

Also explained in

Compare how 29 books explain container →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents in Action, Second Edition. The section links point to the parts of the book it draws on.