|
| 1 | +# This is an example Dockerfile that builds a minimal container for running LK Agents |
| 2 | +# For more information on the build process, see https://docs.livekit.io/agents/ops/deployment/builds/ |
| 3 | +# syntax=docker/dockerfile:1 |
| 4 | + |
| 5 | +# Use the official UV Python base image with Python 3.13 on Debian Bookworm |
| 6 | +# UV is a fast Python package manager that provides better performance than pip |
| 7 | +# We use the slim variant to keep the image size smaller while still having essential tools |
| 8 | +ARG PYTHON_VERSION=3.13 |
| 9 | +FROM ghcr.io/astral-sh/uv:python${PYTHON_VERSION}-bookworm-slim AS base |
| 10 | + |
| 11 | +# Keeps Python from buffering stdout and stderr to avoid situations where |
| 12 | +# the application crashes without emitting any logs due to buffering. |
| 13 | +ENV PYTHONUNBUFFERED=1 |
| 14 | + |
| 15 | +# Create a non-privileged user that the app will run under. |
| 16 | +# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#user |
| 17 | +ARG UID=10001 |
| 18 | +RUN adduser \ |
| 19 | + --disabled-password \ |
| 20 | + --gecos "" \ |
| 21 | + --home "/app" \ |
| 22 | + --shell "/sbin/nologin" \ |
| 23 | + --uid "${UID}" \ |
| 24 | + appuser |
| 25 | + |
| 26 | +# Install build dependencies required for Python packages with native extensions |
| 27 | +# gcc: C compiler needed for building Python packages with C extensions |
| 28 | +# g++: C++ compiler needed for building Python packages with C++ extensions |
| 29 | +# python3-dev: Python development headers needed for compilation |
| 30 | +# We clean up the apt cache after installation to keep the image size down |
| 31 | +RUN apt-get update && apt-get install -y \ |
| 32 | + gcc \ |
| 33 | + g++ \ |
| 34 | + python3-dev \ |
| 35 | + && rm -rf /var/lib/apt/lists/* |
| 36 | + |
| 37 | +# Create a new directory for our application code |
| 38 | +# And set it as the working directory |
| 39 | +WORKDIR /app |
| 40 | + |
| 41 | +# Copy just the dependency files first, for more efficient layer caching |
| 42 | +COPY pyproject.toml uv.lock ./ |
| 43 | +RUN mkdir -p src |
| 44 | + |
| 45 | +# Install Python dependencies using UV's lock file |
| 46 | +# --locked ensures we use exact versions from uv.lock for reproducible builds |
| 47 | +# This creates a virtual environment and installs all dependencies |
| 48 | +# Ensure your uv.lock file is checked in for consistency across environments |
| 49 | +RUN uv sync --locked |
| 50 | + |
| 51 | +# Copy all remaining pplication files into the container |
| 52 | +# This includes source code, configuration files, and dependency specifications |
| 53 | +# (Excludes files specified in .dockerignore) |
| 54 | +COPY . . |
| 55 | + |
| 56 | +# Change ownership of all app files to the non-privileged user |
| 57 | +# This ensures the application can read/write files as needed |
| 58 | +RUN chown -R appuser:appuser /app |
| 59 | + |
| 60 | +# Switch to the non-privileged user for all subsequent operations |
| 61 | +# This improves security by not running as root |
| 62 | +USER appuser |
| 63 | + |
| 64 | +# Pre-download any ML models or files the agent needs |
| 65 | +# This ensures the container is ready to run immediately without downloading |
| 66 | +# dependencies at runtime, which improves startup time and reliability |
| 67 | +RUN uv run "agent.py" download-files |
| 68 | + |
| 69 | +# Run the application using UV |
| 70 | +# UV will activate the virtual environment and run the agent. |
| 71 | +# The "start" command tells the worker to connect to LiveKit and begin waiting for jobs. |
| 72 | +CMD ["uv", "run", "agent.py", "start"] |
0 commit comments