From 154fbbd68142280aea48f69e83c7fded841e22de Mon Sep 17 00:00:00 2001 From: RedsAnalysis Date: Sat, 15 Mar 2025 01:22:16 -0400 Subject: [PATCH] Add Docker build script, Dockerfile, and Docker Compose setup for GPU compatibility --- Dockerfile | 53 ++++++++++++++++++++++++++++++++++++++++++++++ docker-build.sh | 26 +++++++++++++++++++++++ docker-compose.yml | 23 ++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 Dockerfile create mode 100644 docker-build.sh create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..6793ad0e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,53 @@ +FROM nvidia/cuda:12.1.0-base-ubuntu20.04 AS base +ENV DEBIAN_FRONTEND=noninteractive + +# Install necessary dependencies and Python 3.12 +RUN apt-get update \ + && apt-get install -y \ + git \ + software-properties-common \ + curl \ + && add-apt-repository ppa:deadsnakes/ppa \ + && apt-get update \ + && apt-get install -y \ + python3.12 \ + python3.12-dev \ + python3.12-venv \ + python3-setuptools \ + wget \ + ffmpeg \ + libsm6 \ + libxext6 \ + libgl1 \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +# Install pip for Python 3.12 explicitly +RUN curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py \ + && python3.12 get-pip.py \ + && rm get-pip.py + +# Set Python 3.12 as the default python3 and pip +RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1 \ + && update-alternatives --install /usr/bin/pip pip /usr/local/bin/pip3 1 + +# Set the working directory +WORKDIR /app + +# Clone the ComfyUI repository into the working directory +RUN git clone https://github.com/comfyanonymous/ComfyUI.git /app/comfyui + +# Install backend dependencies +RUN python3 -m venv /app/venv +RUN . venv/bin/activate && pip install --upgrade pip +RUN . venv/bin/activate && pip install pyyaml +RUN . venv/bin/activate && pip install -r /app/comfyui/requirements.txt + +# Install PyTorch with CUDA 12.1 support +RUN . venv/bin/activate && pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu121 + +# Expose the backend port +EXPOSE 8188 + +# Set the entrypoint command to activate the virtual environment and run the script +CMD ["/bin/bash", "-c", "source /app/venv/bin/activate && python3 /app/comfyui/main.py --listen 0.0.0.0 --port 8188 && echo 'Server started and ready to accept requests'"] diff --git a/docker-build.sh b/docker-build.sh new file mode 100644 index 00000000..ff776d75 --- /dev/null +++ b/docker-build.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Check if Docker and Docker Compose are installed +if ! command -v docker &> /dev/null || ! command -v docker-compose &> /dev/null +then + echo "Docker or Docker Compose not found. Please install them before proceeding." + exit 1 +fi + +# Build and start the container +docker-compose up --build -d + +# Wait for the container logs to indicate it's ready (looking for the custom message) +container_name="comfyui-v1" +while ! docker logs "$container_name" 2>&1 | grep -q "Server started and ready to accept requests"; do + echo "Waiting for the container to be fully started..." + sleep 1 +done + +# Copy the 'venv' directory from the container to the host +docker cp "$container_name:/app/venv" ./data/venv + +# Optional: Stop the container after everything is set up +docker-compose down + +echo "The container is set up, and the virtual environment is ready at ./venv." diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..81b49d79 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,23 @@ +services: + comfyui_backend: + build: + context: . + dockerfile: Dockerfile + container_name: comfyui-red-docker + ports: + - "8188:8188" # Expose the backend API or server + volumes: + - ./venv:/app/venv # Mount the venv directory for persistence helps with downloading custom nodes's dependencies from ConfyUI-Manager + - ./input:/app/comfyui/input # Mount the input directory directly inside /app/comfyui + - ./models:/app/comfyui/models # Mount the models directory directly inside /app/comfyui + - ./output:/app/comfyui/output # Mount the output directory directly inside /app/comfyui + - ./custom_nodes:/app/comfyui/custom_nodes # Mount the custom nodes directory directly inside /app/comfyui + - ./user:/app/comfyui/user # Comment this line and uncomment the next line if you want to have a workflows directory in the root directory. + #- ./workflows:/app/comfyui/user/default/workflows + + environment: + - DISPLAY=${DISPLAY} # Optional, for X11 display forwarding (if you use it) + - NVIDIA_VISIBLE_DEVICES=all # Ensure NVIDIA GPU is available to the container + - NVIDIA_DRIVER_CAPABILITIES=all # For CUDA support + runtime: nvidia # Use the NVIDIA runtime for GPU support + restart: "no" #change to "always" if you want to restart the container \ No newline at end of file