runpod-worker-comfy
Run ComfyUI as a serverless API on RunPod.
500K+
timpietruskyblibla/runpod-worker-comfy:3.6.0-base: doesn't contain anything, just a clean ComfyUItimpietruskyblibla/runpod-worker-comfy:3.6.0-flux1-schnell: contains the checkpoint, text encoders and VAE for FLUX.1 schnelltimpietruskyblibla/runpod-worker-comfy:3.6.0-flux1-dev: contains the checkpoint, text encoders and VAE for FLUX.1 devtimpietruskyblibla/runpod-worker-comfy:3.6.0-sdxl: contains the checkpoint and VAE for Stable Diffusion XLtimpietruskyblibla/runpod-worker-comfy:3.6.0-sd3: contains the checkpoint for Stable Diffusion 3 mediumtimpietruskyblibla/runpod-worker-comfy:3.6.0-flux1-schnell: contains the flux1-schnell.safetensors checkpoint, the clip_l.safetensors + t5xxl_fp8_e4m3fn.safetensors text encoders and ae.safetensors VAE for FLUX.1-schnelltimpietruskyblibla/runpod-worker-comfy:3.6.0-flux1-dev: contains the flux1-dev.safetensors checkpoint, the clip_l.safetensors + t5xxl_fp8_e4m3fn.safetensors text encoders and ae.safetensors VAE for FLUX.1-devtimpietruskyblibla/runpod-worker-comfy:3.6.0-sdxl: contains the checkpoints and VAE for Stable Diffusion XL
timpietruskyblibla/runpod-worker-comfy:3.6.0-sd3: contains the sd3_medium_incl_clips_t5xxlfp8.safetensors checkpoint for Stable Diffusion 3 medium| Environment Variable | Description | Default |
|---|---|---|
REFRESH_WORKER | When you want to stop the worker after each finished job to have a clean state, see official documentation. | false |
COMFY_POLLING_INTERVAL_MS | Time to wait between poll attempts in milliseconds. | 250 |
COMFY_POLLING_MAX_RETRIES | Maximum number of poll attempts. This should be increased the longer your workflow is running. | 500 |
SERVE_API_LOCALLY | Enable local API server for development and testing. See Local Testing for more details. | disabled |
This is only needed if you want to upload the generated picture to AWS S3. If you don't configure this, your image will be exported as base64-encoded string.
BUCKET_ENDPOINT_URL)BUCKET_ACCESS_KEY_ID & BUCKET_SECRET_ACCESS_KEY) for that IAM| Environment Variable | Description | Example |
|---|---|---|
BUCKET_ENDPOINT_URL | The endpoint URL of your S3 bucket. | https://<bucket>.s3.<region>.amazonaws.com |
BUCKET_ACCESS_KEY_ID | Your AWS access key ID for accessing the S3 bucket. | AKIAIOSFODNN7EXAMPLE |
BUCKET_SECRET_ACCESS_KEY | Your AWS secret access key for accessing the S3 bucket. | wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY |
New Templaterunpod-worker-comfy (it can be anything you want)<dockerhub_username>/<repository_name>:tag, in this case: timpietruskyblibla/runpod-worker-comfy:3.6.0-sd3 (or -base for a clean image or -sdxl for Stable Diffusion XL or -flex1-schnell for FLUX.1 schnell)20 GBSave TemplateNavigate to Serverless > Endpoints and click on New Endpoint
In the dialog, configure:
comfy0 (whatever makes sense for you)3 (whatever makes sense for you)15 (you can leave the default)enabled (doesn't cost more, but provides faster boot of our worker, which is good)runpod-worker-comfy (or whatever name you gave your template)Select Network Volume. Otherwise leave the defaults.Click deploy
Your endpoint will be created, you can click on it to see the dashboard
| Model | Image | Minimum VRAM Required | Container Size |
|---|---|---|---|
| Stable Diffusion XL | sdxl | 8 GB | 15 GB |
| Stable Diffusion 3 Medium | sd3 | 5 GB | 20 GB |
| FLUX.1 Schnell | flux1-schnell | 24 GB | 30 GB |
| FLUX.1 dev | flux1-dev | 24 GB | 30 GB |
The following describes which fields exist when doing requests to the API. We only describe the fields that are sent via input as those are needed by the worker itself. For a full list of fields, please take a look at the official documentation.
{
"input": {
"workflow": {},
"images": [
{
"name": "example_image_name.png",
"image": "base64_encoded_string"
}
]
}
}
| Field Path | Type | Required | Description |
|---|---|---|---|
input | Object | Yes | The top-level object containing the request data. |
input.workflow | Object | Yes | Contains the ComfyUI workflow configuration. |
input.images | Array | No | An array of images. Each image will be added into the "input"-folder of ComfyUI and can then be used in the workflow by using it's name |
An array of images, where each image should have a different name.
🚨 The request body for a RunPod endpoint is 10 MB for /run and 20 MB for /runsync, so make sure that your input images are not super huge as this will be blocked by RunPod otherwise, see the official documentation
| Field Name | Type | Required | Description |
|---|---|---|---|
name | String | Yes | The name of the image. Please use the same name in your workflow to reference the image. |
image | String | Yes | A base64 encoded string of the image. |
Generate an API Key:
API Keys and then on the API Key button.Use the API Key:
<api_key> with your key.Use your Endpoint:
<endpoint_id> with the ID of the endpoint. (You can find the endpoint ID by clicking on your endpoint; it is written underneath the name of the endpoint at the top and also part of the URLs shown at the bottom of the first box.)
curl -H "Authorization: Bearer <api_key>" https://api.runpod.ai/v2/<endpoint_id>/health
You can either create a new job async by using /run or a sync by using /runsync. The example here is using a sync job and waits until the response is delivered.
The API expects a JSON in this form, where workflow is the workflow from ComfyUI, exported as JSON and images is optional.
Please also take a look at the test_input.json to see how the API input should look like.
curl -X POST -H "Authorization: Bearer <api_key>" -H "Content-Type: application/json" -d '{"input":{"workflow":{"3":{"inputs":{"seed":1337,"steps":20,"cfg":8,"sampler_name":"euler","scheduler":"normal","denoise":1,"model":["4",0],"positive":["6",0],"negative":["7",0],"latent_image":["5",0]},"class_type":"KSampler"},"4":{"inputs":{"ckpt_name":"sd_xl_base_1.0.safetensors"},"class_type":"CheckpointLoaderSimple"},"5":{"inputs":{"width":512,"height":512,"batch_size":1},"class_type":"EmptyLatentImage"},"6":{"inputs":{"text":"beautiful scenery nature glass bottle landscape, purple galaxy bottle,","clip":["4",1]},"class_type":"CLIPTextEncode"},"7":{"inputs":{"text":"text, watermark","clip":["4",1]},"class_type":"CLIPTextEncode"},"8":{"inputs":{"samples":["3",0],"vae":["4",2]},"class_type":"VAEDecode"},"9":{"inputs":{"filename_prefix":"ComfyUI","images":["8",0]},"class_type":"SaveImage"}}}}' https://api.runpod.ai/v2/<endpoint_id>/runsync
Example response with AWS S3 bucket configuration
{
"delayTime": 2188,
"executionTime": 2297,
"id": "sync-c0cd1eb2-068f-4ecf-a99a-55770fc77391-e1",
"output": {
"message": "https://bucket.s3.region.amazonaws.com/10-23/sync-c0cd1eb2-068f-4ecf-a99a-55770fc77391-e1/c67ad621.png",
"status": "success"
},
"status": "COMPLETED"
}
Example response as base64-encoded image
{
"delayTime": 2188,
"executionTime": 2297,
"id": "sync-c0cd1eb2-068f-4ecf-a99a-55770fc77391-e1",
"output": { "message": "base64encodedimage", "status": "success" },
"status": "COMPLETED"
}
Settings (gear icon in the top right of the menu)Enable Dev mode Options: enableSettingsSave (API Format) button, which will download a file named workflow_api.jsonYou can now take the content of this file and put it into your workflow when interacting with the API.
Using a Network Volume allows you to store and access custom models:
Create a Network Volume:
Populate the Volume:
Manage > Storage, click Deploy under the volume, and deploy any GPU or CPU instance.Manage > Pods. Under the new pod, click Connect to open a shell (either via Jupyter notebook or SSH).cd /workspace
for i in checkpoints clip clip_vision configs controlnet embeddings loras upscale_models vae; do mkdir -p models/$i; done
wget -O models/checkpoints/sd_xl_turbo_1.0_fp16.safetensors https://huggingface.co/stabilityai/sdxl-turbo/resolve/main/sd_xl_turbo_1.0_fp16.safetensors
Delete the Temporary GPU Instance:
Configure Your Endpoint:
Advanced > Select Network Volume, select your Network Volume.Note: The folders in the Network Volume are automatically available to ComfyUI when the network volume is configured and attached.
If you prefer to include your models and custom nodes directly in the Docker image, follow these steps:
To include additional models in your Docker image, edit the Dockerfile and add the download commands:
RUN wget -O models/checkpoints/sd_xl_base_1.0.safetensors https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0.safetensors
To include custom nodes in your Docker image:
Export a snapshot from ComfyUI Manager that includes all your desired custom nodes
*_snapshot.json from your ComfyUI: ComfyUI/custom_nodes/ComfyUI-Manager/snapshotsSave the snapshot file in the root directory of the project
The snapshot will be automatically restored during the Docker build process, see Building the Image
Note
- Some custom nodes may download additional models during installation, which can significantly increase the image size
- Having many custom nodes may increase ComfyUI's initialization time
Build your customized Docker image locally:
# Build the base image
docker build -t <your_dockerhub_username>/runpod-worker-comfy:dev-base --target base --platform linux/amd64 .
# Build the SDXL image
docker build --build-arg MODEL_TYPE=sdxl -t <your_dockerhub_username>/runpod-worker-comfy:dev-sdxl --platform linux/amd64 .
# Build the SD3 image
docker build --build-arg MODEL_TYPE=sd3 --build-arg HUGGINGFACE_ACCESS_TOKEN=<your-huggingface-token> -t <your_dockerhub_username>/runpod-worker-comfy:dev-sd3 --platform linux/amd64 .
Note
Ensure to specify `--platform linux/amd64` to avoid errors on RunPod, see [issue #13](https://github.com/blib-la/runpod-worker-comfy/issues/13)
Both tests will use the data from test_input.json, so make your changes in there to test this properly.
python -m venv venv
.\venv\Scripts\activate
source ./venv/bin/activate
pip install -r requirements.txt
wsl -d Ubuntu
sudo apt update
sudo apt-get install docker-compose
nvidia runtime.docker group to use Docker without sudo:
sudo usermod -aG docker $USER
Once these steps are completed, you can either run the Docker image directly on Windows using Docker Desktop or switch to Ubuntu in the terminal to run the Docker image via WSL
wsl -d Ubuntu
Note
- Windows: Accessing the API or ComfyUI might not work when you run the Docker Image via WSL, so it is recommended to run the Docker Image directly on Windows using Docker Desktop
python -m unittest discoverpython -m unittest tests.test_rp_handler.TestRunpodWorkerComfy.test_bucket_endpoint_not_configuredYou can also start the handler itself to have the local server running: python src/rp_handler.py
To get this to work you will also need to start "ComfyUI", otherwise the handler will not work.
For enhanced local development, you can start an API server that simulates the RunPod worker environment. This feature is particularly useful for debugging and testing your integrations locally.
Set the SERVE_API_LOCALLY environment variable to true to activate the local API server when running your Docker container. This is already the default value in the docker-compose.yml, so you can get it running by executing:
docker-compose up
Note
- This will only work on computer with an NVIDIA GPU for now, as it requires CUDA. Please open an issue if you want to use it on a CPU / Mac
Note
- Windows: Accessing the API or ComfyUI might not work when you run the Docker Image via WSL, so it is recommended to run the Docker Image directly on Windows using Docker Desktop
Note
- Windows: Accessing the API or ComfyUI might not work when you run the Docker Image via WSL, so it is recommended to run the Docker Image directly on Windows using Docker Desktop
The repo contains two workflows that publish the image to Docker hub using GitHub Actions:
dev tag on every push to the main branchlatest and the release tag. It will only be triggered when you create a release on GitHubIf you want to use this, you should add these secrets to your repository:
| Configuration Variable | Description | Example Value |
|---|---|---|
DOCKERHUB_USERNAME | Your Docker Hub username. | your-username |
DOCKERHUB_TOKEN | Your Docker Hub token for authentication. | your-token |
| `HUGGING |
Content type
Image
Digest
sha256:8252988d5…
Size
30 GB
Last updated
11 months ago
Requires Docker Desktop 4.37.1 or later.