Disclosure: I'm currently learning how to use the uv
package manager as well as, concurrently, Docker.
Say my folder structure is:
my_project/
├── src/
│ └── __init__.py
│ └── utils.py
│ └── hello_world.py
├── pyproject.toml
├── README.md
├── uv.lock
In order to call src
as a package I configured my pyproject.toml
the following way:
[project]
name = "project-name"
version = "0.1.0"
description = "project-description"
authors = [
{name = "my-name", email = "[email protected]"}
]
dependencies = []
readme = "README.md"
requires-python = "==3.12.*"
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"
[tool.setuptools]
packages = ["src"]
This way I can call some functionality of the utils.py
file in the hello_world.py
file like:
from src.utils import print_hello_world
print_hello_world()
I then tried to 'dockerize' my simple app. The Dockerfile is:
# Use a Python image with uv pre-installed
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim
WORKDIR /app
ADD . /app
RUN uv sync
ENTRYPOINT [ "uv", "run", "src/hello_world.py" ]
I then built the image with docker build -t myimage .
And ran it with docker run myimage
.
For some reason whenever I run the image uv
builds my project before printing the "Hello, world!" message.
user@host:~/projects/my-project$ docker run -it myimage:latest
Built project-name @ file:///app
Uninstalled 1 package in 4ms
Installed 1 package in 3ms
Hello, world!
However, if I do the same steps detailed in the Dockerfile but on my machine no 'build' stage seems to take place whenever I run my file with uv run
. E.g.:
user@host:~/projects/my-project$ uv lock
Using CPython 3.12.0
Resolved 1 package in 0.71ms
user@host:~/projects/my-project$ uv sync
Using CPython 3.12.0
Creating virtual environment at: .venv
Resolved 1 package in 0.71ms
Installed 1 package in 52ms
+ project-name==0.1.0 (from file:///home/fmppo/projects/so-question)
user@host:~/projects/my-project$ uv run src/hello_world.py
Hello, world!
Could you help me understand why this is happening?