Installing uv globally without pip
This is how you can install uv directly (without pip) into a Docker container:
FROM bitnami/deepspeed:0.14.0
# Install uv
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
# Copy your folder's files into the container.
COPY . /app
# go into the container
WORKDIR /app
# activate a virtual environment
RUN uv venv
# install into the virtual environment all dependencies
RUN uv pip install -r requirements.txt
# entry point - fun the shell to inspect your container
CMD ["sh"]
build by
docker build -t fastapi-app .
docker run --platform linux/amd64 -it fastapi-app # I am on a arm64 macos
16:28:00.95 INFO ==>
16:28:00.96 INFO ==> Welcome to the Bitnami deepspeed container
16:28:00.98 INFO ==> Subscribe to project updates by watching https://github.com/bitnami/containers
16:28:00.99 INFO ==> Submit issues and feature requests at https://github.com/bitnami/containers/issues
16:28:01.00 INFO ==> Upgrade to Tanzu Application Catalog for production environments to access custom-configured and pre-packaged software components. Gain enhanced features, including Software Bill of Materials (SBOM), CVE scan result reports, and VEX documents. To learn more, visit https://bitnami.com/enterprise
16:28:01.01 INFO ==>
16:28:01.04 INFO ==> Configuring libnss_wrapper
$ which uv
/usr/bin/uv
$ ls -hals /usr/bin/uv
31M -rwxr-xr-x 1 0 deepspeed 31M Mar 26 00:57 /usr/bin/uv
So you are running uv as deepspeed.
Installing uv with pip
This Dockerfile in which you use pip to install uv first, works, too.
Here - as others pointed out, too - you have to make uv visible by
adding ~/.local/bin to your PATH variable.
Another issue is that the user deepspeed runs into permission problems
when installing the pip packages. Like in the solution given without pip, too.
The trick is to activate first a uv environment by uv venv.
By that, now, all the requirements.txt packages will be installed locally.
And thus, you don't have any permission issues.
FROM bitnami/deepspeed:0.14.0
# Copy the application into the container.
COPY . /app
WORKDIR /app
RUN pip install uv
ENV PATH=/.local/bin:$PATH
RUN uv venv # activates uv virtual environment - avoids permission issues
RUN uv pip install -r requirements.txt # now, installation is flawless
# entry point (to inspect the container)
CMD ["sh"]
Without RUN uv venv, or when trying to install with RUN uv pip install --system -r requirements.txt, you run into permission errors:
2.499 error: failed to remove file `/opt/bitnami/python/lib/python3.10/site-packages/Jinja2-3.1.3.dist-info/INSTALLER`: Permission denied (os error 13)
build by
docker build -t fastapi-app .
docker run --platform linux/amd64 -it fastapi-app # I am on a arm64 macos
This shell however behaves very weirdly.
- It doesn't recognize
uv. Only afterexport PATH=/.local/bin:$PATHit can recognizeuv. - It was not possible to reactivate the
uvvirtual environment into whichfastapiwas installed into.fastapias well asdotenvetc are present in/app/.venv/bin/. However,$ cd /appand$ uv venvjust overwrites/app/.venvand creates a new virtual environment. When starting directly$ python, thenimport fastapididn't work. - Only
$ uv run pythonrun a Python console which runsimport fastapi. So$ uv run pythonis the solution.
Does someone know how to activate the original virtual environment?
/app/.venv/bin/activate didn't work source was not found when doing source /app/.venv/bin/activate.
Even the PYTHONPATH manipulating didn't work.
I recommend the first solution therefore.
Answer from Gwang-Jin Kim on Stack Overflow
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
EDIT: See my solution in the comments!
I've loved uv's local DevEx until now, but I can't seem to find the "recommended"/"best" way to create a docker image that installs all my dependencies system-wide (without a venv) in a Docker image.
I've tried with a 'pyproject.toml' (and associated 'uv.lock'):
[project]
name = "api"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
"fastapi>=0.115.6",
etc...
]
[dependency-groups]
dev = [
"pytest>=8.3.4",
etc...
]And a Dockerfile:
FROM ghcr.io/astral-sh/uv:python3.13-alpine #ENV UV_PROJECT_ENVIRONMENT="/usr/local/bin/" TODO ??? ENV PYTHONUNBUFFERED True WORKDIR /app COPY . . RUN uv sync --locked --python-preference system --no-dev
BUT exec'ing into the image after build, I see no '.venv/' and:
/app # python main.py
Traceback (most recent call last):
File "/app/main.py", line 1, in <module>
from fastapi import FastAPI
ModuleNotFoundError: No module named 'fastapi'-
Should I be using
uv install --system...instead or is that deprecated? -
Should I use UV_PROJECT_ENVIRONMENT?
-
Should I simply run everything within a venv inside containers somehow?
Any help or suggestions is greatly appreciated!!
Installing uv globally without pip
This is how you can install uv directly (without pip) into a Docker container:
FROM bitnami/deepspeed:0.14.0
# Install uv
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
# Copy your folder's files into the container.
COPY . /app
# go into the container
WORKDIR /app
# activate a virtual environment
RUN uv venv
# install into the virtual environment all dependencies
RUN uv pip install -r requirements.txt
# entry point - fun the shell to inspect your container
CMD ["sh"]
build by
docker build -t fastapi-app .
docker run --platform linux/amd64 -it fastapi-app # I am on a arm64 macos
16:28:00.95 INFO ==>
16:28:00.96 INFO ==> Welcome to the Bitnami deepspeed container
16:28:00.98 INFO ==> Subscribe to project updates by watching https://github.com/bitnami/containers
16:28:00.99 INFO ==> Submit issues and feature requests at https://github.com/bitnami/containers/issues
16:28:01.00 INFO ==> Upgrade to Tanzu Application Catalog for production environments to access custom-configured and pre-packaged software components. Gain enhanced features, including Software Bill of Materials (SBOM), CVE scan result reports, and VEX documents. To learn more, visit https://bitnami.com/enterprise
16:28:01.01 INFO ==>
16:28:01.04 INFO ==> Configuring libnss_wrapper
$ which uv
/usr/bin/uv
$ ls -hals /usr/bin/uv
31M -rwxr-xr-x 1 0 deepspeed 31M Mar 26 00:57 /usr/bin/uv
So you are running uv as deepspeed.
Installing uv with pip
This Dockerfile in which you use pip to install uv first, works, too.
Here - as others pointed out, too - you have to make uv visible by
adding ~/.local/bin to your PATH variable.
Another issue is that the user deepspeed runs into permission problems
when installing the pip packages. Like in the solution given without pip, too.
The trick is to activate first a uv environment by uv venv.
By that, now, all the requirements.txt packages will be installed locally.
And thus, you don't have any permission issues.
FROM bitnami/deepspeed:0.14.0
# Copy the application into the container.
COPY . /app
WORKDIR /app
RUN pip install uv
ENV PATH=/.local/bin:$PATH
RUN uv venv # activates uv virtual environment - avoids permission issues
RUN uv pip install -r requirements.txt # now, installation is flawless
# entry point (to inspect the container)
CMD ["sh"]
Without RUN uv venv, or when trying to install with RUN uv pip install --system -r requirements.txt, you run into permission errors:
2.499 error: failed to remove file `/opt/bitnami/python/lib/python3.10/site-packages/Jinja2-3.1.3.dist-info/INSTALLER`: Permission denied (os error 13)
build by
docker build -t fastapi-app .
docker run --platform linux/amd64 -it fastapi-app # I am on a arm64 macos
This shell however behaves very weirdly.
- It doesn't recognize
uv. Only afterexport PATH=/.local/bin:$PATHit can recognizeuv. - It was not possible to reactivate the
uvvirtual environment into whichfastapiwas installed into.fastapias well asdotenvetc are present in/app/.venv/bin/. However,$ cd /appand$ uv venvjust overwrites/app/.venvand creates a new virtual environment. When starting directly$ python, thenimport fastapididn't work. - Only
$ uv run pythonrun a Python console which runsimport fastapi. So$ uv run pythonis the solution.
Does someone know how to activate the original virtual environment?
/app/.venv/bin/activate didn't work source was not found when doing source /app/.venv/bin/activate.
Even the PYTHONPATH manipulating didn't work.
I recommend the first solution therefore.
The installed uv binary is not on PATH, and presumably since it's not a regular Python wrapper script, pip doesn't print out the helpful warning about that.
Refer to the .local-installed uv manually:
RUN pip install uv && ~/.local/bin/uv pip install --system --no-cache -r requirements.txt
Note that --system will likely fail since that image is clearly not running these commands as root.
(Personally, I would probably not use a bitnami/deepspeed image.)
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/