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