harre.dev

Using Python and UV with Docker

Quick little recipe here that I ended up piecing together last night. I had set up a new Python FastAPI project using uv and needed a container image for the project as well.

Using the regular Python image as a base and then including the uv bits to get access to uvs functionality. Then copying my files and syncing the dependencies.

I then proceeded to use the regular FastAPI commands for the final CMD instruction to run the project when the container is started but no dice, it kept saying that fastapi was not a thing you can run. The key is to also invoke uv in the startup process since uv is managing the environment that has all the dependencies.

Here's the full Dockerfile I ended up with.

FROM python:3.13-slim-trixie
ENV UV_SYSTEM_PYTHON=1

COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/

WORKDIR /app
COPY . /app

RUN uv sync --locked

EXPOSE 8080

CMD ["uv", "run", "fastapi", "run", "main.py", "--port", "8080"]

It was a short search, but still, here for later use and so you don't have to look any further!

python uv docker