惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
D
Docker
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
Y
Y Combinator Blog
Vercel News
Vercel News
F
Fortinet All Blogs
B
Blog
Recent Announcements
Recent Announcements
A
About on SuperTechFans
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - Franky
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
B
Blog RSS Feed
C
Check Point Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Visual Studio Blog
月光博客
月光博客

ashishb.net

A day in Luxembourg - the richest country in the world I was asked to install malware during a fake interview Book summary: Breakneck - China's quest to engineer the future by Dan Wang Book summary: How to Teach Your Baby to Read Book Summary: The Discontented Little Baby Book by Pamela Douglas Introducing Amazing Sandbox - run third-party tools and AI agents securely on your machine Why software outsourcing gets a bad reputation? Book summary: The Natural Baby Sleep Solution by Polly Moore A day in Antwerp, Belgium Journey of online influencers Two days in Brussels, Belgium Shortcuts - when we love them and when we don't A visit to Rakhigarhi Three days in overhyped Paris Empty Japan, crowded Tokyo The real lock-in in GitHub is not the code, but the stars 11-day Norwegian Breakaway East Caribbean cruise Sanskrit and Sri Lankan Air Force Use REST with Open API The Achilles heel of American capitalism Costa Rica in 4 days At a juice stall in Sri Lanka A short stay at Warsaw, Poland Two days in Vilnius, Lithuania How IntelliJ IDEs waste disk space Pregnancy Why there aren't many digital nomads from India Two days in Riga, Latvia To keep your machine secure, run third-party tools inside Docker Family Ties in Your DNA: Some relatives are closer than others
Best practices for using Python & uv inside Docker
Ashish Bhatia · 2025-10-11 · via ashishb.net
RedditFeatured in Pycoder’s WeeklyAwesome Python Weekly NewsletterPythonHub

I have been watching uv, the open-source Package manager for Python, for a while.

Earlier this year, I decided that it would be my preferred Python package manager going forward.

I migrated my private as well as public codebases to uv and have since recommended it in my relatively popular article on running Python in production.

Getting uv right inside Docker is a bit tricky and even their official recommendations are not optimal.

Similar to Poetry, I recommend using a two-step build process to eliminate uv from the final image size.

Consider a simple Flask-based web server as an example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Create a sample package
$ uv init --name=src
$ uv add flask && uv sync
$ touch README.md
$ mkdir src

# Create a file src/server.py in your favorite editor
$ cat src/server.py
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
  return "<p>Hello, World!</p>"

if __name__ == "__main__":
  app.run()

Let’s finish the build process Now, let’s add a simple Dockerfile Dockerfile1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
FROM ghcr.io/astral-sh/uv:trixie-slim AS base

WORKDIR /app
# Only copy uv.lock and not pyproject.toml
# This ensures hermiticity of the build
# And prevents Docker image invalidation in case of non-dependency changes
# are made to pyproject.toml
COPY uv.lock /app
# Install dependencies
RUN uv init --name src && uv sync --no-dev --frozen
COPY src /app/src

ENTRYPOINT ["uv", "run", "python", "src/server.py"]

And let’s build and check its size

1
2
3
$ docker build -f Dockerfile1 -t example1 . && \
  docker image inspect example1 --format='{{.Size}}' | numfmt --to=iec-i
210Mi

We don’t need uv in the final build, so we can save space via multi-stage Docker builds.

Consider following the multi-stage Docker file Dockerfile2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
FROM ghcr.io/astral-sh/uv:trixie-slim AS builder

WORKDIR /app
# Only copy uv.lock and not pyproject.toml
# This ensures hermiticity of the build
# And prevents docker image invalidation in case non-dependency changes
# are made to pyproject.toml
COPY uv.lock /app
# Install dependencies
# virtual env is created in "/app/.venv" directory
RUN uv init --name src && uv sync --no-dev --frozen

FROM python:3.13-slim AS runner
COPY src /app/src
COPY --from=builder /app/.venv /app/.venv
ENV PATH="/app/.venv/bin:$PATH"
ENV PYTHONPATH=/app/.venv/lib/python3.13/site-packages

WORKDIR /app
ENTRYPOINT ["python", "src/server.py"]

And the result

1
2
3
$ docker build -f Dockerfile2 -t example1 . && \
  docker image inspect example1 --format='{{.Size}}' | numfmt --to=iec-i
143Mi

That’s an extra 77Mi (37%) of savings while reducing the attack surface of the Docker image by eliminating uv from the final image.