Astral
docs.astral.sh βΊ uv βΊ guides βΊ integration βΊ docker
Using uv in Docker | uv
1 week ago - See a complete example in the uv-docker-example project. Compiling Python source files to bytecode is typically desirable for production images as it tends to improve startup time (at the cost of increased installation time and image size). To enable bytecode compilation, use the --compile-bytecode flag: ... Alternatively, you can set the UV_COMPILE_BYTECODE environment variable to ensure that all commands within the Dockerfile ...
Reddit
reddit.com βΊ r/python βΊ best practices for using python & uv inside docker
r/Python on Reddit: Best practices for using Python & uv inside Docker
October 11, 2025 -
Getting uv right inside Docker is a bit tricky and even their official recommendations are not optimal.
It is better to use a two-step build process to eliminate uv from the final image size.
A two-step build process not only saves disk space but also reduces attack surface against security vulerabilities
Top answer 1 of 5
174
Youβre skipping past the first solution they offer, which is the more efficient distroless solution. You can literally just copy the standalone uv binary directly into your image; you donβt need to base your entire image on theirs. COPY --from=ghcr.io/astral-sh/uv:0.9.2 /uv /bin/ This takes ~43MiB, not the 77MiB you cite.
2 of 5
52
The linked security issue is a bad example. If an attacker can use uv in your container, they could also download and run whatever executable they want and do not need to exploit bugs in uv for that. With very few exceptions, CVEs in unused executables in containers are almost never an issue, because if the attacker already has shell access to be able to use them, they won't gain anything from exploiting those bugs.
Adding Python to Docker in 2 seconds using uv's Python command
Hmm this is interesting. I'm wondering if it's worth migrating from poetry to uv for our package management. We also use docker to containerize images for CI/docker compose local stacks. More on reddit.com
Announcing uv: Python packaging in Rust
no fucking way now do a drop-in replacement for mypy as well, and my entire python toolkit will be handled by the same party pkg mgmt + lint + format + type checks More on reddit.com
How do I manage a monorepo with multiple subprograms, each of which needs its own docker image?
At first I tried the uv package manager which has a workspace feature, but it is not possible to have separate docker images (as far as I've understand it) because you have one single lock file. There's nothing about having a single lockfile that prevents you from having mulitple dockerfiles. let's pretend we have this: monorepo βββ subproject1 β βββ file1.py β βββ file2.py β βββ Dockerfile β βββ pyproject.toml βββ subproject2 β βββ file1.py β βββ file2.py β βββ Dockerfile β βββ pyproject.toml βββ pyproject.toml βββ uv.lock You can still build each dockerfile with the uv.lock file, you just need to execute the dockerfiles from the context of monorepo eg docker build -t exampletag -f ./subproject<1|2>/Dockerfile .. now the dockerfile will have access to the monorepo's uv.lock file as well as all the contents of every subfolder. You will need to path everything in the dockerfile as if it was in the parent folder though. That's the only downside to this. Alternatively you can copy the uv.lock into the subfolders as part of some sort of pipeline job. Another option is to use build contexts . though depending on the docker pipeline this may add additional unwanted complexity if you don't wanna do multi-stage pipelines There may be some nuances I'm missing since I don't use uv or work on a monorepo with python but that should be the gist of it More on reddit.com
uv with django and docker?
They have a guide on how to use uv with docker: Docker | uv (astral.sh) You can do a single build or multi-build. I use a multi-build and you don't have to actually install uv into docker like you do with the single build. Their demo is for fastapi but it's the same concept. More on reddit.com
Videos
17:14
Easy Python Package Management with UV Inside Docker | A FastAPI ...
18:47
This Is How You Write an Efficient Python Dockerfile - YouTube
11:03
uv - Docker setup with a FastAPI application! | Using uv in ...
17:13
Switching pip to uv in a Dockerized Flask / Django App - YouTube
24:13
My 2025 uv-based Python Project Layout for Production Apps - YouTube
GitHub
github.com βΊ astral-sh βΊ uv-docker-example
GitHub - astral-sh/uv-docker-example: An example of using uv in Docker images Β· GitHub
The run.sh script includes an example of invoking docker run for local development, mounting the source code for the project into the container so that edits are reflected immediately. The compose.yml file includes a Docker compose definition for the web application. It includes a watch directive for Docker compose, which is a best-practice method for updating the container on local changes. The Python application code for the project is at src/uv_docker_example/__init__.py β there's a command line entrypoint and a basic FastAPI application β both of which just display "hello world" output.
Starred by 762 users
Forked by 74 users
Languages Β Dockerfile 81.4% | Shell 16.2% | Python 2.4%
GitHub
github.com βΊ astral-sh βΊ uv-docker-example βΊ blob βΊ main βΊ Dockerfile
uv-docker-example/Dockerfile at main Β· astral-sh/uv-docker-example
# Use a Python image with uv pre-installed Β· FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim Β· Β· # Setup a non-root user Β· RUN groupadd --system --gid 999 nonroot \ && useradd --system --gid 999 --uid 999 --create-home nonroot Β· Β· # Install the project into `/app` WORKDIR /app Β·
Author Β astral-sh
Medium
medium.com βΊ @shaliamekh βΊ python-package-management-with-uv-for-dockerized-environments-f3d727795044
Python package management with uv for dockerized environments | by Raman Shaliamekh | Medium
December 3, 2024 - # docker/Dockerfile FROM python:3.13.0-slim ENV USER=uv-example-user \ PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ UV_PROJECT_ENVIRONMENT=/usr/local RUN apt-get update && apt-get install --no-install-recommends -y \ curl \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* \ && useradd -m -s /bin/bash $USER COPY --from=ghcr.io/astral-sh/uv:0.5.5 /uv /uvx /bin/ ENV APP_DIR=/home/$USER/src WORKDIR $APP_DIR RUN --mount=type=cache,target=/home/$USER/.cache/uv \ --mount=type=bind,source=src/uv.lock,target=uv.lock \ --mount=type=bind,source=src/pyproject.toml,target=pyproject.toml \ uv sync --frozen --no-install-project COPY src $APP_DIR RUN --mount=type=cache,target=/home/$USER/.cache/uv \ uv sync --frozen ENV PYTHONPATH=$APP_DIR RUN chown -R "$USER":"$USER" $APP_DIR USER $USER CMD ["uvicorn", "main:app", "--host", "0.0.0.0"]
Hynek
hynek.me βΊ articles βΊ docker-uv
Production-ready Python Docker Containers with uv
September 24, 2024 - Anyways, if your application isnβt packaged for whatever reason: I have added inline notes how to adjust the Dockerfile. In a nutshell, instead of installing your application in the build step, you COPY it into the runtime container after COPYing /app over. Be careful to not overwrite /app. P.S. If you need more help with Docker, my friend Itamar Turner-Trauring has great resources for you. 2024-11-12: Added a workaround for #9046: Use absolute paths for UV_PYTHON on uv 0.5.0+.
Josh Kasuboski
joshkasuboski.com βΊ posts βΊ distroless-python-uv
Building a Python Docker Image with Distroless and Uv | Josh Kasuboski
March 8, 2025 - /app RUN --mount=type=cache,target=/root/.cache/uv \ uv sync --frozen --no-dev --no-editable # Then, use a final image without uv FROM gcr.io/distroless/cc # Copy the Python version COPY --from=builder --chown=python:python /python /python WORKDIR /app # Copy the application from the builder COPY --from=builder --chown=app:app /app/.venv /app/.venv # Place executables in the environment at the front of the path ENV PATH="/app/.venv/bin:$PATH" # Run the FastAPI application by default CMD ["fastapi", "run", "--host", "0.0.0.0", "/app/.venv/lib/python3.12/site-packages/uv_docker_example"]
Digon
digon.io βΊ en βΊ blog βΊ 2025_07_28_python_docker_images_with_uv
Build Multistage Python Docker Images Using UV
July 27, 2025 - In this post, we walked through the process of building a minimal, reproducible and production-ready Docker image for a FastAPI application using uv and a multistage Dockerfile. We covered how to structure your Python project for efficient builds, how to manage dependencies with uv sync, and how to isolate the runtime environment for security and performance.
Microsoft Developer Blogs
devblogs.microsoft.com βΊ dev blogs βΊ ise developer blog βΊ dockerizing uv
Dockerizing UV - ISE Developer Blog
June 12, 2025 - We start with a specialized Python image (mcr.microsoft.com/cbl-mariner/base/python:3) that comes with UV pre-installed. Key environment variables (such as UV_COMPILE_BYTECODE and UV_LINK_MODE) are set to speed up dependency installations by enabling bytecode compilation and optimizing linking behavior. The working directory is set to /usr/app, and all project files are copied into this directory. A helper script (scripts/setup/Dockerfile/base.ba.sh) then installs the necessary build tools, ensuring that any dependencies requiring compilation are handled correctly.
DEV Community
dev.to βΊ kummerer94 βΊ multi-stage-docker-builds-for-pyton-projects-using-uv-223g
Multi-Stage Docker Builds for Python Projects using uv - DEV Community
November 11, 2024 - A more general example for a Dockerfile ... be this: # syntax = docker/dockerfile:1.0-experimental # Base image FROM python:3.12 as build RUN apt-get update && apt-get install -y build-essential curl ENV VIRTUAL_ENV=/opt/venv \ PATH="/opt/venv/bin:$PATH" ADD https://astral.sh/uv/......
Docker Hub
hub.docker.com βΊ r βΊ astral βΊ uv
astral/uv - Docker Image
The official Docker image for uv, the Python package manager.
Deanlofts
blog.deanlofts.xyz βΊ guides βΊ uv-python-docker
Mastering UV with Python and Docker: A Comprehensive Guide to Modern Python Development - Dean "Loftwah" Lofts
Hereβs a complete example using FastAPI with custom UV integration. ... # syntax=docker/dockerfile:1.4 # Build stage FROM python:3.13-slim-bookworm AS builder # Install UV COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/ ENV UV_SYSTEM_PYTHON=1 WORKDIR /build # Copy dependency files COPY pyproject.toml uv.lock ./ # Install dependencies with caching RUN --mount=type=cache,target=/root/.cache/uv \ uv pip install --system --compile-bytecode \ --no-editable --only-binary :all: \ -r pyproject.toml # Final stage FROM python:3.13-slim-bookworm WORKDIR /app # Copy installed packages COPY --from=builder /usr/local/lib/python3.13/site-packages /usr/local/lib/python3.13/site-packages # Copy application code COPY .
Medium
medium.com βΊ @benitomartin βΊ deep-dive-into-uv-dockerfiles-by-astral-image-size-performance-best-practices-5790974b9579
Deep Dive into uv Dockerfiles by Astral: Image Size, Performance & Best Practices | by Benito Martin | Medium
March 18, 2025 - FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS builder ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy # Disable Python downloads, because we want to use the system interpreter # across both images. If using a managed Python version, it needs to be # copied from the build image into the final image; see `standalone.Dockerfile` # for an example.
YouTube
youtube.com βΊ watch
How To Use uv in Production - Simple Docker Setup - YouTube
Today we learn how to use uv in production and how to deploy it with Docker.βΎβΎβΎβΎβΎβΎβΎβΎβΎβΎβΎβΎβΎβΎβΎβΎβΎπ Programming Books & Merch ππ The Python Bible Book: https:/...
Published Β June 6, 2025
r y x, r
ryxcommar.com βΊ 2024 βΊ 02 βΊ 15 βΊ how-to-cut-your-python-docker-builds-in-half-with-uv
How to cut your Python Docker build times in half with uv β r y x, r
March 4, 2024 - Long story short, the Python executable for the Python base image is located at /usr/local/bin/python. uv is insistent on users utilizing venvs, but you can bypass this by just defining your βvenvβ to be where the global Python installation is inside the Docker image. uv recognizes the VIRTUAL_ENV env var internally. With uv version 0.1.12, this approach is no longer necessary due to the --system flag. This blog post is mostly a relic of the literal first few hours after uv came out. This post was originally written to provide an optimal Docker installation for uv 0.1.0, since it was not clear in 0.1.0 how to best install and use uv in a Dockerfile, and I was so excited for the project that I wanted to make sure everyone had access to it for their builds.
Reddit
reddit.com βΊ r/python βΊ adding python to docker in 2 seconds using uv's python command
r/Python on Reddit: Adding Python to Docker in 2 seconds using uv's Python command
September 7, 2024 -
Had great success speeding up our Docker workflow over at Talk Python using the brand new features of uv for managing Python and virtual environments. Wrote it up if you're interested:
https://mkennedy.codes/posts/python-docker-images-using-uv-s-new-python-features/
Top answer 1 of 5
47
Hmm this is interesting. I'm wondering if it's worth migrating from poetry to uv for our package management. We also use docker to containerize images for CI/docker compose local stacks.
2 of 5
14
Are you building a fresh image every time? Why not bake python into a common base image that you then FROM ... with? Push that into your registry or artifact repository and avoid repeating work.
Hotovo
hotovo.com βΊ blog βΊ python-dockerfile-with-uv
Python Dockerfile with UV
August 19, 2025 - >FROM python:3.12-slim-bookworm AS builder >RUN apt-get update && apt-get install -y --no-install-recommends curl ca-certificates >ADD https://astral.sh/uv/0.5.31/install.sh /uv-installer.sh >RUN sh /uv-installer.sh && rm /uv-installer.sh >ENV PATH="/root/.local/bin/:$PATH" >ENV UV_COMPILE_BYTECODE=1 >ENV UV_LINK_MODE=copy >WORKDIR /app >COPY pyproject.toml uv.lock ./ >RUN uv sync --frozen --no-install-project ># Run arbitrary Python with UV >RUN uv run python -c "import logging; logging.basicConfig(level=logging.INFO); logging.info('Hello, world')" >FROM python:3.12-slim-bookworm AS runtime >WORKDIR /app ># Copy the environment that UV created in the builder stage >COPY --from=builder /app/.venv /app/.venv >ENV PATH="/app/.venv/bin:$PATH" >ENV PYTHONPATH="/app/src:${PYTHONPATH}" >