From 33f01eb0df91692824889a8affc61b8c6873a5e6 Mon Sep 17 00:00:00 2001 From: Bernhard Frauendienst Date: Thu, 7 Sep 2023 21:43:55 +0200 Subject: [PATCH 1/2] Add Dockerfile Based on ComfyUI PR #530 Co-Authored-By: ZacharyACoon --- .dockerignore | 8 +++++ Dockerfile | 78 +++++++++++++++++++++++++++++++++++++++++++++ README.md | 23 +++++++++++++ docker-compose.yaml | 21 ++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..827e02326 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +.* +!.git +__pycache__/ +*.py[cod] +input +models +notebooks +output diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..d6b891068 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,78 @@ +# syntax=docker/dockerfile:1 + +ARG PYTHON_VERSION=3.12 + +FROM python:${PYTHON_VERSION}-slim + +ARG PYTORCH_INSTALL_ARGS="" +ARG EXTRA_ARGS="" +ARG USERNAME=comfyui +ARG USER_UID=1000 +ARG USER_GID=${USER_UID} + +# Fail fast on errors or unset variables +SHELL ["/bin/bash", "-eux", "-o", "pipefail", "-c"] + +RUN < [!NOTE] +> For building the CPU-only image, it is recommended that you add the --cpu flag to the EXTRA_ARGS build arg: +> +> ```shell +> docker build --build-arg PYTORCH_INSTALL_ARGS="--index-url https://download.pytorch.org/whl/cpu" --build-arg EXTRA_ARGS=--cpu . +> ``` + ### Others: #### Apple Mac silicon diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 000000000..858840fbe --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,21 @@ +services: + comfyui: + user: "1000:1000" + build: . + deploy: + resources: + reservations: + devices: + - driver: nvidia + count: all + capabilities: [gpu] + ports: + - "8188:8188" + volumes: + - "./models:/app/models" + - "./input:/app/input" + - "./temp:/app/output/temp" + - "./output:/app/output" + - "./user:/app/user" + - "./custom_venv:/app/custom_venv" + - "./custom_nodes:/app/custom_nodes" From dac0710c8863044f2e3e7a036dd07e76aeb3722f Mon Sep 17 00:00:00 2001 From: Bernhard Frauendienst Date: Sat, 9 Sep 2023 13:57:29 +0200 Subject: [PATCH 2/2] Add GitHub Action for Docker images Add a GitHub Action that builds a docker image on every push and tag, and publishes it to GitHub container repository, and -- if a docker username is defined in the repository secrets -- to the Docker Hub. --- .github/workflows/docker.yml | 101 +++++++++++++++++++++++++++++++++++ Dockerfile | 3 +- README.md | 16 ++++++ 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/docker.yml diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 000000000..541a7aac9 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,101 @@ +name: Build and publish Docker images + +on: + push: + branches: + - master + pull_request: + branches: + - master + release: + types: [published] + +jobs: + docker: + strategy: + fail-fast: false + matrix: + include: + - id: cu118 + name: CUDA 11.8 + pytorch_install_args: "--index-url https://download.pytorch.org/whl/cu118" + - id: cu121 + name: CUDA 12.1 + pytorch_install_args: "--index-url https://download.pytorch.org/whl/cu121" + - id: cu124 + name: CUDA 12.4 + pytorch_install_args: "--index-url https://download.pytorch.org/whl/cu124" + - id: rocm6.2 + name: ROCm 6.2 + pytorch_install_args: "--index-url https://download.pytorch.org/whl/rocm6.2" + - id: cpu + name: CPU only + pytorch_install_args: "--index-url https://download.pytorch.org/whl/cpu" + extra_args: --cpu + + + name: ${{ matrix.name }} + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Check which repositories to use + id: repositories + run: | + echo "GHCR_IMAGE_NAME=ghcr.io/${GITHUB_REPOSITORY_OWNER}/comfyui" >> "$GITHUB_ENV" + if [[ -n "${DOCKERHUB_USERNAME}" ]]; then + echo "DOCKERHUB_IMAGE_NAME=${DOCKERHUB_USERNAME}/comfyui" >> "$GITHUB_ENV" + else + echo "DOCKERHUB_IMAGE_NAME=" >> "$GITHUB_ENV" + echo "No Docker Hub username set, only deploying to GitHub Container Repository" + fi + env: + DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} + GITHUB_REPOSITORY_OWNER: ${{ github.repository_owner }} + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + # list of Docker images to use as base name for tags + images: | + ${{ env.DOCKERHUB_IMAGE_NAME }} + ${{ env.GHCR_IMAGE_NAME }} + flavor: | + suffix=-${{ matrix.id }},onlatest=true + # generate Docker tags based on the following events/attributes + tags: | + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + type=sha + # set latest tag for default branch + type=raw,value=latest,enable=${{ github.event_name == 'release' }} + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Login to Docker Hub + if: github.event_name != 'pull_request' && env.DOCKERHUB_IMAGE_NAME != '' + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Login to GHCR + if: github.event_name != 'pull_request' + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Build and push + uses: docker/build-push-action@v5 + with: + context: . + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + build-args: | + PYTORCH_INSTALL_ARGS=${{ matrix.pytorch_install_args }} + EXTRA_ARGS=${{ matrix.extra_args }} + cache-from: type=gha,scope=${{ github.ref_name }}-${{ matrix.id }} + cache-to: type=gha,mode=max,scope=${{ github.ref_name }}-${{ matrix.id }} diff --git a/Dockerfile b/Dockerfile index d6b891068..ee361f276 100644 --- a/Dockerfile +++ b/Dockerfile @@ -68,6 +68,7 @@ COPY --chown=nobody:${USER_GID} .git .git # default environment variables ENV COMFYUI_ADDRESS=0.0.0.0 ENV COMFYUI_PORT=8188 +ENV COMFYUI_EXTRA_BUILD_ARGS="${EXTRA_ARGS}" ENV COMFYUI_EXTRA_ARGS="" # default start command CMD \ @@ -75,4 +76,4 @@ CMD \ rsync -aP "${VIRTUAL_ENV}/" "${VIRTUAL_ENV_CUSTOM}/" ;\ sed -i "s!${VIRTUAL_ENV}!${VIRTUAL_ENV_CUSTOM}!g" "${VIRTUAL_ENV_CUSTOM}/pyvenv.cfg" ;\ fi ;\ - python -u main.py --listen "${COMFYUI_ADDRESS}" --port "${COMFYUI_PORT}" ${EXTRA_ARGS} ${COMFYUI_EXTRA_ARGS} + python -u main.py --listen "${COMFYUI_ADDRESS}" --port "${COMFYUI_PORT}" ${COMFYUI_EXTRA_BUILD_ARGS} ${COMFYUI_EXTRA_ARGS} diff --git a/README.md b/README.md index bf2da4151..ce24c262c 100644 --- a/README.md +++ b/README.md @@ -238,6 +238,22 @@ After this you should have everything installed and can proceed to running Comfy ## Docker +There are prebuilt docker images for AMD and NVIDIA GPUs on [GitHub Packages](https://ghcr.io/comfyanonymous/comfyui). + +You can pull them to your local docker registry with: + +```shell +# For NVIDIA GPUs +docker pull ghcr.io/comfyanonymous/comfyui:latest-cu124 +# For AMD GPUs +docker pull ghcr.io/comfyanonymous/comfyui:latest-rocm6.2 + +# For CPU only +docker pull ghcr.io/comfyanonymous/comfyui:latest-cpu +``` + +### Building images manually + You can build a docker image with the `Dockerfile` in this repo. Specify, `PYTORCH_INSTALL_ARGS` build arg with one of the PyTorch commands above to build for AMD or NVIDIA GPUs. ```shell