From 5a7c9c73d6c292ca654a82f3b8f0e9779b01cbb4 Mon Sep 17 00:00:00 2001 From: Ben Cherry Date: Tue, 12 Aug 2025 21:42:00 -0700 Subject: [PATCH] dockerfile --- .dockerignore | 54 +++++++++++++++++++++++++--------- Dockerfile | 80 +++++++++++++++------------------------------------ 2 files changed, 64 insertions(+), 70 deletions(-) diff --git a/.dockerignore b/.dockerignore index 331e543..b01251e 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,20 +1,48 @@ -*.egg-info -__pycache__ -.pytest_cache -.ruff_cache -.env -.env.* -.DS_Store -.idea -.venv -.vscode -*.pyc +# Python bytecode and artifacts +__pycache__/ +*.py[cod] *.pyo *.pyd +*.egg-info/ +dist/ +build/ + +# Virtual environments +.venv/ +venv/ + +# Caches and test output +.cache/ +.pytest_cache/ +.ruff_cache/ +coverage/ + +# Logs and temp files +*.log +*.gz +*.tgz +.tmp +.cache + +# Environment variables +.env +.env.* + +# VCS, editor, OS .git .gitignore +.gitattributes +.github/ +.idea/ +.vscode/ +.DS_Store + +# Project docs and misc README.md LICENSE -.github -tests/ +# Project tests +test/ +tests/ +eval/ +evals/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 9cd7886..d37738b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,77 +1,43 @@ -# This sample Dockerfile creates a production-ready container for a LiveKit voice AI agent # syntax=docker/dockerfile:1 -# Use the official UV Python base image with Python 3.11 on Debian Bookworm -# UV is a fast Python package manager that provides better performance than pip -# We use the slim variant to keep the image size smaller while still having essential tools -FROM ghcr.io/astral-sh/uv:python3.11-bookworm-slim +ARG PYTHON_VERSION=3.11 +# Fast Python builds using uv on Debian bookworm-slim +FROM ghcr.io/astral-sh/uv:python${PYTHON_VERSION}-bookworm-slim AS base -# Keeps Python from buffering stdout and stderr to avoid situations where -# the application crashes without emitting any logs due to buffering. +ARG UID=10001 + +# Ensures that logs are captured in realtime ENV PYTHONUNBUFFERED=1 -# Define the program entrypoint file where your agent is started -ARG PROGRAM_MAIN="src/agent.py" -ENV PROGRAM_MAIN=${PROGRAM_MAIN} - -# Create a non-privileged user that the app will run under. -# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#user -ARG UID=10001 +# Create unprivileged user RUN adduser \ --disabled-password \ --gecos "" \ - --home "/home/appuser" \ + --home "/app" \ --shell "/sbin/nologin" \ --uid "${UID}" \ appuser -# Install build dependencies required for Python packages with native extensions -# gcc: C compiler needed for building Python packages with C extensions -# python3-dev: Python development headers needed for compilation -# We clean up the apt cache after installation to keep the image size down -RUN apt-get update && \ - apt-get install -y \ +# System build deps for common Python wheels +RUN apt-get update && apt-get install -y \ gcc \ python3-dev \ - && rm -rf /var/lib/apt/lists/* + && rm -rf /var/lib/apt/lists/* -# Set the working directory to the user's home directory -# This is where our application code will live -WORKDIR /home/appuser +WORKDIR /app -# Copy all application files into the container -# This includes source code, configuration files, and dependency specifications -# (Excludes files specified in .dockerignore) -COPY . . - -# Change ownership of all app files to the non-privileged user -# This ensures the application can read/write files as needed -RUN chown -R appuser:appuser /home/appuser - -# Switch to the non-privileged user for all subsequent operations -# This improves security by not running as root -USER appuser - -# Create a cache directory for the user -# This is used by UV and Python for caching packages and bytecode -RUN mkdir -p /home/appuser/.cache - -# Install Python dependencies using UV's lock file -# --locked ensures we use exact versions from uv.lock for reproducible builds -# This creates a virtual environment and installs all dependencies -# Ensure your uv.lock file is checked in for consistency across environments +# Dependency install first for better caching +COPY pyproject.toml uv.lock ./ +RUN mkdir -p src RUN uv sync --locked -# Pre-download any ML models or files the agent needs -# This ensures the container is ready to run immediately without downloading -# dependencies at runtime, which improves startup time and reliability -RUN uv run "$PROGRAM_MAIN" download-files +# Copy application code +COPY . . +RUN chown -R appuser:appuser /app +USER appuser -# Expose the healthcheck port -# This allows Docker and orchestration systems to check if the container is healthy -EXPOSE 8081 +# Pre-download models/assets at build time +RUN uv run src/agent.py download-files -# Run the application using UV -# UV will activate the virtual environment and run the agent -# The "start" command tells the worker to connect to LiveKit and begin waiting for jobs -CMD ["uv", "run", "$PROGRAM_MAIN", "start"] +# Start the agent +CMD ["uv", "run", "src/agent.py", "start"]