Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Docker integration docs for intermediate layers #7166

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions docs/guides/integration/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,18 +340,31 @@ WORKDIR /app
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-project
uv sync --frozen --no-install-project --link-mode=copy --compile-bytecode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I have a preference for setting an environment variable for this as in astral-sh/uv-docker-example#16

I think we cover bytecode compilation in a separate example though https://docs.astral.sh/uv/guides/integration/docker/#compiling-bytecode

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that bytecode compilation shouldn't be included in this example, as it is already covered afterwards

Copy link
Author

@NiklasRosenstein NiklasRosenstein Sep 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, although I feel obliged to point out that it also talks about caching later but uses a cache mount in this example already. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I missed that when someone added the bind mounts to the example in #6921 — we should probably drop that from here too for clarity.


# Copy the project into the image
ADD . /app

# Sync the project
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
Comment on lines +350 to +351
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't these be added by the ADD . /app invocation?

Copy link
Author

@NiklasRosenstein NiklasRosenstein Sep 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my, I blame tunnel vision. 🤦

In my case I added only the src directory because ADD . /app adds a lot of things that I don't want to add. Thinking about it, with --link-mode=copy, I could probably even --mount the source directory itself as well? Arguably, a very minor optimization as you don't usually have many hundreds of megabytes of source code.

Copy link
Member

@zanieb zanieb Sep 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The source directory needs to stick around because we don't support non-editable installs of workspace members during syncs yet (#5792). Maybe it'd be best to address your case in the documentation by adding a case to the optimization examples for copying the src directory instead of the full project directory?

--mount=type=bind,source=README.md,target=README.md \
uv sync --frozen --link-mode=copy --compile-bytecode
```

Note that the `pyproject.toml` is required to identify the project root and name, but the project
_contents_ are not copied into the image until the final `uv sync` command.
!!! note Explanation

* The `pyproject.toml` is required to identify the project root and name, but the project
_contents_ are not copied into the image until the final `uv sync` command.
* The `README.md` is required if your `pyproject.toml` references it in the `project.readme`
key. Update this if your configuration references a different file (or remove it if your
`pyproject.toml` references no readme).
* Uv by default hardlinks files from the cache directory into the virtual environment. The
cache directory here is only mounted at build time, which Uv will detect and fall back to
copy the files, but issue a warning. Hence, we tell Uv explicitly to copy files instead of
linking them with `--link-mode=copy`.
* Adding `--compile-bytecode` improves the startup performance of your project.

!!! tip

Expand Down
Loading