#Docker & ComfyUI

129 messages · Page 1 of 1 (latest)

lethal sorrel
#

Thread tutorial to understand the power of Docker & add comfyui to it.
Might include how to deploy to runpod (pods & serverless) later.

soft bayBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

lethal sorrel
#

@lofty wadi

#

So, basically,
a docker is a "mini" computer running in your operating system, it's totally seperated from your current computer, only share components (CPU, GPU, etc). This helps creating seperated environments to work on. You can then export a docker and install it anywhere you want and open it to work on it. Everything will be installed: your files, softwares etc

#

We will learn to create a Docker with ComfyUI & Open comfyui from the docker

#

First create a new folder we will work in

#

then create a Dockerfile

FROM runpod/worker-comfyui:5.4.1-base
ENTRYPOINT ["/bin/bash", "-c", "ls"]
#

This will pull a pre-existing template with comfyui already installed in it.
The entrypoint is the command that will be executed whenever you start the docker

#

in this case it's /bin/bash ls so when you run your docker it should print all the files within

#

@lofty wadi can you build this and confirm ? I'm building mine but it takes ages it have a slow download speed hahahaha

#

To build it use
docker build -t comfyui .
To run it use
docker run comfyui

lethal sorrel
#

it's stored on your computer.
Each line of a dockerfile is a "layer" and each layers are store on your computer for caching. That way when you modify a line it doesn't re-download the whole runpod/worker-comfyui since it has been cached

lofty wadi
lethal sorrel
#

yep yep, I fixed the dockerfile can you try again

#

ok perfect

lofty wadi
#

It works !

lethal sorrel
#

So here we created a docker & executed a command within it ! you now have a computer within your computer. Cool no ?

#

Now we can even navigate within the docker

#

docker run --entrypoint bash -it comfyui

lofty wadi
#

Even if it tells me there is a folder called comfyui !

lethal sorrel
#

ye ahahah my bad

lethal sorrel
#

and then type ls

lofty wadi
#

We are trying to look what is into comfy folder ?

lethal sorrel
#

docker run --entrypoint bash -it comfyui

#

With this command we are overwritting the entrypoint to execute bash and not ls so we can navigate in the docker ourselves

lofty wadi
#

Yeah ! I'm in !

lethal sorrel
#

so then you can do : cd comfyui andls

#

you should see a main.py right ?

lofty wadi
#

Yeah !

lethal sorrel
#

nice 🙂
So now update your Dockerfile like this

FROM runpod/worker-comfyui:5.4.1-base

ENTRYPOINT ["python","/comfyui/main.py"]

This will start the docker by starting the main.py from comfyui

lethal sorrel
lofty wadi
#

I get an error, it tells me that I dont have a NVIDIA driver !

lethal sorrel
#

Ok, good, we can make a quick test fix by adding --cpu

FROM runpod/worker-comfyui:5.4.1-base
ENTRYPOINT ["python","/comfyui/main.py", "--cpu"]
#

This will run comfy without the cpu, ofc, build & run your docker again

#

You will now see that comfy is running on port 8188. Try to open the URL 😉 (does it work?)

lofty wadi
#

It does !!

lethal sorrel
#

it does work ? ahaha you can open the URL ? you should not

lofty wadi
#

Yeah I can !

#

It displays me ComfyUI

lethal sorrel
#

Ah that's because you're on linux

lofty wadi
#

No I'm on Windows but maybe it is because Comfy was already installed !

lethal sorrel
#

Oh yeah if you have it open on your computer then it runs the one of your computer 😆😆

#

stop it

#

It doesn't work because as said before, it's a completely seperated computer running. We will need to map ports from your Docker to your host machine for it to work. So we will bind the port 8188 of your docker (where comfy runs) on the port 3000 of your host (to better differentiate)

FROM runpod/worker-comfyui:5.4.1-base
ENTRYPOINT ["python","/comfyui/main.py", "--listen", "0.0.0.0", "--cpu"]

docker build -t comfyui .
docker run -p 3000:8188 -it comfyui
So now you should be able to open it via this url http://127.0.0.1:3000/

lofty wadi
#

Yes I can see it ! And now it does not have my checkpoints anymore ^^

#

Nice !

lethal sorrel
#

Right that's because it's the comfy from within your docker, now you have a completely seperated comfy and environment to work with

#

But you're not done yet. As you can see, it doesn't use your GPU, since we added the --cpu param

#

we need to fix this

tepid ingot
#

@lethal sorrel Feel free to also post in #1194691135436763276. Thank you! runpodheart

lofty wadi
#

It is already really practical since I can now see if I added my checkpoint properly !

lethal sorrel
#

Aaah Ill move the thread there @tepid ingot I didnt know of it

lethal sorrel
lofty wadi
#

Ok !

lethal sorrel
#
# docker-compose.yml
services:
  comfyui:
    image: comfyui
    build:
      context: .
      dockerfile: Dockerfile
    container_name: comfyui
    volumes:
      # Map a local directory to /comfyui inside the container
      - ./storage/comfyui:/comfyui2
    environment:
      # Pass any environment variables you need
      NVIDIA_VISIBLE_DEVICES: all
      NVIDIA_DRIVER_CAPABILITIES: compute,utility
    runtime: nvidia
    ports:
      - 3000:8188
#

now you can use the new commands :
docker compose build & docker compose up

#

we also want to update the Dockerfile to use the GPU

FROM runpod/worker-comfyui:5.4.1-base
ENTRYPOINT ["python","/comfyui/main.py", "--listen", "0.0.0.0"]
lofty wadi
lethal sorrel
#

What you want now is to work with the Docker Compose, and then once you're done add all the commands you used inside the Dockerfile.
The docker compose makes it WAAAAY easier to work with and track changes

#

When you push your Docker you will only push the Dockerfile, not the docker-compose

#

but it's important to use it for the next steps, you'll understand

#

you can see that a /storage/comfyui2 was created when doing "docker compose up" right ?

lethal sorrel
#

Exit with Ctrl+C it will stop the docker.
Now run it with docker compose up -d

#

It should return a local id right ?

#

ah no it does not ahha, but you can track your docker with

#

docker ps

lofty wadi
#
  • the ones i used for postgres
lethal sorrel
#

You can stop them
docker stop <PARTIAL_ID>

For example if your Id is 5bf5d3434d45 you can just do docker stop 5b

#

so close the comfyui dockers

#

and docker compose up -d again, so we start it fresh

lofty wadi
#

Oh lol I see the push created a lot of dockers in my Docker Desktop ^^

lethal sorrel
#

Ahahah yeah, you will be able to delete them and rebuild your machine to start it fresh dont worry. it's actually layers change (so a few Kb)

#

so you have a docker detached up ?
you can run docker ps to see the id

lofty wadi
#

Yeah I see them !

lethal sorrel
#

we want to go into it just like before
docker exec -it <PARTIAL_ID> bash

lofty wadi
#

I'm in

#

That one is called "comfyui"

lethal sorrel
#

and then just

cp -r /comfyui/* /comfyui2
#

now you should see that your storage/comfyui is full of files ahha

lofty wadi
#

Yeah !

lethal sorrel
#

stop this docker once again

#

And update the docker-compose once again

services:
  comfyui:
    image: comfyui
    build:
      context: .
      dockerfile: Dockerfile
    container_name: comfyui
    volumes:
      # Map a local directory to /comfyui inside the container
      - ./storage/comfyui:/comfyui
    environment:
      # Pass any environment variables you need
      NVIDIA_VISIBLE_DEVICES: all
      NVIDIA_DRIVER_CAPABILITIES: compute,utility
    runtime: nvidia
    ports:
      - 3000:8188
#

I just changed

   - ./storage/comfyui:/comfyui2
to
   - ./storage/comfyui:/comfyui
#

now open your docker again, (either in headless mode or not, to see the logs)

lethal sorrel
#

So now your comfyui folder from within your docker is linked to /storage/comfyui

#

if you add a checkpoint to your /storage/comfyui folder, it will add it to your docker automatically

#

no need to rebuild

#

just refresh your browser and you'll see the checkpoint

lofty wadi
#

Should I run it with that ?

lethal sorrel
#

that's good to iterate. Also, if you install custom nodes and restart your docker, since the files are stored on your computer now, you won't lose your custom_nodes

lethal sorrel
#

docker compose up to see the logs
docker compose up -d to run it in the background

lofty wadi
#

So after typing docker compose up -d I should access the ComfyUI interface again ?

lethal sorrel
#

Yep

#

and comfy will run in the background (you should see the docker running on docker desktop app)

lofty wadi
#

I have this now

#

I think I made a mistake somewhere

lethal sorrel
#

Just delete them

#

and do docker compose build

#

and docker compose up

lofty wadi
lethal sorrel
#

perfect

#

base-comfy is your folder

lofty wadi
#

Yes

#

For some reason I cant run them

#

Or it

lethal sorrel
#

use the commands

#

So what we learnt ?

  • Creating a Docker
  • Linking ports of a docker to your main computer
  • Linking a folder of your computer to your docker

Now you can use comfyui with docker compose up. It will be in a totally seperated "sub-computer" so you make sure that everything works when you copy it.

Now how to add checkpoints ? Well, you can just drag them inside the /storage/comfyui/models/checkpoints folder.
But when I export the Docker it won't be there right ? Yep exactly, this is just a folder linked to your computer. If you want to export the checkpoints in your "Docker" you need to add them in the Dockerfile Anything you want to be there when copying your Docker should be set inside your Dockerfile.

So then when you push and pull your docker everything will be there 🙂

A simple way to make everything works is to do this:

FROM runpod/worker-comfyui:5.4.1-base

COPY ./storage/comfyui /comfyui

ENTRYPOINT ["python","/comfyui/main.py", "--listen", "0.0.0.0"]
#

This will copy your entire /storage/comfyui inside the docker. so when you compile it, it will be an EXACT copy of your running docker 🙂 easy to push, easy to pull in runpod

lofty wadi
#

Okay ! Make sense !

lethal sorrel
#

I never used it this way tho, im using their github integration to push Dockerfiles. So I never build it on my computer to then push it on docker hub

#

but it should work that way ahah

#

To push my checkpoints I download them directly in the dockerfile

#
FROM runpod/worker-comfyui:5.4.1-base

# Get a model from hugging face
RUN wget -O /comfyui/models/checkpoints/sdxl_lightning_4step.safetensors https://huggingface.co/ByteDance/SDXL-Lightning/resolve/main/sdxl_lightning_4step.safetensors

ENTRYPOINT ["python","/comfyui/main.py", "--listen", "0.0.0.0"]

(that's an example)

#

Ok im gonna go, I hope this helped, if you have any questions feel free to dm me 🙂

#

Docker & ComfyUI