From 5cb877e09614805e91e2027254fb99f702e419d4 Mon Sep 17 00:00:00 2001 From: mauwii Date: Fri, 24 Feb 2023 02:53:27 +0100 Subject: [PATCH 1/7] reorder .dockerignore --- .dockerignore | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.dockerignore b/.dockerignore index 81301571b8..0c177e5912 100644 --- a/.dockerignore +++ b/.dockerignore @@ -4,10 +4,6 @@ !ldm !pyproject.toml -# Guard against pulling in any models that might exist in the directory tree -**/*.pt* -**/*.ckpt - # ignore frontend but whitelist dist invokeai/frontend/ !invokeai/frontend/dist/ @@ -16,10 +12,14 @@ invokeai/frontend/ invokeai/assets/ !invokeai/assets/web/ +# Guard against pulling in any models that might exist in the directory tree +**/*.pt* +**/*.ckpt + # Byte-compiled / optimized / DLL files **/__pycache__/ **/*.py[cod] # Distribution / packaging -*.egg-info/ -*.egg +**/*.egg-info/ +**/*.egg From 90cda118688eeec86ba1ad927e0a230b431c5e3b Mon Sep 17 00:00:00 2001 From: mauwii Date: Fri, 24 Feb 2023 03:51:18 +0100 Subject: [PATCH 2/7] separate installation of requirements and source this should highly increase rebuilding of the image when: - version did not change - requirements didn't change --- docker/Dockerfile | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 230c7e584e..629d252d0b 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -8,11 +8,11 @@ FROM python:${PYTHON_VERSION}-slim AS python-base LABEL org.opencontainers.image.authors="mauwii@outlook.de" -# prepare for buildkit cache +# Prepare apt for buildkit cache 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 -# Install necessary packages +# Install dependencies RUN \ --mount=type=cache,target=/var/cache/apt,sharing=locked \ --mount=type=cache,target=/var/lib/apt,sharing=locked \ @@ -23,7 +23,7 @@ RUN \ libglib2.0-0=2.66.* \ libopencv-dev=4.5.* -# set working directory and env +# Set working directory and env ARG APPDIR=/usr/src ARG APPNAME=InvokeAI WORKDIR ${APPDIR} @@ -32,7 +32,7 @@ ENV PATH ${APPDIR}/${APPNAME}/bin:$PATH ENV PYTHONDONTWRITEBYTECODE 1 # Turns off buffering for easier container logging ENV PYTHONUNBUFFERED 1 -# don't fall back to legacy build system +# Don't fall back to legacy build system ENV PIP_USE_PEP517=1 ####################### @@ -40,7 +40,7 @@ ENV PIP_USE_PEP517=1 ####################### FROM python-base AS pyproject-builder -# Install dependencies +# Install build dependencies RUN \ --mount=type=cache,target=/var/cache/apt,sharing=locked \ --mount=type=cache,target=/var/lib/apt,sharing=locked \ @@ -51,26 +51,39 @@ RUN \ gcc=4:10.2.* \ python3-dev=3.9.* -# prepare pip for buildkit cache +# Install pip-tools +RUN pip install \ + --no-cache-dir \ + pip-tools==6.12.2 + +# Prepare pip for buildkit cache ARG PIP_CACHE_DIR=/var/cache/buildkit/pip ENV PIP_CACHE_DIR ${PIP_CACHE_DIR} RUN mkdir -p ${PIP_CACHE_DIR} -# create virtual environment -RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ +# Create virtual environment +RUN --mount=type=cache,target=${PIP_CACHE_DIR} \ python3 -m venv "${APPNAME}" \ --upgrade-deps -# copy sources -COPY --link . . - -# install pyproject.toml +# Install requirements +COPY --link pyproject.toml . +COPY --link ldm/invoke/_version.py ldm/invoke/__init__.py ldm/invoke/ ARG PIP_EXTRA_INDEX_URL ENV PIP_EXTRA_INDEX_URL ${PIP_EXTRA_INDEX_URL} -RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ +RUN --mount=type=cache,target=${PIP_CACHE_DIR} \ + pip-compile \ + --resolver=backtracking \ + --output-file=requirements.txt \ + pyproject.toml && "${APPNAME}"/bin/pip install \ + -r requirements.txt + +# Install pyproject.toml +COPY --link . . +RUN --mount=type=cache,target=${PIP_CACHE_DIR} \ "${APPNAME}/bin/pip" install . -# build patchmatch +# Build patchmatch RUN python3 -c "from patchmatch import patch_match" ##################### @@ -86,12 +99,12 @@ RUN useradd \ -U \ "${UNAME}" -# create volume directory +# Create volume directory ARG VOLUME_DIR=/data RUN mkdir -p "${VOLUME_DIR}" \ && chown -R "${UNAME}" "${VOLUME_DIR}" -# setup runtime environment +# Setup runtime environment USER ${UNAME} COPY --chown=${UNAME} --from=pyproject-builder ${APPDIR}/${APPNAME} ${APPNAME} ENV INVOKEAI_ROOT ${VOLUME_DIR} From b7718985d5629e7202c749d46152ac3b5a243981 Mon Sep 17 00:00:00 2001 From: mauwii Date: Fri, 24 Feb 2023 03:58:22 +0100 Subject: [PATCH 3/7] update build-container.yml - add branches 'dev/ci/docker/*' and 'dev/docker/*' --- .github/workflows/build-container.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/build-container.yml b/.github/workflows/build-container.yml index 5a8ccdcc1d..8f336af542 100644 --- a/.github/workflows/build-container.yml +++ b/.github/workflows/build-container.yml @@ -5,6 +5,8 @@ on: - 'main' - 'update/ci/docker/*' - 'update/docker/*' + - 'dev/ci/docker/*' + - 'dev/docker/*' paths: - 'pyproject.toml' - 'ldm/**' From 01f8c37bd32d2b0ed0fec5003a9898cd2285d691 Mon Sep 17 00:00:00 2001 From: mauwii Date: Fri, 24 Feb 2023 06:20:44 +0100 Subject: [PATCH 4/7] rename amd flavor to rocm --- .github/workflows/build-container.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-container.yml b/.github/workflows/build-container.yml index 8f336af542..efff1d2f8a 100644 --- a/.github/workflows/build-container.yml +++ b/.github/workflows/build-container.yml @@ -26,11 +26,11 @@ jobs: fail-fast: false matrix: flavor: - - amd + - rocm - cuda - cpu include: - - flavor: amd + - flavor: rocm pip-extra-index-url: 'https://download.pytorch.org/whl/rocm5.2' - flavor: cuda pip-extra-index-url: '' From 49198a61ef3c09940d5a0315873a406b31ea2215 Mon Sep 17 00:00:00 2001 From: mauwii Date: Sun, 26 Feb 2023 00:50:13 +0100 Subject: [PATCH 5/7] enable BuildKit in env.sh --- docker/build.sh | 2 +- docker/env.sh | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/docker/build.sh b/docker/build.sh index 21108d2d21..8bfb9a9ddc 100755 --- a/docker/build.sh +++ b/docker/build.sh @@ -41,7 +41,7 @@ else fi # Build Container -DOCKER_BUILDKIT=1 docker build \ +docker build \ --platform="${PLATFORM:-linux/amd64}" \ --tag="${CONTAINER_IMAGE:-invokeai}" \ ${CONTAINER_FLAVOR:+--build-arg="CONTAINER_FLAVOR=${CONTAINER_FLAVOR}"} \ diff --git a/docker/env.sh b/docker/env.sh index 6eecb24e8e..ee3b54f5f6 100644 --- a/docker/env.sh +++ b/docker/env.sh @@ -49,3 +49,6 @@ CONTAINER_FLAVOR="${CONTAINER_FLAVOR-cuda}" CONTAINER_TAG="${CONTAINER_TAG-"${INVOKEAI_BRANCH##*/}-${CONTAINER_FLAVOR}"}" CONTAINER_IMAGE="${CONTAINER_REGISTRY}/${CONTAINER_REPOSITORY}:${CONTAINER_TAG}" CONTAINER_IMAGE="${CONTAINER_IMAGE,,}" + +# enable docker buildkit +export DOCKER_BUILDKIT=1 From ec1de5ae8b5ffc920e920a72dd912b5d6d54293c Mon Sep 17 00:00:00 2001 From: mauwii Date: Sun, 26 Feb 2023 00:51:30 +0100 Subject: [PATCH 6/7] more detailed volume parameters --- docker/run.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docker/run.sh b/docker/run.sh index c52ccb4c4d..d685788146 100755 --- a/docker/run.sh +++ b/docker/run.sh @@ -21,10 +21,10 @@ docker run \ --tty \ --rm \ --platform="${PLATFORM}" \ - --name="${REPOSITORY_NAME,,}" \ - --hostname="${REPOSITORY_NAME,,}" \ - --mount=source="${VOLUMENAME}",target=/data \ - --mount type=bind,source="$(pwd)"/outputs,target=/data/outputs \ + --name="${REPOSITORY_NAME}" \ + --hostname="${REPOSITORY_NAME}" \ + --mount type=volume,volume-driver=local,source="${VOLUMENAME}",target=/data \ + --mount type=bind,source="$(pwd)"/outputs/,target=/data/outputs/ \ ${MODELSPATH:+--mount="type=bind,source=${MODELSPATH},target=/data/models"} \ ${HUGGING_FACE_HUB_TOKEN:+--env="HUGGING_FACE_HUB_TOKEN=${HUGGING_FACE_HUB_TOKEN}"} \ --publish=9090:9090 \ @@ -32,7 +32,7 @@ docker run \ ${GPU_FLAGS:+--gpus="${GPU_FLAGS}"} \ "${CONTAINER_IMAGE}" ${@:+$@} -# Remove Trash folder +echo -e "\nCleaning trash folder ..." for f in outputs/.Trash*; do if [ -e "$f" ]; then rm -Rf "$f" From 92304b9f8a00e4b0be729f03f89161724c4d851d Mon Sep 17 00:00:00 2001 From: mauwii Date: Sun, 26 Feb 2023 00:53:43 +0100 Subject: [PATCH 7/7] remove pip-tools, still split requirements install - also use user:group for definitions - add `--platform=${TARGETPLATFORM}` to base --- docker/Dockerfile | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 629d252d0b..2c3320cc0f 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -4,7 +4,7 @@ ARG PYTHON_VERSION=3.9 ################## ## base image ## ################## -FROM python:${PYTHON_VERSION}-slim AS python-base +FROM --platform=${TARGETPLATFORM} python:${PYTHON_VERSION}-slim AS python-base LABEL org.opencontainers.image.authors="mauwii@outlook.de" @@ -51,11 +51,6 @@ RUN \ gcc=4:10.2.* \ python3-dev=3.9.* -# Install pip-tools -RUN pip install \ - --no-cache-dir \ - pip-tools==6.12.2 - # Prepare pip for buildkit cache ARG PIP_CACHE_DIR=/var/cache/buildkit/pip ENV PIP_CACHE_DIR ${PIP_CACHE_DIR} @@ -72,11 +67,7 @@ COPY --link ldm/invoke/_version.py ldm/invoke/__init__.py ldm/invoke/ ARG PIP_EXTRA_INDEX_URL ENV PIP_EXTRA_INDEX_URL ${PIP_EXTRA_INDEX_URL} RUN --mount=type=cache,target=${PIP_CACHE_DIR} \ - pip-compile \ - --resolver=backtracking \ - --output-file=requirements.txt \ - pyproject.toml && "${APPNAME}"/bin/pip install \ - -r requirements.txt + "${APPNAME}"/bin/pip install . # Install pyproject.toml COPY --link . . @@ -102,11 +93,11 @@ RUN useradd \ # Create volume directory ARG VOLUME_DIR=/data RUN mkdir -p "${VOLUME_DIR}" \ - && chown -R "${UNAME}" "${VOLUME_DIR}" + && chown -hR "${UNAME}:${UNAME}" "${VOLUME_DIR}" # Setup runtime environment -USER ${UNAME} -COPY --chown=${UNAME} --from=pyproject-builder ${APPDIR}/${APPNAME} ${APPNAME} +USER ${UNAME}:${UNAME} +COPY --chown=${UNAME}:${UNAME} --from=pyproject-builder ${APPDIR}/${APPNAME} ${APPNAME} ENV INVOKEAI_ROOT ${VOLUME_DIR} ENV TRANSFORMERS_CACHE ${VOLUME_DIR}/.cache ENV INVOKE_MODEL_RECONFIGURE "--yes --default_only"