#Docker & ComfyUI
129 messages · Page 1 of 1 (latest)
@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
So where does it is stored ?
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
It works !
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
I receive this
Even if it tells me there is a folder called comfyui !
ye ahahah my bad
retry the command
and then type ls
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
Yeah ! I'm in !
Yeah !
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
Build & Run it again 🙂 You should see ComfyUI logs. But you should see a GPU problem right ?
I get an error, it tells me that I dont have a NVIDIA driver !
Yeah exactly
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?)
It does !!
it does work ? ahaha you can open the URL ? you should not
Ah that's because you're on linux
No I'm on Windows but maybe it is because Comfy was already installed !
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/
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
@lethal sorrel Feel free to also post in #1194691135436763276. Thank you! 
It is already really practical since I can now see if I added my checkpoint properly !
Aaah Ill move the thread there @tepid ingot I didnt know of it
Exactly, so now we will create a docker-compose to better handle the GPU etc
Ok !
# 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"]
Does it changes something if I later use this image in serverless runpod ?
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 ?
Yes !
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
Yes! It shows me the two docker we created (here and in the other thread)
- the ones i used for postgres
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
Oh lol I see the push created a lot of dockers in my Docker Desktop ^^
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
Yeah I see them !
we want to go into it just like before
docker exec -it <PARTIAL_ID> bash
and then just
cp -r /comfyui/* /comfyui2
now you should see that your storage/comfyui is full of files ahha
Yeah !
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)
Why that ?
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
Should I run it with that ?
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
no, with docker compose again
docker compose up to see the logs
docker compose up -d to run it in the background
So after typing docker compose up -d I should access the ComfyUI interface again ?
Yep
and comfy will run in the background (you should see the docker running on docker desktop app)
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
Okay ! Make sense !
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