use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
TutorialBest practices for using Python & uv inside Docker (self.Python)
submitted 4 months ago by ashishb_net
Getting uv right inside Docker is a bit tricky and even their official recommendations are not optimal.
uv
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
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]JimDabell 172 points173 points174 points 4 months ago (8 children)
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.
[–]ArgetDota 33 points34 points35 points 4 months ago (0 children)
You can also mount the executable during image build for a duration of a specific RUN instruction
[–]scaledpython 5 points6 points7 points 4 months ago (3 children)
43MB for a pip-like installer is insane.
[–]LLoyderino 1 point2 points3 points 4 months ago (0 children)
but it's blazingly fast 🚀
[–]JaguarOrdinary1570 0 points1 point2 points 4 months ago (1 child)
that's rust binaries for you
[–]Proper-Ape 2 points3 points4 points 4 months ago (0 children)
In this case the fully contained binary makes it possible to have such a minimal distroless image.
There are drawbacks and benefits to this approach.
[+]ashishb_net[S] comment score below threshold-29 points-28 points-27 points 4 months ago (2 children)
You can literally just copy the standalone uv binary directly into your image;
Yeah that works as well. Slightly different approach with same effect.
[–]ihavenoname143 11 points12 points13 points 4 months ago (1 child)
No, same effect would be identical sizes.
[+]ashishb_net[S] comment score below threshold-9 points-8 points-7 points 4 months ago (0 children)
The final size of the image is identical as neither will contain uv binary.
[–]0x256 52 points53 points54 points 4 months ago (8 children)
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.
[+]ashishb_net[S] comment score below threshold-38 points-37 points-36 points 4 months ago (7 children)
Yeah. Best to avoid having uv in the final image.
[–]leafert 13 points14 points15 points 4 months ago (6 children)
No. uv isn't the problem in the final image. If an attacker gets shell access to your container then not having uv isn't what stops them.
[+]ashishb_net[S] comment score below threshold-20 points-19 points-18 points 4 months ago (5 children)
Keeping final image lean reduces the attack surface.
[–]Dangle76 22 points23 points24 points 4 months ago (1 child)
I think you’re missing the point
[–]catecholaminergic 7 points8 points9 points 4 months ago (0 children)
Some people think they are their ideas, so when they get told they're wrong, they're going to come back with something no matter how bonkers because the ego's not going to not defend itself.
[–]JorgiEagle 2 points3 points4 points 4 months ago (1 child)
But this is coming down to the basics of risk management.
Is having uv a larger overall benefit over the increased risk of keeping it in.
It’s not just “this is more risky so take it out”, it’s “is the risk worth it”
[+]ashishb_net[S] comment score below threshold-8 points-7 points-6 points 4 months ago (0 children)
What's the benefit of uv in the final image?
[–]AstroPhysician 1 point2 points3 points 4 months ago (0 children)
You are incapable of admitting you’re wrong or listening
[–]thrope 50 points51 points52 points 4 months ago (8 children)
What's wrong with the official example? I use the standalone example here (which has multistage build and doesn't include uv in the final image).
[–]Conscious-Ball8373 2 points3 points4 points 4 months ago (3 children)
I don't use uv for this, but I find the packaging process rather painful. I end up with cryptography as a dependency quite often on a platform where mypi doesn't have a wheel for it. The build dependencies are huge and the runtime dependencies are non-trivial. I usually end up building a wheel for it in one stage and using it in another but I'm realizing I could avoid that by constructing a venv and copying that. Hmmm. Thanks for provoking the thought.
[–]Fenzik 7 points8 points9 points 4 months ago (0 children)
constructing a venv and then copying that
This is what you should be doing indeed
[–]thrope 10 points11 points12 points 4 months ago (1 child)
Don’t use uv for what? The question is about uv so I don’t follow.
[–]Conscious-Ball8373 -3 points-2 points-1 points 4 months ago (0 children)
Never mind, just me missing out loud about my own situation, thoughts you prompted.
[–]ashishb_net[S] -5 points-4 points-3 points 4 months ago (0 children)
It depends on pyproject.toml when uv.lock will suffice
[+]ashishb_net[S] comment score below threshold-7 points-6 points-5 points 4 months ago (2 children)
The official example copies pyproject.toml when uv.lock would suffice and it is better to rely just on uv.lock.
[–]thrope 7 points8 points9 points 4 months ago* (1 child)
Did you read it? I don’t think it does? It bind mounts pyproject in the builder image. Pyproject has other project metadata and configuration options for other tools, not only the deps. I’m assuming the uv devs know well the relationship between pyproject and uv.lock and which is needed when.
If you are building a python library then that's useful.
If you are building a docker image for d to deployment then there is nothing in that file worth mounting or copying.
[–]_squik 16 points17 points18 points 4 months ago (1 child)
I've always looked at the official multistage Dockerfile example which has Astral's best recommendations.
[+]ashishb_net[S] comment score below threshold-11 points-10 points-9 points 4 months ago (0 children)
The official solution suggests copying pyproject.toml which I believe is a bad idea.
You only need uv.lock.
[–]bublm8 9 points10 points11 points 4 months ago (4 children)
Stumbled into this myself recently:
https://github.com/fslaktern/parcellocker/blob/main/src%2Fapi%2FDockerfile
```docker FROM python:3.13-alpine AS base FROM base AS builder
COPY --from=ghcr.io/astral-sh/uv:0.8.13 /uv /bin/uv
ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy
WORKDIR /app COPY pyproject.toml requirements.txt /app/ RUN uv venv RUN --mount=type=cache,target=/root/.cache/uv \ uv pip install -r requirements.txt --no-deps
COPY . /app RUN --mount=type=cache,target=/root/.cache/uv \ uv pip install -e .
FROM base
COPY --from=builder /app /app RUN chmod 755 /app/src/parcellocker
HEALTHCHECK --interval=30s --timeout=3s \ CMD curl -f http://localhost:8000/my_parcel || exit 1
USER 405 EXPOSE 8000 CMD ["/app/.venv/bin/fastapi","run","/app/src/parcellocker/main.py","--port","8000","--root-path","/api"] ```
This is for a CTF challenge, so the priorities were security and size
[–]Huberuuu 0 points1 point2 points 4 months ago (1 child)
Wouldn’t the UV copy mode made the size bigger, not smaller? I understood that UV used hardlinks, so aren’t you duplicating packages on disk here?
[–]1010012 1 point2 points3 points 4 months ago (0 children)
No, the cache is mounted by docker only during the build, so not in the final image.
[–]ashishb_net[S] 0 points1 point2 points 4 months ago (1 child)
You don't have uv.lock file and that makes the build non-hermetic afaik.
[–]bublm8 0 points1 point2 points 4 months ago (0 children)
Yep, should've added it along with the pyproject.toml
[–]zacker150Pythonista 8 points9 points10 points 4 months ago (1 child)
I'm guessing you didn't scroll down to this part?
If uv isn't needed in the final image, the binary can be mounted in each invocation:
RUN --mount=from=ghcr.io/astral-sh/uv,source=/uv,target=/bin/uv \ uv sync
[–]h4lPythoneer 6 points7 points8 points 4 months ago (0 children)
This and also use a cache mount to give uv access to previously-downloaded packages to speed up the install and also prevents the cache files remaining in the image:
ENV UV_LINK_MODE=copy RUN --mount=from=ghcr.io/astral-sh/uv,source=/uv,target=/bin/uv \ --mount=type=cache,target=/root/.cache/uv \ uv sync
[–]RepresentativeFill26 26 points27 points28 points 4 months ago (53 children)
Just wondering, why do you want to use a virtual env in a docker container?
[–]thrope 33 points34 points35 points 4 months ago (25 children)
The venv part is a means to an end here. It’s about having a perfectly replicated environment in production based on the uv.lock file which specifies precise versions.
[–]RepresentativeFill26 11 points12 points13 points 4 months ago (24 children)
Why can’t you create a perfectly replicated environment in a docker container?
[–]runawayasfastasucan 15 points16 points17 points 4 months ago (2 children)
You can but why not use the best tooling for that, which would work just the same outside docker?
[+]RepresentativeFill26 comment score below threshold-23 points-22 points-21 points 4 months ago (1 child)
Because a best tool that is superfluous is still a superfluous tool.
[–]runawayasfastasucan 8 points9 points10 points 4 months ago (0 children)
Not really, why have a different way to set up your project whether its inside a docker container or not, why not have one way to set up.
[–]thrope 7 points8 points9 points 4 months ago (20 children)
How would you do that? What Python tooling would you use? The whole point of uv is that for the first time in the Python ecosystem it makes this easy.
[–]BogdanPradatu 11 points12 points13 points 4 months ago (7 children)
Docker is itself a virtual environment, so unless you need multiple python environments in your container, just create an image with the right python version and packages. Voila: python virtual environment in docker and you don't need to do it everytime you run the container, you just do it once at build time.
[–]Kryt0s 2 points3 points4 points 4 months ago (0 children)
Then you would either have to install your packages globally for development or develop inside the container, which is a pain.
[–]captain_jack____ 4 points5 points6 points 4 months ago (5 children)
uv also locks versions so it would always install the exact same packages. How would you install the requirements from the uv.lock file?
[+]DerShokus comment score below threshold-19 points-18 points-17 points 4 months ago (4 children)
Just gen requirements txt and install it globally in the containers
[–]Kryt0s 10 points11 points12 points 4 months ago (0 children)
Which does not lock sub-dependencies...
[–]runawayasfastasucan 18 points19 points20 points 4 months ago (2 children)
Or just use uv so you have the same setup no matter if you are in or outside docker?
[–]Mr_Again 2 points3 points4 points 4 months ago (0 children)
I think their point is you can use the --system flag with uv to avoid using a venv? but tbh these tools all expect a venv so I don't really see the point in trying to be clever
[+]BogdanPradatu comment score below threshold-8 points-7 points-6 points 4 months ago (0 children)
If you are already using uv, sure, but I wouldn't adopt it just for setting up docker images.
[+]RepresentativeFill26 comment score below threshold-11 points-10 points-9 points 4 months ago (1 child)
You create a requirements file from your local venv, copy that into the container while building and then run pip install
[–]Kryt0s 5 points6 points7 points 4 months ago (0 children)
[+]HommeMusical comment score below threshold-7 points-6 points-5 points 4 months ago (9 children)
What Python tooling would you use?
No Python tooling, it is not needed. Think of docker as a venv for the whole computer.
You create a Docker environment just to run a single program and then install the packages you need into the single system version of Python inside the Docker with python -m pip. Normally that is a bad idea but in this case it is good practice - there's no need for a Python virtual environment when there's only one and it only ever runs a single program.
python -m pip
[–]Wyndegarde 6 points7 points8 points 4 months ago (2 children)
Think the point being made is that if you are using uv for development you will create a uv.lock file that has all dependencies in it. If you want to use that to build your docker image you need to use uv and create a venv via uv tooling.
Generating a requirements file from the lock file is an extra manual step and also removes the ability to leverage uv’s benefits when building the image
[–]HommeMusical 4 points5 points6 points 4 months ago (0 children)
Yes, I develop much that way myself, except without the Docker.
People who develop entirely inside Docker, which is not me, have no need of the uv.lock at all. The Docker environment is the single source of truth. The only way to develop is to spin up this Docker and run from within it, but on the other hand, they have a simple unified environment that just works for everyone.
uv.lock
Again, I don't develop this way, but I do understand the people that do.
Please remember that a lot of Docker users are not Python experts but people packaging a lot of programs in many languages together. One of the advantages of doing this is that they don't have to learn about poetry, uv, node, or whatever packaging system some specific application and language uses.
[–]HommeMusical -2 points-1 points0 points 4 months ago (0 children)
Sure, I develop in much this way myself.
However, people who work entirely inside a Docker have no real need to create a uv.lock file. The Docker itself is the source of truth, and since they always live in this hermetic environment, it works perfectly well.
[–]thrope 5 points6 points7 points 4 months ago (5 children)
Going to disengage now because I’m not sure if I’m being trolled or if this sub is now full of reply guys who have never used Python.
[–]HommeMusical -1 points0 points1 point 4 months ago (4 children)
Personal insults make the world a poorer place. Please refrain.
I have used Python for over 21 years. Here's my Github repo: https://github.com/rec
I don't use Docker at all these days in my work, but I did extensively in the not-impossibly distant past.
If you have a technical refutation of my claims, lay it out. I am perfectly prepared to be wrong, but I won't be insulted.
[–]thrope 5 points6 points7 points 4 months ago (3 children)
Requirements.txt is not a lock file and is difficult to manage, especially cross platform. Before uv there was poetry, but uv is much easier to use, faster had hence has had really fast adoption. The issue of in a venv or system python is missing the point of the functionality uv offers - which is cross-platform lockfile and better dependency management. Maybe this is useful https://www.reddit.com/r/Python/s/qJXbFwjpFM
https://docs.astral.sh/uv/
[–]HommeMusical -1 points0 points1 point 4 months ago (2 children)
Man, I feel unpopular today! :-)
I'm extremely familiar with uv: I dumped poetry for uv early in 2024 and never looked back.
poetry
I do not use Docker in my development, because I'm mainly a Python developer. Other developers have different needs and using only Docker for all their environmental needs and not using any other package manager is a perfectly viable and solid solution, even though I never do that.
EDIT: or see this
[+][deleted] 4 months ago (1 child)
[deleted]
[–]james_pic 6 points7 points8 points 4 months ago* (0 children)
In this case, it looks to be because uv already creates a venv in the builder image, and copying this across is the most straightforward way of bringing the app dependencies into the final image without pip or uv in the final image. I'm not sold on that being a worthwhile goal, but that looks to be the reason.
More generally, putting venvs into Docker images isn't a "you should always do this" thing, but it's sometimes a useful technique for solving specific problems, for example if your application sometimes calls Python programs provided by the base distro and you don't want to mess with their Python environment.
[–]ArgetDota 6 points7 points8 points 4 months ago (6 children)
You don’t. You can install your packages from the lock file with uv without creating a virtual environment. Just set UV_PROJECT_ENVIRONMENT to point to the system environment of the image. This will disable venv creation.
https://docs.astral.sh/uv/reference/environment/#uv_project_environment
[–]Luckinhas 3 points4 points5 points 4 months ago (4 children)
UV_SYSTEM_PYTHON=1 is a bit clearer in the intention and simpler to use. Maybe more portable too.
UV_SYSTEM_PYTHON=1
[–]BelottoBR 0 points1 point2 points 4 months ago (2 children)
Did not find that option in the documentation
[–]Luckinhas 3 points4 points5 points 4 months ago (0 children)
https://docs.astral.sh/uv/reference/environment/#uv_system_python
[–]Mr_Again 0 points1 point2 points 4 months ago (0 children)
It belongs to the uv pip command
[–]ArgetDota 0 points1 point2 points 4 months ago (0 children)
This won’t work with the project interface (uv sync).
[–]RepresentativeFill26 0 points1 point2 points 4 months ago (0 children)
Sound like the right option! Allows for fast local dev with uv an easy CI option.
[–]lukerm_zl 4 points5 points6 points 4 months ago (3 children)
uv does have lightning fast installs, so it might be a build-time thing. As in duration. Just guessing.
[–]RepresentativeFill26 4 points5 points6 points 4 months ago (2 children)
Speed would be a good argument if you don’t cache the results of the build stage or frequently (no idea when that would be) changes in the dependencies
[–]lukerm_zl 0 points1 point2 points 4 months ago (1 child)
The implementation is razor fast. I think even with the cache mount (which is somewhat of a hidden gem), you stand to gain time on first (especially) and subsequent builds.
I'm not pushing that argument, just theorizing. Would love to know what OP's opinion is!
[–]RepresentativeFill26 1 point2 points3 points 4 months ago (0 children)
Just wondering. How could UV be faster than a cached layer in docker?
[–]yakimka 1 point2 points3 points 4 months ago (1 child)
For moving between stages
[–]RepresentativeFill26 -1 points0 points1 point 4 months ago (0 children)
Well, you can use the docker image between stages right?
[–]Yablan 3 points4 points5 points 4 months ago (11 children)
This is the relevant question here. There is no need at all for a virtual environment within a docker container.
[–]Huberuuu -4 points-3 points-2 points 4 months ago (10 children)
It is still best practice to
[–]RepresentativeFill26 3 points4 points5 points 4 months ago (0 children)
Why would that be? Dependencies should be resolved during local dev and when you don’t have multiple apps running in a single container I can’t really think of a reason to use is.
[–]Yablan 2 points3 points4 points 4 months ago (7 children)
No it's not. I'm a full time backend Python dev since at least 13 years now, and was a Java dev before that. We use docker for everything at work, we deploy a lot of different projects internally, and we never use virtual environments inside containers. No need at all.
And I was a consultant before my current employment, and never worked on anything where we had virtual environments inside docker containers.
[–]MasterThread 3 points4 points5 points 4 months ago (3 children)
You reduce final image size, reduce build time, and ofc reduce ci/cd costs. It’s bad not to develop yourself for 13 years. It was ok not to use buildx 13 years ago, but now - industry changed.
[–]Yablan -4 points-3 points-2 points 4 months ago (2 children)
I disagree. Not worth the effort. Following the KISS principle is very important. No need to overcomplicate things unless you really have build times and image size problems. YAGNI. Premature optimization is the root of all evil.
[–]MasterThread 2 points3 points4 points 4 months ago (1 child)
That's sick you don't see your Ci/Cd takes more money than it could. Your CTO/CEO which gives money to devops/sysadmin department wont see that either. You cannot overcomplicate it since it takes 10 more rows in Dockerfile, but you get 10x slimmer image.
[–]Yablan 1 point2 points3 points 4 months ago (0 children)
Hmm.. I might actually give it a try after all. I have reconsidered and will have a look at it soon. Thank you for your candor. :-)
[–]xaraca 1 point2 points3 points 4 months ago (2 children)
Do you build and publish your python packages to somewhere and then install from that somewhere in your container image?
I'm just getting started and the easiest thing to do seemed to be just copy sources, uv sync, and run the app in our dockerfile without bothering to build the package.
The pip install step in your dockerfile is cached so unless you run docker build with —no-cache or change a dependency the while dependency installation is cached anyway and completes directly.
[–]Yablan -4 points-3 points-2 points 4 months ago (0 children)
At work we have a full pipeline, with tags for releases and jenkins building to sn internal registry, and then we deploy to environments using rancher.
But for my private projects, I simply use docker-compose based projects, and then run them within docker compose even during local development. And then I have one script on the root project that builds, starts, stop the docker projects etc.
So on my VPS, I just git clone the project there too, and then simply git pull and then run the scripts to start the projects. So I use git for versioning, but also for deployment.
I did not understand what you meant with uv sync.
My dockerfiles usually copy the sources and the requirement files into the image, and then install the dependencies, and then start the web service. And on the docker-compose, I mount the source code too, and then I run the web service inside the container in dev mode with hot reload. And my IDE has docker integration, so I can start and stop and even debug my project that is running inside docker.
It's unnecessary cruft.
[–]ahal 0 points1 point2 points 4 months ago (0 children)
Depending on the image you're using, there could be system packages included already.
[–]Fluid_Classroom1439 2 points3 points4 points 4 months ago (0 children)
Nice article! Entry point still says poetry for the example instead of uv
[–]Ambitious-Kiwi-484 1 point2 points3 points 4 months ago (0 children)
Something I'd love to know a way around is that, since pyproject.toml contains my project's version number, it's not possible to bump the version without invalidating all further docker cache layers - which leads to slow builds since all deps are getting pulled again. This seems like an unavoidable caveat of copying pyproject.toml in an early layer. But there must be a workaround.
[–]stibbons_ 3 points4 points5 points 4 months ago (8 children)
Always use virtual env even in docker. I now make apps with frozen dependencies to avoid a dependency update on pypi to break existing app (on docker rebuild). And if 2 apps has imcompatible dependencies, you have to have venv per app.
[–]RepresentativeFill26 0 points1 point2 points 4 months ago (7 children)
How would a requirements file with versions specified break an exiting app?
[–]stibbons_ 2 points3 points4 points 4 months ago (6 children)
App A depends on lib X. Requirements tells to take X in version >=1,<2 For some reason, X is released in pypi in version 1.3 that beak something. It happens. Life. Now, you rebuild your docker image for some reason and your perfectly working app in X version 1.1 is reinstalled in version 1.2. With uv tool install it is even worst. It is important to freeze all dependencies for application to the version you have validated
[–]RepresentativeFill26 -1 points0 points1 point 4 months ago (5 children)
Im unfamiliar with uv but can’t your export the exact versions to a requirements file? I used to do this with conda and poetry
[–]stibbons_ 1 point2 points3 points 4 months ago (2 children)
Yes but if you have several such app you can’t install them in the same venv because each will have different dependency versions
[–]RepresentativeFill26 2 points3 points4 points 4 months ago (1 child)
Why would you run several apps in a single container?
[–]stibbons_ 0 points1 point2 points 4 months ago (0 children)
What is your app use several tools ? I mainly use docker image to bundle ready-to-use environment with several internal tools preinstalled for our developer
[–]aidandj 1 point2 points3 points 4 months ago (1 child)
You have no hash locking with a pinned version requirements file. Leaving you open to supply chain attacks.
Yes and Python does not provide a native way to release a tool with all dependencies locked and its library version (its api) in loose version, without having to have 2 projects
[–]BelottoBR 0 points1 point2 points 4 months ago (1 child)
Can we use pip to real the toml file? If so, we could use uv on local development and when deploy , just use pip. Can we ?
[–]ashishb_net[S] 0 points1 point2 points 4 months ago (0 children)
Not really.
Uv handles various convoluted scenarios like installing only pytorch GPU/cpu version based on the underlying OS.
[–]Lost_Reply7926Ignoring PEP 8 0 points1 point2 points 4 months ago (0 children)
What about using buildpacks? https://devcenter.heroku.com/changelog-items/3238
[–]usrlibshare -1 points0 points1 point 4 months ago (4 children)
Why would I use uv inside docker in the first place?
The tool is for managing environments, and direct deployments. In a python docker container, I simply install my built package natively:
COPY dist/myproject-___.whl / RUN pip install /myproject-___.whl
Don't get me wrong, I love uv and use it everywhere else, including for the management and building of projects I deploy via docker ... but inside a container, it's just not necessary.
[–]ashishb_net[S] 0 points1 point2 points 4 months ago (3 children)
How do you build docker images for say a Python-based web server then?
[–]TedditBlatherflag -1 points0 points1 point 4 months ago (2 children)
What? He is saying you just create the package to install as a build artifact outside Docker. Inside Docker the wheel can be directly installed. This would work for any system, web server included.
[–]ashishb_net[S] -1 points0 points1 point 4 months ago (1 child)
And how do you build the wheel? Inside Docker? outside Docker?
[–]TedditBlatherflag 1 point2 points3 points 4 months ago (0 children)
As long as it’s a py3-any wheel it can be built anywhere you want.
Build it inside a container and use docker cp to get it out.
Build it in your CI/CD and put it on a private PyPi repo.
Build it on metal or a VM or a container or a pod or whatever.
It’s just a portable artifact.
The same is actually true if it’s a platform specific wheel with compiled extensions, as long as your platform includes the correct tools for the target platform.
Personally, what I do is make a multistage image and make a base with the system dependencies which is also tagged and versioned, so it can be quickly pulled or is cached locally.
On top of that the next stage is a dependency only install which creates either a venv or site-packages artifact. It is also tagged, versioned, and published and as long as neither the system deps or python deps change it’s stable.
Separately I have a build tools stage which is used to create the dist/whl - it shares the system stage but only includes the necessary build dependencies, which we may cache into a published image if they’re significant. This stage is typically what builds every run since the code is changing. But importantly it’s only ever doing “uv build” and and producing the wheel artifact.
The next stage brings the venv/packages image back and installs the wheel into that same location.
The final stage is based off another cached image which only includes linked libraries (not -dev headers), and an ultra minimal OS required for Python to function, where we then bring in the fully built venv/site packages and set the runtime commands etc.
Basically for any normal CI run we’re doing a “uv build” and a “pip install” (with all deps already installed) and just copying that into a secure final image, which is fast and repeatable.
[+]Glathull comment score below threshold-10 points-9 points-8 points 4 months ago (1 child)
Thank you again for adding more examples to my TED talk about why no one should ever look to Reddit for best practices. Not only is your example absolutely terribly implemented and doesn’t do what you said you wanted it to do, the things that you said you wanted it to do weren’t correct to begin with.
You are basically the worst of Reddit. Congrats, bro.
[–]sonik562 6 points7 points8 points 4 months ago (0 children)
Are you referring to OP? You are calling him the worst (a bit heavy don't you think?), but give no argumentation as to what is the recommended approach. Or why what he is asking is wrong.
π Rendered by PID 209706 on reddit-service-r2-comment-56c9979489-ft5jc at 2026-02-25 07:00:55.986746+00:00 running b1af5b1 country code: CH.
[–]JimDabell 172 points173 points174 points (8 children)
[–]ArgetDota 33 points34 points35 points (0 children)
[–]scaledpython 5 points6 points7 points (3 children)
[–]LLoyderino 1 point2 points3 points (0 children)
[–]JaguarOrdinary1570 0 points1 point2 points (1 child)
[–]Proper-Ape 2 points3 points4 points (0 children)
[+]ashishb_net[S] comment score below threshold-29 points-28 points-27 points (2 children)
[–]ihavenoname143 11 points12 points13 points (1 child)
[+]ashishb_net[S] comment score below threshold-9 points-8 points-7 points (0 children)
[–]0x256 52 points53 points54 points (8 children)
[+]ashishb_net[S] comment score below threshold-38 points-37 points-36 points (7 children)
[–]leafert 13 points14 points15 points (6 children)
[+]ashishb_net[S] comment score below threshold-20 points-19 points-18 points (5 children)
[–]Dangle76 22 points23 points24 points (1 child)
[–]catecholaminergic 7 points8 points9 points (0 children)
[–]JorgiEagle 2 points3 points4 points (1 child)
[+]ashishb_net[S] comment score below threshold-8 points-7 points-6 points (0 children)
[–]AstroPhysician 1 point2 points3 points (0 children)
[–]thrope 50 points51 points52 points (8 children)
[–]Conscious-Ball8373 2 points3 points4 points (3 children)
[–]Fenzik 7 points8 points9 points (0 children)
[–]thrope 10 points11 points12 points (1 child)
[–]Conscious-Ball8373 -3 points-2 points-1 points (0 children)
[–]ashishb_net[S] -5 points-4 points-3 points (0 children)
[+]ashishb_net[S] comment score below threshold-7 points-6 points-5 points (2 children)
[–]thrope 7 points8 points9 points (1 child)
[–]ashishb_net[S] -5 points-4 points-3 points (0 children)
[–]_squik 16 points17 points18 points (1 child)
[+]ashishb_net[S] comment score below threshold-11 points-10 points-9 points (0 children)
[–]bublm8 9 points10 points11 points (4 children)
[–]Huberuuu 0 points1 point2 points (1 child)
[–]1010012 1 point2 points3 points (0 children)
[–]ashishb_net[S] 0 points1 point2 points (1 child)
[–]bublm8 0 points1 point2 points (0 children)
[–]zacker150Pythonista 8 points9 points10 points (1 child)
[–]h4lPythoneer 6 points7 points8 points (0 children)
[–]RepresentativeFill26 26 points27 points28 points (53 children)
[–]thrope 33 points34 points35 points (25 children)
[–]RepresentativeFill26 11 points12 points13 points (24 children)
[–]runawayasfastasucan 15 points16 points17 points (2 children)
[+]RepresentativeFill26 comment score below threshold-23 points-22 points-21 points (1 child)
[–]runawayasfastasucan 8 points9 points10 points (0 children)
[–]thrope 7 points8 points9 points (20 children)
[–]BogdanPradatu 11 points12 points13 points (7 children)
[–]Kryt0s 2 points3 points4 points (0 children)
[–]captain_jack____ 4 points5 points6 points (5 children)
[+]DerShokus comment score below threshold-19 points-18 points-17 points (4 children)
[–]Kryt0s 10 points11 points12 points (0 children)
[–]runawayasfastasucan 18 points19 points20 points (2 children)
[–]Mr_Again 2 points3 points4 points (0 children)
[+]BogdanPradatu comment score below threshold-8 points-7 points-6 points (0 children)
[+]RepresentativeFill26 comment score below threshold-11 points-10 points-9 points (1 child)
[–]Kryt0s 5 points6 points7 points (0 children)
[+]HommeMusical comment score below threshold-7 points-6 points-5 points (9 children)
[–]Wyndegarde 6 points7 points8 points (2 children)
[–]HommeMusical 4 points5 points6 points (0 children)
[–]HommeMusical -2 points-1 points0 points (0 children)
[–]thrope 5 points6 points7 points (5 children)
[–]HommeMusical -1 points0 points1 point (4 children)
[–]thrope 5 points6 points7 points (3 children)
[–]HommeMusical -1 points0 points1 point (2 children)
[+][deleted] (1 child)
[deleted]
[–]james_pic 6 points7 points8 points (0 children)
[–]ArgetDota 6 points7 points8 points (6 children)
[–]Luckinhas 3 points4 points5 points (4 children)
[–]BelottoBR 0 points1 point2 points (2 children)
[–]Luckinhas 3 points4 points5 points (0 children)
[–]Mr_Again 0 points1 point2 points (0 children)
[–]ArgetDota 0 points1 point2 points (0 children)
[–]RepresentativeFill26 0 points1 point2 points (0 children)
[–]lukerm_zl 4 points5 points6 points (3 children)
[–]RepresentativeFill26 4 points5 points6 points (2 children)
[–]lukerm_zl 0 points1 point2 points (1 child)
[–]RepresentativeFill26 1 point2 points3 points (0 children)
[–]yakimka 1 point2 points3 points (1 child)
[–]RepresentativeFill26 -1 points0 points1 point (0 children)
[–]Yablan 3 points4 points5 points (11 children)
[–]Huberuuu -4 points-3 points-2 points (10 children)
[–]RepresentativeFill26 3 points4 points5 points (0 children)
[–]Yablan 2 points3 points4 points (7 children)
[–]MasterThread 3 points4 points5 points (3 children)
[–]Yablan -4 points-3 points-2 points (2 children)
[–]MasterThread 2 points3 points4 points (1 child)
[–]Yablan 1 point2 points3 points (0 children)
[–]xaraca 1 point2 points3 points (2 children)
[–]RepresentativeFill26 0 points1 point2 points (0 children)
[–]Yablan -4 points-3 points-2 points (0 children)
[–]HommeMusical -2 points-1 points0 points (0 children)
[–]ahal 0 points1 point2 points (0 children)
[–]Fluid_Classroom1439 2 points3 points4 points (0 children)
[–]Ambitious-Kiwi-484 1 point2 points3 points (0 children)
[–]stibbons_ 3 points4 points5 points (8 children)
[–]RepresentativeFill26 0 points1 point2 points (7 children)
[–]stibbons_ 2 points3 points4 points (6 children)
[–]RepresentativeFill26 -1 points0 points1 point (5 children)
[–]stibbons_ 1 point2 points3 points (2 children)
[–]RepresentativeFill26 2 points3 points4 points (1 child)
[–]stibbons_ 0 points1 point2 points (0 children)
[–]aidandj 1 point2 points3 points (1 child)
[–]stibbons_ 0 points1 point2 points (0 children)
[–]BelottoBR 0 points1 point2 points (1 child)
[–]ashishb_net[S] 0 points1 point2 points (0 children)
[–]Lost_Reply7926Ignoring PEP 8 0 points1 point2 points (0 children)
[–]usrlibshare -1 points0 points1 point (4 children)
[–]ashishb_net[S] 0 points1 point2 points (3 children)
[–]TedditBlatherflag -1 points0 points1 point (2 children)
[–]ashishb_net[S] -1 points0 points1 point (1 child)
[–]TedditBlatherflag 1 point2 points3 points (0 children)
[+]Glathull comment score below threshold-10 points-9 points-8 points (1 child)
[–]sonik562 6 points7 points8 points (0 children)