From 154fbbd68142280aea48f69e83c7fded841e22de Mon Sep 17 00:00:00 2001 From: RedsAnalysis Date: Sat, 15 Mar 2025 01:22:16 -0400 Subject: [PATCH 1/4] 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 From 1ac7710b8d28a6a885b02526f8dd558cc06f8e8e Mon Sep 17 00:00:00 2001 From: RedsAnalysis Date: Sat, 15 Mar 2025 02:32:58 -0400 Subject: [PATCH 2/4] Update docker-build.sh and docker-compose.yml --- docker-build.sh | 25 ++++++++++++++++++------- docker-compose.yml | 3 ++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/docker-build.sh b/docker-build.sh index ff776d75..9c2b6b8f 100644 --- a/docker-build.sh +++ b/docker-build.sh @@ -7,20 +7,31 @@ then exit 1 fi -# Build and start the container +# Step 1: Build and start the container without mounting the venv volume +echo "Building and starting the container to initialize the virtual environment..." docker-compose up --build -d # Wait for the container logs to indicate it's ready (looking for the custom message) -container_name="comfyui-v1" +container_name="comfyui-red-docker" 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 + sleep 20 done -# Copy the 'venv' directory from the container to the host -docker cp "$container_name:/app/venv" ./data/venv +# Step 2: Copy the 'venv' directory from the container to the host +echo "Copying the virtual environment from the container to the host..." +docker cp "$container_name:/app/venv" ./venv -# Optional: Stop the container after everything is set up +# Step 3: Stop the container +echo "Stopping the container..." docker-compose down -echo "The container is set up, and the virtual environment is ready at ./venv." +# Step 4: Update the Docker Compose file to mount the venv volume +echo "Updating Docker Compose file to mount the virtual environment..." +sed -i '/# Mount the venv directory for persistence/a \ \ \ \ \ \ - ./venv:/app/venv' docker-compose.yml + +# Step 5: Restart the container with the venv volume mounted +echo "Restarting the container with the virtual environment mounted..." +docker-compose up -d + +echo "Setup complete! The container is running with the virtual environment persisted at ./venv." \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 81b49d79..53813303 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,13 +7,14 @@ services: 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 + + # Mount the venv directory for persistence environment: - DISPLAY=${DISPLAY} # Optional, for X11 display forwarding (if you use it) From 1c6acbaf1f4fc9881ba180e48d58ba8f4f598805 Mon Sep 17 00:00:00 2001 From: RedsAnalysis Date: Sat, 15 Mar 2025 02:42:10 -0400 Subject: [PATCH 3/4] Updated docker-build.sh to include error handling for venv cp and in docker-compose.yml added image name tag --- docker-build.sh | 4 ++-- docker-compose.yml | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/docker-build.sh b/docker-build.sh index 9c2b6b8f..992d5a11 100644 --- a/docker-build.sh +++ b/docker-build.sh @@ -9,10 +9,10 @@ fi # Step 1: Build and start the container without mounting the venv volume echo "Building and starting the container to initialize the virtual environment..." -docker-compose up --build -d +COMPOSE_BAKE=true docker-compose up --build -d # Wait for the container logs to indicate it's ready (looking for the custom message) -container_name="comfyui-red-docker" +container_name="comfyui-red-container" 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 20 diff --git a/docker-compose.yml b/docker-compose.yml index 53813303..a55310ed 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,8 @@ services: build: context: . dockerfile: Dockerfile - container_name: comfyui-red-docker + image: comfyui-red-image + container_name: comfyui-red-container ports: - "8188:8188" # Expose the backend API or server volumes: From 974d0b1f1a09e78eaf477c2f491978dc07b5e73d Mon Sep 17 00:00:00 2001 From: RedsAnalysis Date: Sat, 15 Mar 2025 04:27:18 -0400 Subject: [PATCH 4/4] Added Confyui manager to dockerfile, made changes to .sh file to recieve message from main.py, add place holder for venv volume mount --- Dockerfile | 8 +++++ docker-build.sh | 78 +++++++++++++++++++++++++++++++++++++++------- docker-compose.yml | 5 +-- 3 files changed, 77 insertions(+), 14 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6793ad0e..c78c0c2d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -46,6 +46,14 @@ 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 +# Clone the ComfyUI-manager repository into a temporary directory, move it, and clean up +RUN git clone https://github.com/ltdrdata/ComfyUI-Manager.git /app/temp/ComfyUI-Manager && \ + mv /app/temp/* /app/comfyui/custom_nodes/ && \ + rm -rf /app/temp + +# Install ComfyUI-manager dependencies +RUN . venv/bin/activate && pip install -r /app/comfyui/custom_nodes/ComfyUI-Manager/requirements.txt + # Expose the backend port EXPOSE 8188 diff --git a/docker-build.sh b/docker-build.sh index 992d5a11..5c20d985 100644 --- a/docker-build.sh +++ b/docker-build.sh @@ -7,31 +7,85 @@ then exit 1 fi -# Step 1: Build and start the container without mounting the venv volume -echo "Building and starting the container to initialize the virtual environment..." -COMPOSE_BAKE=true docker-compose up --build -d +# Step 1: Build the container without using cache +echo "Building the container to initialize the virtual environment..." +COMPOSE_BAKE=true docker-compose build --no-cache +if [ $? -eq 0 ]; then + echo "Build completed successfully." +else + echo "Build failed. Exiting." + exit 1 +fi + +# Step 2: Start the container without mounting the venv volume +echo "Starting the container..." +COMPOSE_BAKE=true docker-compose up -d +if [ $? -eq 0 ]; then + echo "Container started successfully." +else + echo "Failed to start the container. Exiting." + exit 1 +fi + +# Step 3: Stream Docker logs to the terminal +container_name="comfyui-red-container" +echo "Streaming Docker logs for container: $container_name..." +docker logs -f "$container_name" & +LOGS_PID=$! # Save the PID of the background process # Wait for the container logs to indicate it's ready (looking for the custom message) -container_name="comfyui-red-container" -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..." +echo "Waiting for the container to be fully started..." +while ! docker logs "$container_name" 2>&1 | grep -q "To see the GUI go to: http://0.0.0.0:8188"; do sleep 20 done -# Step 2: Copy the 'venv' directory from the container to the host -echo "Copying the virtual environment from the container to the host..." -docker cp "$container_name:/app/venv" ./venv +# Stop streaming logs (kill the background process) +kill $LOGS_PID +echo "Container is fully started." -# Step 3: Stop the container +# Step 4: Copy the 'venv' directory from the container to the host +echo "Checking if /app/venv exists in the container..." +if docker exec "$container_name" ls /app/venv; then + echo "Copying the virtual environment from the container to the host..." + if ! docker cp "$container_name:/app/venv" ./venv; then + echo "Failed to copy the virtual environment. Exiting." + exit 1 + else + echo "Virtual environment copied successfully." + fi +else + echo "/app/venv does not exist in the container. Exiting." + exit 1 +fi + +# Step 5: Stop the container echo "Stopping the container..." docker-compose down +if [ $? -eq 0 ]; then + echo "Container stopped successfully." +else + echo "Failed to stop the container. Exiting." + exit 1 +fi -# Step 4: Update the Docker Compose file to mount the venv volume +# Step 6: Update the Docker Compose file to mount the venv volume echo "Updating Docker Compose file to mount the virtual environment..." sed -i '/# Mount the venv directory for persistence/a \ \ \ \ \ \ - ./venv:/app/venv' docker-compose.yml +if [ $? -eq 0 ]; then + echo "Docker Compose file updated successfully." +else + echo "Failed to update Docker Compose file. Exiting." + exit 1 +fi -# Step 5: Restart the container with the venv volume mounted +# Step 7: Restart the container with the venv volume mounted echo "Restarting the container with the virtual environment mounted..." docker-compose up -d +if [ $? -eq 0 ]; then + echo "Container restarted successfully." +else + echo "Failed to restart the container. Exiting." + exit 1 +fi echo "Setup complete! The container is running with the virtual environment persisted at ./venv." \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index a55310ed..be888196 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,8 +15,9 @@ services: - ./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 - # Mount the venv directory for persistence - + # Mount the venv directory for persistence(automatically mounted with you run docker-build.sh) + + environment: - DISPLAY=${DISPLAY} # Optional, for X11 display forwarding (if you use it) - NVIDIA_VISIBLE_DEVICES=all # Ensure NVIDIA GPU is available to the container