Skip to content

Commit 4de6fd3

Browse files
authored
chore(docker): reduce size between docker builds (#7571)
by adding a layer with all the pytorch dependencies that don't change most of the time. ## Summary Every time the [`main` docker images](https://github.com/invoke-ai/InvokeAI/pkgs/container/invokeai) rebuild and I pull `main-cuda`, it gets another 3+ GB, which seems like about a zillion times too much since most things don't change from one commit on `main` to the next. This is an attempt to follow the guidance in [Using uv in Docker: Intermediate Layers](https://docs.astral.sh/uv/guides/integration/docker/#intermediate-layers) so there's one layer that installs all the dependencies—including PyTorch with its bundled nvidia libraries—_before_ the project's own frequently-changing files are copied in to the image. ## Related Issues / Discussions - [Improved docker layer cache with uv](https://discord.com/channels/1020123559063990373/1329975172022927370) - [astral: Can `uv pip install` torch, but not `uv sync` it](https://discord.com/channels/1039017663004942429/1329986610770612347) ## QA Instructions Hopefully the CI system building the docker images is sufficient. But there is one change to `pyproject.toml` related to xformers, so it'd be worth checking that `python -m xformers.info` still says it has triton on the platforms that expect it. ## Merge Plan I don't expect this to be a disruptive merge. (An earlier revision of this PR moved the venv, but I've reverted that change at ebr's recommendation.) ## Checklist - [ ] _The PR has a short but descriptive title, suitable for a changelog_ - [ ] _Tests added / updated (if applicable)_ - [ ] _Documentation added / updated (if applicable)_ - [ ] _Updated `What's New` copy (if doing a release after this PR)_
2 parents ea2320c + 3feb1a6 commit 4de6fd3

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

Diff for: docker/Dockerfile

+33-17
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,63 @@ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
1313
git
1414

1515
# Install `uv` for package management
16-
COPY --from=ghcr.io/astral-sh/uv:0.5.5 /uv /uvx /bin/
16+
COPY --from=ghcr.io/astral-sh/uv:0.6.0 /uv /uvx /bin/
1717

1818
ENV VIRTUAL_ENV=/opt/venv
1919
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
2020
ENV INVOKEAI_SRC=/opt/invokeai
2121
ENV PYTHON_VERSION=3.11
22+
ENV UV_PYTHON=3.11
2223
ENV UV_COMPILE_BYTECODE=1
2324
ENV UV_LINK_MODE=copy
25+
ENV UV_PROJECT_ENVIRONMENT="$VIRTUAL_ENV"
26+
ENV UV_INDEX="https://download.pytorch.org/whl/cu124"
2427

2528
ARG GPU_DRIVER=cuda
26-
ARG TARGETPLATFORM="linux/amd64"
2729
# unused but available
2830
ARG BUILDPLATFORM
2931

3032
# Switch to the `ubuntu` user to work around dependency issues with uv-installed python
3133
RUN mkdir -p ${VIRTUAL_ENV} && \
3234
mkdir -p ${INVOKEAI_SRC} && \
33-
chmod -R a+w /opt
35+
chmod -R a+w /opt && \
36+
mkdir ~ubuntu/.cache && chown ubuntu: ~ubuntu/.cache
3437
USER ubuntu
3538

36-
# Install python and create the venv
37-
RUN uv python install ${PYTHON_VERSION} && \
38-
uv venv --relocatable --prompt "invoke" --python ${PYTHON_VERSION} ${VIRTUAL_ENV}
39+
# Install python
40+
RUN --mount=type=cache,target=/home/ubuntu/.cache/uv,uid=1000,gid=1000 \
41+
uv python install ${PYTHON_VERSION}
3942

4043
WORKDIR ${INVOKEAI_SRC}
41-
COPY invokeai ./invokeai
42-
COPY pyproject.toml ./
4344

44-
# Editable mode helps use the same image for development:
45-
# the local working copy can be bind-mounted into the image
46-
# at path defined by ${INVOKEAI_SRC}
45+
# Install project's dependencies as a separate layer so they aren't rebuilt every commit.
46+
# bind-mount instead of copy to defer adding sources to the image until next layer.
47+
#
4748
# NOTE: there are no pytorch builds for arm64 + cuda, only cpu
4849
# x86_64/CUDA is the default
4950
RUN --mount=type=cache,target=/home/ubuntu/.cache/uv,uid=1000,gid=1000 \
51+
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
52+
--mount=type=bind,source=invokeai/version,target=invokeai/version \
53+
if [ "$TARGETPLATFORM" = "linux/arm64" ] || [ "$GPU_DRIVER" = "cpu" ]; then \
54+
UV_INDEX="https://download.pytorch.org/whl/cpu"; \
55+
elif [ "$GPU_DRIVER" = "rocm" ]; then \
56+
UV_INDEX="https://download.pytorch.org/whl/rocm6.1"; \
57+
fi && \
58+
uv sync --no-install-project
59+
60+
# Now that the bulk of the dependencies have been installed, copy in the project files that change more frequently.
61+
COPY invokeai invokeai
62+
COPY pyproject.toml .
63+
64+
RUN --mount=type=cache,target=/home/ubuntu/.cache/uv,uid=1000,gid=1000 \
65+
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
5066
if [ "$TARGETPLATFORM" = "linux/arm64" ] || [ "$GPU_DRIVER" = "cpu" ]; then \
51-
extra_index_url_arg="--extra-index-url https://download.pytorch.org/whl/cpu"; \
67+
UV_INDEX="https://download.pytorch.org/whl/cpu"; \
5268
elif [ "$GPU_DRIVER" = "rocm" ]; then \
53-
extra_index_url_arg="--extra-index-url https://download.pytorch.org/whl/rocm6.1"; \
54-
else \
55-
extra_index_url_arg="--extra-index-url https://download.pytorch.org/whl/cu124"; \
69+
UV_INDEX="https://download.pytorch.org/whl/rocm6.1"; \
5670
fi && \
57-
uv pip install --python ${PYTHON_VERSION} $extra_index_url_arg -e "."
71+
uv sync
72+
5873

5974
#### Build the Web UI ------------------------------------
6075

@@ -98,6 +113,7 @@ RUN apt update && apt install -y --no-install-recommends \
98113

99114
ENV INVOKEAI_SRC=/opt/invokeai
100115
ENV VIRTUAL_ENV=/opt/venv
116+
ENV UV_PROJECT_ENVIRONMENT="$VIRTUAL_ENV"
101117
ENV PYTHON_VERSION=3.11
102118
ENV INVOKEAI_ROOT=/invokeai
103119
ENV INVOKEAI_HOST=0.0.0.0
@@ -109,7 +125,7 @@ ENV CONTAINER_GID=${CONTAINER_GID:-1000}
109125
# Install `uv` for package management
110126
# and install python for the ubuntu user (expected to exist on ubuntu >=24.x)
111127
# this is too tiny to optimize with multi-stage builds, but maybe we'll come back to it
112-
COPY --from=ghcr.io/astral-sh/uv:0.5.5 /uv /uvx /bin/
128+
COPY --from=ghcr.io/astral-sh/uv:0.6.0 /uv /uvx /bin/
113129
USER ubuntu
114130
RUN uv python install ${PYTHON_VERSION}
115131
USER root

Diff for: pyproject.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ dependencies = [
101101
"xformers" = [
102102
# Core generation dependencies, pinned for reproducible builds.
103103
"xformers>=0.0.28.post1; sys_platform!='darwin'",
104-
# Auxiliary dependencies, pinned only if necessary.
105-
"triton; sys_platform=='linux'",
104+
# torch 2.4+cu carries its own triton dependency
106105
]
107106
"onnx" = ["onnxruntime"]
108107
"onnx-cuda" = ["onnxruntime-gpu"]

0 commit comments

Comments
 (0)