How To Use uv in Production - Simple Docker Setup

NeuralNineAbout 4 min readJun 8, 2025Watch original
THE SUMMARYAI-generated

Key Concepts

  • UV: A rust-based Python package manager, positioned as a fast alternative to pip, poetry, and virtualenv.
  • Docker: A platform for containerization, used to package and deploy applications.
  • Dockerfile: A text file that contains instructions for building a Docker image.
  • uv init: Command to initialize a UV project.
  • uv add: Command to add packages to a UV project.
  • uv run: Command to run a Python application using UV.
  • uv venv: Command to create a virtual environment using UV.
  • uv sync: Command to synchronize the virtual environment with the project's dependencies.
  • uv sync --locked: Installs dependencies based on the uv.lock file, ensuring consistent versions.
  • pyproject.toml: Configuration file for Python projects, used by UV to manage dependencies and project metadata.

Dockerizing a Python Application with UV

Introduction to UV

The video introduces UV as a modern, rust-based Python package manager that aims to replace tools like pip, poetry, and virtualenv due to its speed and comprehensive features. The focus is on integrating UV into a Docker setup for production deployments.

Setting up a Sample Application

  1. Initialization: The presenter starts by initializing a UV project using uv init in a working directory.
  2. Adding Dependencies: Packages like fastapi, yfinance, and uvicorn are added to the project using the uv add command. The presenter notes the speed of this process.
  3. Creating a Simple FastAPI Application: A basic "Hello, World"-like FastAPI application is created, including an endpoint that downloads stock data using the yfinance library.
    • Example: The /download_ticker/{ticker} endpoint downloads stock data for a given ticker symbol and saves it to a CSV file.
  4. Running the Application Locally: The application is run locally using uv run uvicorn main:app, demonstrating its functionality.

Creating a Dockerfile for UV Integration

  1. Base Image: The Dockerfile starts with a Python base image, specifically python:3.13-slim-bookworm.
  2. Installing UV: The video highlights two methods for installing UV within the Docker image:
    • Copying Binaries: The recommended approach involves copying pre-built UV binaries from a specified URL. The presenter emphasizes the importance of pinning a specific UV version for production use, rather than using latest.
      • Example: COPY --from=ghcr.io/astral-sh/uv:0.6.6 / /usr/local/bin/
    • Using the Installer: An alternative method involves using apt-get to install necessary dependencies and then running the UV installer script. The presenter prefers the binary copy method for its simplicity.
  3. Copying Application Code: The application code is copied into the Docker image using the ADD ./app command.
  4. Setting the Working Directory: The working directory inside the container is set to /app.
  5. Installing Dependencies with UV: The uv sync --locked command is used to install the application's dependencies based on the uv.lock file. This ensures that the exact versions of the packages specified in the lock file are installed.
    • Explanation: The uv.lock file is analogous to a requirements.txt file in pip, containing the pinned versions of all dependencies.
  6. Exposing the Port: The EXPOSE 8000 instruction is used to expose port 8000, although the presenter notes that this is primarily for documentation purposes.
  7. Running the Application: The CMD instruction specifies the command to run the application using UV: uv run uvicorn main:app --host 0.0.0.0 --port 8000.

Building and Running the Docker Image

  1. Building the Image: The Docker image is built using the command docker build -t uv-production ..
  2. Running the Container: The container is run using the command docker run -p 8000:8000 --name uv-prod-container uv-production.
  3. Verification: The presenter verifies that the application is running correctly by accessing the /download_ticker/nvda endpoint and checking the contents of the data.csv file inside the container.

Additional Considerations

  • pyproject.toml Configuration: The presenter mentions the importance of properly configuring the pyproject.toml file with the correct name, version, and description, especially when publishing the application as a package.
  • Volume Mapping: The presenter suggests mapping a volume to the container to easily access the generated data.csv file without having to execute a bash shell inside the container.

Conclusion

The video provides a practical guide on integrating UV into a Docker workflow for deploying Python applications. By using UV, developers can leverage its speed and efficiency for managing dependencies and running applications in production environments. The key steps involve installing UV in the Docker image, copying the application code, synchronizing dependencies using uv sync --locked, and running the application using uv run.

AI summaries can miss context or contain errors. Check important details against the original video.

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.