-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Optimized Docker build with support for external working directory #1544
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 all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
40c2286
add docker build optimized for size; do not copy models to image
ebr f2b03ef
add workflow to build cloud img in ci
ebr a13148c
push cloud image in addition to building
ebr ca2224c
(ci) also tag docker images with git SHA
ebr 7a514d9
(docker) rework Makefile for easy cache population and local use
ebr cf0fe13
support the new conda-less install; further optimize docker build
ebr 162b075
(ci) clean up the build-cloud-img action
ebr 6ebac30
improve the Makefile for local use
ebr 5e3a7e6
move execution of invoke script from entrypoint to cmd, allows overri…
ebr f9d1a2b
remove unnecessary copyright statements
ebr c1da896
(docs) add a section on running InvokeAI in the cloud using Docker
ebr 2d34d0d
(docker) add patchmatch to the cloud image; improve build caching; si…
ebr 5647ff6
(docker) fix pip requirements path to use binary_installer directory
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 |
---|---|---|
@@ -1,12 +1,26 @@ | ||
* | ||
!backend | ||
!configs | ||
!environments-and-requirements | ||
!frontend | ||
!installer | ||
!binary_installer | ||
!ldm | ||
!main.py | ||
!scripts | ||
!server | ||
!static | ||
!setup.py | ||
!docker-build | ||
!docs | ||
docker-build/Dockerfile | ||
|
||
# Guard against pulling in any models that might exist in the directory tree | ||
**/*.pt* | ||
|
||
# unignore configs, but only ignore the custom models.yaml, in case it exists | ||
!configs | ||
configs/models.yaml | ||
|
||
# unignore environment dirs/files, but ignore the environment.yml file or symlink in case it exists | ||
!environment* | ||
environment.yml | ||
|
||
**/__pycache__ |
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
name: Build and push cloud image | ||
on: | ||
workflow_dispatch: | ||
push: | ||
branches: | ||
- main | ||
- development | ||
tags: | ||
- v* | ||
|
||
permissions: | ||
contents: read | ||
packages: write | ||
|
||
env: | ||
REGISTRY: ghcr.io | ||
IMAGE_NAME: ${{ github.repository }} | ||
|
||
jobs: | ||
docker: | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
# only x86_64 for now. aarch64+cuda isn't really a thing yet | ||
arch: | ||
- x86_64 | ||
runs-on: ubuntu-latest | ||
name: ${{ matrix.arch }} | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Docker meta | ||
id: meta | ||
uses: docker/metadata-action@v4 | ||
with: | ||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | ||
tags: | | ||
type=ref,event=branch | ||
type=ref,event=tag | ||
type=ref,event=pr | ||
type=sha | ||
|
||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v2 | ||
|
||
- if: github.event_name != 'pull_request' | ||
name: Docker login | ||
uses: docker/login-action@v2 | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Build and push cloud image | ||
uses: docker/build-push-action@v3 | ||
with: | ||
context: . | ||
file: docker-build/Dockerfile.cloud | ||
platforms: Linux/${{ matrix.arch }} | ||
push: ${{ github.event_name != 'pull_request' }} | ||
tags: ${{ steps.meta.outputs.tags }} | ||
labels: ${{ steps.meta.outputs.labels }} |
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
####################### | ||
#### Builder stage #### | ||
|
||
FROM library/ubuntu:22.04 AS builder | ||
|
||
ARG DEBIAN_FRONTEND=noninteractive | ||
RUN rm -f /etc/apt/apt.conf.d/docker-clean; echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache | ||
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ | ||
--mount=type=cache,target=/var/lib/apt,sharing=locked \ | ||
apt update && apt-get install -y \ | ||
git \ | ||
libglib2.0-0 \ | ||
libgl1-mesa-glx \ | ||
python3-venv \ | ||
python3-pip \ | ||
build-essential \ | ||
python3-opencv \ | ||
libopencv-dev | ||
|
||
# This is needed for patchmatch support | ||
RUN cd /usr/lib/x86_64-linux-gnu/pkgconfig/ &&\ | ||
ln -sf opencv4.pc opencv.pc | ||
|
||
ARG WORKDIR=/invokeai | ||
WORKDIR ${WORKDIR} | ||
|
||
ENV VIRTUAL_ENV=${WORKDIR}/.venv | ||
ENV PATH="$VIRTUAL_ENV/bin:$PATH" | ||
|
||
RUN --mount=type=cache,target=/root/.cache/pip \ | ||
python3 -m venv ${VIRTUAL_ENV} &&\ | ||
pip install --extra-index-url https://download.pytorch.org/whl/cu116 \ | ||
torch==1.12.0+cu116 \ | ||
torchvision==0.13.0+cu116 &&\ | ||
pip install -e git+https://github.com/invoke-ai/PyPatchMatch@0.1.3#egg=pypatchmatch | ||
|
||
COPY . . | ||
RUN --mount=type=cache,target=/root/.cache/pip \ | ||
cp binary_installer/py3.10-linux-x86_64-cuda-reqs.txt requirements.txt && \ | ||
pip install -r requirements.txt &&\ | ||
pip install -e . | ||
|
||
|
||
####################### | ||
#### Runtime stage #### | ||
|
||
FROM library/ubuntu:22.04 as runtime | ||
|
||
ARG DEBIAN_FRONTEND=noninteractive | ||
ENV PYTHONUNBUFFERED=1 | ||
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ | ||
--mount=type=cache,target=/var/lib/apt,sharing=locked \ | ||
apt update && apt install -y --no-install-recommends \ | ||
git \ | ||
curl \ | ||
ncdu \ | ||
iotop \ | ||
bzip2 \ | ||
libglib2.0-0 \ | ||
libgl1-mesa-glx \ | ||
python3-venv \ | ||
python3-pip \ | ||
build-essential \ | ||
python3-opencv \ | ||
libopencv-dev &&\ | ||
apt-get clean && apt-get autoclean | ||
|
||
ARG WORKDIR=/invokeai | ||
WORKDIR ${WORKDIR} | ||
|
||
ENV INVOKEAI_ROOT=/mnt/invokeai | ||
ENV VIRTUAL_ENV=${WORKDIR}/.venv | ||
ENV PATH="$VIRTUAL_ENV/bin:$PATH" | ||
|
||
COPY --from=builder ${WORKDIR} ${WORKDIR} | ||
COPY --from=builder /usr/lib/x86_64-linux-gnu/pkgconfig /usr/lib/x86_64-linux-gnu/pkgconfig | ||
|
||
# build patchmatch | ||
RUN python -c "from patchmatch import patch_match" | ||
|
||
## workaround for non-existent initfile when runtime directory is mounted; see #1613 | ||
RUN touch /root/.invokeai | ||
|
||
ENTRYPOINT ["bash"] | ||
mauwii marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
CMD ["-c", "python3 scripts/invoke.py --web --host 0.0.0.0"] |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Directory in the container where the INVOKEAI_ROOT (runtime dir) will be mounted | ||
INVOKEAI_ROOT=/mnt/invokeai | ||
# Host directory to contain the runtime dir. Will be mounted at INVOKEAI_ROOT path in the container | ||
HOST_MOUNT_PATH=${HOME}/invokeai | ||
|
||
IMAGE=local/invokeai:latest | ||
|
||
USER=$(shell id -u) | ||
GROUP=$(shell id -g) | ||
|
||
# All downloaded models, config, etc will end up in ${HOST_MOUNT_PATH} on the host. | ||
# This is consistent with the expected non-Docker behaviour. | ||
# Contents can be moved to a persistent storage and used to prime the cache on another host. | ||
|
||
build: | ||
DOCKER_BUILDKIT=1 docker build -t local/invokeai:latest -f Dockerfile.cloud .. | ||
|
||
configure: | ||
docker run --rm -it --runtime=nvidia --gpus=all \ | ||
-v ${HOST_MOUNT_PATH}:${INVOKEAI_ROOT} \ | ||
-e INVOKEAI_ROOT=${INVOKEAI_ROOT} \ | ||
${IMAGE} -c "python scripts/configure_invokeai.py" | ||
|
||
# Run the container with the runtime dir mounted and the web server exposed on port 9090 | ||
web: | ||
docker run --rm -it --runtime=nvidia --gpus=all \ | ||
-v ${HOST_MOUNT_PATH}:${INVOKEAI_ROOT} \ | ||
-e INVOKEAI_ROOT=${INVOKEAI_ROOT} \ | ||
-p 9090:9090 \ | ||
${IMAGE} -c "python scripts/invoke.py --web --host 0.0.0.0" | ||
|
||
# Run the cli with the runtime dir mounted | ||
cli: | ||
docker run --rm -it --runtime=nvidia --gpus=all \ | ||
-v ${HOST_MOUNT_PATH}:${INVOKEAI_ROOT} \ | ||
-e INVOKEAI_ROOT=${INVOKEAI_ROOT} \ | ||
${IMAGE} -c "python scripts/invoke.py" | ||
|
||
# Run the container with the runtime dir mounted and open a bash shell | ||
shell: | ||
docker run --rm -it --runtime=nvidia --gpus=all \ | ||
-v ${HOST_MOUNT_PATH}:${INVOKEAI_ROOT} ${IMAGE} -- | ||
|
||
.PHONY: build configure web cli shell |
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
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.