astral-sh / uv

An extremely fast Python package and project manager, written in Rust.
https://docs.astral.sh/uv
Apache License 2.0
23.69k stars 679 forks source link

`uv` sync in workspace during docker build doesn't install dependencies #8460

Open VVoruganti opened 3 hours ago

VVoruganti commented 3 hours ago

I have a project that is making use of the uv workspaces feature. It has the following structure

.
├── pyproject.toml
├── uv.lock
├── bot/
│   └── pyproject.toml
├── api/
│   └── pyproject.toml
└── agent/
    ├── agent/
    ├── pyroject.toml
    └── dist/

This is my root pyproject.toml

[project]
name = "tutor-gpt"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.11"
dependencies = [
    "honcho-ai>=0.0.15",
    "python-dotenv>=1.0.1",
]

[tool.uv.sources]
agent = { workspace = true }
api = { workspace = true }
bot = { workspace = true }

[tool.uv.workspace]
members = ["agent/", "api/", "bot/"]
exclude = ["www/*"]

The agent package is installed by the other two apps.

I need to build a docker image that setups up the api package so I modified the cached example in the docs.

FROM python:3.11-slim-bullseye

COPY --from=ghcr.io/astral-sh/uv:0.4.9 /uv /bin/uv

# Set Working directory
WORKDIR /app

RUN addgroup --system app && adduser --system --group app
RUN chown -R app:app /app
USER app

# Enable bytecode compilation
ENV UV_COMPILE_BYTECODE=1

# Copy from the cache instead of linking since it's a mounted volume
ENV UV_LINK_MODE=copy

# Install the project's dependencies using the lockfile and settings
RUN --mount=type=cache,target=/root/.cache/uv \
    --mount=type=bind,source=uv.lock,target=uv.lock \
    --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
    uv sync --frozen --no-install-workspace --no-dev

# Copy only requirements to cache them in docker layer
COPY --chown=app:app pyproject.toml uv.lock  /app/

# Place executables in the environment at the front of the path
ENV PATH="/app/.venv/bin:$PATH"

EXPOSE 8000

COPY --chown=app:app agent/ /app/agent/
COPY --chown=app:app bot/ /app/bot/
COPY --chown=app:app api/ /app/api/

RUN --mount=type=cache,target=/root/.cache/uv \
      uv sync --frozen --package api

CMD ["fastapi", "run", "--host", "0.0.0.0", "api/main.py"]

When I run my container I'm getting the following error which indicated to me that none of the packages installed.

docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "fastapi": executable file not found in $PATH: unknown.

I verified this by opening a shell in the container

app@612b3a28fa65:/app$ uv pip list
app@612b3a28fa65:/app$ uv sync --package api
Resolved 54 packages in 2ms
Audited in 0.00ms
app@612b3a28fa65:/app$

I've noticed that I can force the container to install packages if I remove the uv.lock and the --frozen tag, but that not desirable for having truly reproducible builds.

charliermarsh commented 3 hours ago

Do you mind providing a reproduction with code, like a Git repo I can clone? I'm happy to help but it's time consuming to scaffold this stuff.