-
Notifications
You must be signed in to change notification settings - Fork 2.6k
chore(docker): reduce size between docker builds #7571
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3848e19
chore(docker): reduce size between docker builds
keturn 275d891
Merge branch 'main' into build/docker-dependency-layer
keturn 2236235
chore(docker): revert to keeping venv in /opt/venv
80d38c0
chore(docker): include fewer files while installing dependencies
keturn 0362bd5
Merge branch 'main' into build/docker-dependency-layer
ebr 3feb1a6
Merge branch 'main' into build/docker-dependency-layer
ebr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,52 +13,62 @@ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ | |
git | ||
|
||
# Install `uv` for package management | ||
COPY --from=ghcr.io/astral-sh/uv:0.5.5 /uv /uvx /bin/ | ||
COPY --from=ghcr.io/astral-sh/uv:0.5.21 /uv /uvx /bin/ | ||
|
||
ENV VIRTUAL_ENV=/opt/venv | ||
ENV PATH="$VIRTUAL_ENV/bin:$PATH" | ||
ENV INVOKEAI_SRC=/opt/invokeai | ||
ENV PYTHON_VERSION=3.11 | ||
ENV UV_PYTHON=3.11 | ||
ENV UV_COMPILE_BYTECODE=1 | ||
ENV UV_LINK_MODE=copy | ||
ENV UV_INDEX="https://download.pytorch.org/whl/cu124" | ||
|
||
ARG GPU_DRIVER=cuda | ||
ARG TARGETPLATFORM="linux/amd64" | ||
# unused but available | ||
ARG BUILDPLATFORM | ||
|
||
# Switch to the `ubuntu` user to work around dependency issues with uv-installed python | ||
RUN mkdir -p ${VIRTUAL_ENV} && \ | ||
mkdir -p ${INVOKEAI_SRC} && \ | ||
RUN mkdir -p ${INVOKEAI_SRC} && \ | ||
chmod -R a+w /opt | ||
USER ubuntu | ||
|
||
# Install python and create the venv | ||
RUN uv python install ${PYTHON_VERSION} && \ | ||
uv venv --relocatable --prompt "invoke" --python ${PYTHON_VERSION} ${VIRTUAL_ENV} | ||
# Install python | ||
RUN --mount=type=cache,target=/home/ubuntu/.cache/uv,uid=1000,gid=1000 \ | ||
uv python install ${PYTHON_VERSION} | ||
|
||
WORKDIR ${INVOKEAI_SRC} | ||
COPY invokeai ./invokeai | ||
COPY pyproject.toml ./ | ||
|
||
# Editable mode helps use the same image for development: | ||
# the local working copy can be bind-mounted into the image | ||
# at path defined by ${INVOKEAI_SRC} | ||
# Install project's dependencies as a separate layer so they aren't rebuilt every commit. | ||
# bind-mount instead of copy to defer adding sources to the image until next layer. | ||
# | ||
# NOTE: there are no pytorch builds for arm64 + cuda, only cpu | ||
# x86_64/CUDA is the default | ||
RUN --mount=type=cache,target=/home/ubuntu/.cache/uv,uid=1000,gid=1000 \ | ||
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \ | ||
--mount=type=bind,source=invokeai,target=invokeai \ | ||
if [ "$TARGETPLATFORM" = "linux/arm64" ] || [ "$GPU_DRIVER" = "cpu" ]; then \ | ||
UV_INDEX="https://download.pytorch.org/whl/cpu"; \ | ||
elif [ "$GPU_DRIVER" = "rocm" ]; then \ | ||
UV_INDEX="https://download.pytorch.org/whl/rocm6.1"; \ | ||
fi && \ | ||
uv sync --no-install-project | ||
|
||
# Now that the bulk of the dependencies have been installed, copy in the project files that change more frequently. | ||
COPY invokeai invokeai | ||
COPY pyproject.toml . | ||
|
||
RUN --mount=type=cache,target=/home/ubuntu/.cache/uv,uid=1000,gid=1000 \ | ||
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \ | ||
if [ "$TARGETPLATFORM" = "linux/arm64" ] || [ "$GPU_DRIVER" = "cpu" ]; then \ | ||
extra_index_url_arg="--extra-index-url https://download.pytorch.org/whl/cpu"; \ | ||
UV_INDEX="https://download.pytorch.org/whl/cpu"; \ | ||
elif [ "$GPU_DRIVER" = "rocm" ]; then \ | ||
extra_index_url_arg="--extra-index-url https://download.pytorch.org/whl/rocm6.1"; \ | ||
else \ | ||
extra_index_url_arg="--extra-index-url https://download.pytorch.org/whl/cu124"; \ | ||
UV_INDEX="https://download.pytorch.org/whl/rocm6.1"; \ | ||
fi && \ | ||
uv pip install --python ${PYTHON_VERSION} $extra_index_url_arg -e "." | ||
uv sync | ||
|
||
|
||
#### Build the Web UI ------------------------------------ | ||
|
||
FROM node:20-slim AS web-builder | ||
FROM docker.io/node:20-slim AS web-builder | ||
ENV PNPM_HOME="/pnpm" | ||
ENV PATH="$PNPM_HOME:$PATH" | ||
RUN corepack use [email protected] | ||
|
@@ -97,26 +107,24 @@ RUN apt update && apt install -y --no-install-recommends \ | |
apt-get clean && apt-get autoclean | ||
|
||
ENV INVOKEAI_SRC=/opt/invokeai | ||
ENV VIRTUAL_ENV=/opt/venv | ||
ENV PYTHON_VERSION=3.11 | ||
ENV INVOKEAI_ROOT=/invokeai | ||
ENV INVOKEAI_HOST=0.0.0.0 | ||
ENV INVOKEAI_PORT=9090 | ||
ENV PATH="$VIRTUAL_ENV/bin:$INVOKEAI_SRC:$PATH" | ||
ENV PATH="$INVOKEAI_SRC/.venv/bin:$INVOKEAI_SRC:$PATH" | ||
ENV CONTAINER_UID=${CONTAINER_UID:-1000} | ||
ENV CONTAINER_GID=${CONTAINER_GID:-1000} | ||
|
||
# Install `uv` for package management | ||
# and install python for the ubuntu user (expected to exist on ubuntu >=24.x) | ||
# this is too tiny to optimize with multi-stage builds, but maybe we'll come back to it | ||
COPY --from=ghcr.io/astral-sh/uv:0.5.5 /uv /uvx /bin/ | ||
COPY --from=ghcr.io/astral-sh/uv:0.5.21 /uv /uvx /bin/ | ||
USER ubuntu | ||
RUN uv python install ${PYTHON_VERSION} | ||
USER root | ||
|
||
# --link requires buldkit w/ dockerfile syntax 1.4 | ||
COPY --link --from=builder ${INVOKEAI_SRC} ${INVOKEAI_SRC} | ||
COPY --link --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV} | ||
COPY --link --from=web-builder /build/dist ${INVOKEAI_SRC}/invokeai/frontend/web/dist | ||
|
||
# Link amdgpu.ids for ROCm builds | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.