Skip to content

Commit dad5e7c

Browse files
Container user is now the same as the host user
It is now possible to specify the user ID and group ID of the host system user as environment variables when starting the container. During startup, the container will create a user with the same user ID and group ID, and run ComfyUI with this user. This is helpful, because the model and custom node files downloaded by the ComfyUI Manager will be owned by the host system user instead of root. The documentation in the read me was updated to reflect this change. Furthermore, while the custom nodes themselves are installed outside of the container, their requirements are installed inside of the container. This means that stopping and removing the container will remove the installed requirements. Therefore, the dependencies are now installed when the container is started again.
1 parent cb32775 commit dad5e7c

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

.cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"controlnet",
6666
"cudnn",
6767
"findall",
68+
"getent",
6869
"gligen",
6970
"hypernetworks",
7071
"lecode",

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
## v0.2.0 (December 16, 2024)
44

55
- Previously, only the model files were stored outside of the container, but the custom nodes installed by ComfyUI Manager were not. The reason was that the ComfyUI Manager itself is implemented as a custom node, which means that mounting a host system directory into the custom nodes directory would hide the ComfyUI Manager. This problem was fixed by installing the ComfyUI Manager in a separate directory and symlinking it upon container startup into the mounted directory.
6+
- It is now possible to specify the user ID and group ID of the host system user as environment variables when starting the container. During startup, the container will create a user with the same user ID and group ID, and run ComfyUI with this user. This is helpful, because the model and custom node files downloaded by the ComfyUI Manager will be owned by the host system user instead of `root`. The documentation in the read me was updated to reflect this change.
67
- Updated the image to the latest version of ComfyUI (from v0.3.4 to v0.3.7).
78
- In the previous version, the main branch of the ComfyUI Manager was installed and not a specific version. Since the main branch may contain breaking changes or bugs, the ComfyUI Manager is now installed with a specific version (v2.55.5).
89
- The ComfyUI Manager installs its dependencies when it is first launched. This means that the dependencies have to be installed every time the container is started. To avoid this, the dependencies are now installed manually during the image build process.
10+
- While the custom nodes themselves are installed outside of the container, their requirements are installed inside of the container. This means that stopping and removing the container will remove the installed requirements. Therefore, the dependencies are now installed when the container is started again.
911
- The directory structure of the ComfyUI models directory is now automatically created upon container startup if it does not exist.
1012
- The Docker image now has two more tags: one with the ComfyUI version and the second with the ComfyUI and ComfyUI Manager versions that are installed in the image. This makes it easier for users to find out which ComfyUI version they are installing before pulling the image.
1113
- A section was added to the read me, which explains how to update the local image to the latest version.

Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ FROM pytorch/pytorch:2.5.1-cuda12.4-cudnn9-runtime
44

55
# Installs Git, because ComfyUI and the ComfyUI Manager are installed by cloning their respective Git repositories
66
RUN apt update --assume-yes && \
7-
apt install --assume-yes git
7+
apt install --assume-yes \
8+
git \
9+
sudo
810

911
# Clones the ComfyUI repository and checks out the latest release
1012
RUN git clone https://github.com/comfyanonymous/ComfyUI.git /opt/comfyui && \
@@ -34,7 +36,8 @@ WORKDIR /opt/comfyui
3436
EXPOSE 8188
3537

3638
# Adds the startup script to the container; the startup script will create all necessary directories in the models and custom nodes volumes that were
37-
# mounted to the container and symlink the ComfyUI Manager to the correct directory
39+
# mounted to the container and symlink the ComfyUI Manager to the correct directory; it will also create a user with the same UID and GID as the user
40+
# that started the container, so that the files created by the container are owned by the user that started the container and not the root user
3841
ADD entrypoint.sh /entrypoint.sh
3942
ENTRYPOINT ["/bin/bash", "/entrypoint.sh"]
4043

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ docker run \
1717
--name comfyui \
1818
--detach \
1919
--restart unless-stopped \
20+
--env USER_ID="$(id -u)" \
21+
--env GROUP_ID="$(id -g)" \
2022
--volume "<path/to/models/folder>:/opt/comfyui/models:rw" \
2123
--volume "<path/to/custom/nodes/folder>:/opt/comfyui/custom_nodes:rw" \
2224
--publish 8188:8188 \
@@ -27,7 +29,7 @@ docker run \
2729

2830
Please note, that the `<path/to/models/folder>` and `<path/to/custom/nodes/folder>` must be replaced with paths to directories on the host system where the models and custom nodes will be stored, e.g., `$HOME/.comfyui/models` and `$HOME/.comfyui/custom-nodes`, which can be created like so: `mkdir -p $HOME/.comfyui/{models,custom-nodes}`.
2931

30-
The `--detach` flag causes the container to run in the background and `--restart unless-stopped` configures the Docker Engine to automatically restart the container if it stopped itself, experienced an error, or the computer was shutdown, unless you explicitly stopped the container using `docker stop`. This means that ComfyUI will be automatically started in the background when you boot your computer. The `--runtime nvidia` and `--gpus all` arguments enable ComfyUI to access the GPUs of your host system. If you do not want to expose all GPUs, you can specify the desired GPU index or ID instead.
32+
The `--detach` flag causes the container to run in the background and `--restart unless-stopped` configures the Docker Engine to automatically restart the container if it stopped itself, experienced an error, or the computer was shutdown, unless you explicitly stopped the container using `docker stop`. This means that ComfyUI will be automatically started in the background when you boot your computer. The two `--env` arguments inject the user ID and group ID of the current host user into the container. During startup, a user with the same user ID and group ID will be created, and ComfyUI will be run using this user. This ensures that files written to the volumes (e.g., models and custom nodes installed with the ComfyUI Manager) will be owned by the host system's user. Normally, the user inside the container is `root`, which means that the files that are written from the container to the host system are also owned by `root`. If you have run ComfyUI Docker without setting the environment variables, then you may have to change the owner of the files in the models and custom nodes directories: `sudo chown -r "$(id -un):$(id -gn)" <path/to/models/folder> <path/to/custom/nodes/folder>`. The `--runtime nvidia` and `--gpus all` arguments enable ComfyUI to access the GPUs of your host system. If you do not want to expose all GPUs, you can specify the desired GPU index or ID instead.
3133

3234
After the container has started, you can navigate to [localhost:8188](http://localhost:8188) to access ComfyUI.
3335

@@ -38,6 +40,9 @@ docker stop comfyui
3840
docker rm comfyui
3941
```
4042

43+
> [!WARNING]
44+
> While the custom nodes themselves are installed outside of the container, their requirements are installed inside of the container. This means that stopping and removing the container will remove the installed requirements. When the container is started again, the requirements will be automatically installed, but this may, depending on the number of custom nodes and their requirements, take some time.
45+
4146
## Updating
4247

4348
To update ComfyUI Docker to the latest version you have to first stop the running container, then pull the new version, optionally remove dangling images, and then restart the container:
@@ -53,6 +58,8 @@ docker run \
5358
--name comfyui \
5459
--detach \
5560
--restart unless-stopped \
61+
--env USER_ID="$(id -u)" \
62+
--env GROUP_ID="$(id -g)" \
5663
--volume "<path/to/models/folder>:/opt/comfyui/models:rw" \
5764
--volume "<path/to/custom/nodes/folder>:/opt/comfyui/custom_nodes:rw" \
5865
--publish 8188:8188 \
@@ -77,6 +84,8 @@ docker run \
7784
--name comfyui \
7885
--detach \
7986
--restart unless-stopped \
87+
--env USER_ID="$(id -u)" \
88+
--env GROUP_ID="$(id -g)" \
8089
--volume "<path/to/models/folder>:/opt/comfyui/models:rw" \
8190
--volume "<path/to/custom/nodes/folder>:/opt/comfyui/custom_nodes:rw" \
8291
--publish 8188:8188 \

entrypoint.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/bash
22

33
# Creates the directories for the models inside of the volume that is mounted from the host
4+
echo "Creating directories for models..."
45
MODEL_DIRECTORIES=(
56
"checkpoints"
67
"clip"
@@ -26,7 +27,45 @@ for MODEL_DIRECTORY in ${MODEL_DIRECTORIES[@]}; do
2627
done
2728

2829
# Creates the symlink for the ComfyUI Manager to the custom nodes directory, which is also mounted from the host
30+
echo "Creating symlink for ComfyUI Manager..."
2931
rm --force /opt/comfyui/custom_nodes/ComfyUI-Manager
3032
ln -s \
3133
/opt/comfyui-manager \
3234
/opt/comfyui/custom_nodes/ComfyUI-Manager
35+
36+
# The custom nodes that were installed using the ComfyUI Manager may have requirements of their own, which are not installed when the container is
37+
# started for the first time; this loops over all custom nodes and installs the requirements of each custom node
38+
echo "Installing requirements for custom nodes..."
39+
for CUSTOM_NODE_DIRECTORY in /opt/comfyui/custom_nodes/*;
40+
do
41+
if [ "$CUSTOM_NODE_DIRECTORY" != "/opt/comfyui/custom_nodes/ComfyUI-Manager" ];
42+
then
43+
if [ -f "$CUSTOM_NODE_DIRECTORY/requirements.txt" ];
44+
then
45+
CUSTOM_NODE_NAME=${CUSTOM_NODE_DIRECTORY##*/}
46+
CUSTOM_NODE_NAME=${CUSTOM_NODE_NAME//[-_]/ }
47+
echo "Installing requirements for $CUSTOM_NODE_NAME..."
48+
pip install --requirement "$CUSTOM_NODE_DIRECTORY/requirements.txt"
49+
fi
50+
fi
51+
done
52+
53+
# Under normal circumstances, the container would be run as the root user, which is not ideal, because the files that are created by the container in
54+
# the volumes mounted from the host, i.e., custom nodes and models downloaded by the ComfyUI Manager, are owned by the root user; the user can specify
55+
# the user ID and group ID of the host user as environment variables when starting the container; if these environment variables are set, a non-root
56+
# user with the specified user ID and group ID is created, and the container is run as this user
57+
if [ -z "$USER_ID" ] || [ -z "$GROUP_ID" ];
58+
then
59+
echo "Running container as $USER..."
60+
exec "$@"
61+
else
62+
echo "Creating non-root user..."
63+
getent group $GROUP_ID > /dev/null 2>&1 || groupadd --gid $GROUP_ID comfyui-user
64+
id -u $USER_ID > /dev/null 2>&1 || useradd --uid $USER_ID --gid $GROUP_ID --create-home comfyui-user
65+
chown --recursive $USER_ID:$GROUP_ID /opt/comfyui
66+
chown --recursive $USER_ID:$GROUP_ID /opt/comfyui-manager
67+
export PATH=$PATH:/home/comfyui-user/.local/bin
68+
69+
echo "Running container as $USER..."
70+
sudo --set-home --preserve-env=PATH --user \#$USER_ID "$@"
71+
fi

0 commit comments

Comments
 (0)