From d679b5f75fe78c93593942bf301bc08aa408cd12 Mon Sep 17 00:00:00 2001 From: SamSamhuns Date: Fri, 10 Mar 2023 18:49:47 +0400 Subject: [PATCH 1/5] Add docker build & run --- .dockerignore | 97 +++++++++++++++++++++++++++++++++++++++++ Dockerfile | 46 +++++++++++++++++++ README.md | 11 +++++ scripts/build_docker.sh | 3 ++ scripts/run_docker.sh | 40 +++++++++++++++++ 5 files changed, 197 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 scripts/build_docker.sh create mode 100644 scripts/run_docker.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000..4c428e0a32 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,97 @@ +# Git +.git +.gitignore + +# CI +.codeclimate.yml +.travis.yml +.taskcluster.yml + +# Docker +docker-compose.yml +.docker + +# Byte-compiled / optimized / DLL files +__pycache__/ +*/__pycache__/ +*/*/__pycache__/ +*/*/*/__pycache__/ +*.py[cod] +*/*.py[cod] +*/*/*.py[cod] +*/*/*/*.py[cod] + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.cache +nosetests.xml +coverage.xml + +# Translations +*.mo +*.pot + +# Django stuff: +*.log + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Virtual environment +.env/ +.venv/ +venv/ +venv* + +# PyCharm +.idea + +# Python mode for VIM +.ropeproject +*/.ropeproject +*/*/.ropeproject +*/*/*/.ropeproject + +# Vim swap files +*.swp +*/*.swp +*/*/*.swp +*/*/*/*.swp + +# ignore model directory, instead mount it with docker run +models \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000..152bebd527 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,46 @@ +FROM pytorch/pytorch:1.12.1-cuda11.3-cudnn8-runtime +LABEL maintainer="fname.lname@domain.com" + +# install opencv åreqs +RUN apt-get update \ + && apt-get install libsm6 libxext6 libgl1-mesa-glx libglib2.0-0 libxrender1 wget --no-install-recommends -y + +# remove cache +RUN apt-get autoremove -y \ + && rm -rf /var/lib/apt/lists/* + +# set username inside docker +ARG uname=user1 + +# add user uname as a member of the sudoers group +RUN useradd -rm --home-dir "/home/$uname" --shell /bin/bash -g root -G sudo -u 1001 "$uname" +# activate user +USER "$uname" +WORKDIR "/home/$uname" + +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 +ENV PATH="/home/$uname/miniconda3/bin:${PATH}" +ARG PATH="/home/$uname/miniconda3/bin:${PATH}" + +# download and install miniconda +RUN wget \ + https://repo.anaconda.com/miniconda/Miniconda3-py38_23.1.0-1-Linux-x86_64.sh \ + && mkdir "/home/$uname/.conda" \ + && bash Miniconda3-py38_23.1.0-1-Linux-x86_64.sh -b \ + && rm -f Miniconda3-py38_23.1.0-1-Linux-x86_64.sh + +# copy env yaml file +COPY environment.yaml "/home/$uname" + +# create conda env +RUN conda init bash \ + && conda env create -f environment.yaml + +# add conda env activation to bashrc +RUN echo "conda activate control" >> ~/.bashrc + +# copy all files from current dir except those in .dockerignore +COPY . "/home/$uname" + +CMD ["/bin/bash"] \ No newline at end of file diff --git a/README.md b/README.md index cbcce6e051..3ddcffd053 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,17 @@ We provide 9 Gradio apps with these models. All test images can be found at the folder "test_imgs". +## Run with Docker + +Alternatively, build and run docker image after models have been downloaded to `ControlNet/models`. + + bash scripts/build_docker.sh # build docker image to set up environment + bash scripts/run_docker.sh -p 8000 # run docker with an external port 0.0.0.0:8000 exposed + +Example run gradio_scribble2image.py + + python gradio_scribble2image.py # gradio app will be hosted at localhost:8000 + ## ControlNet with Canny Edge Stable Diffusion 1.5 + ControlNet (using simple Canny edge detection) diff --git a/scripts/build_docker.sh b/scripts/build_docker.sh new file mode 100644 index 0000000000..a1724fb9fe --- /dev/null +++ b/scripts/build_docker.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +docker build -t controlnet:latest . diff --git a/scripts/run_docker.sh b/scripts/run_docker.sh new file mode 100644 index 0000000000..78f04e97df --- /dev/null +++ b/scripts/run_docker.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +def_cont_name=controlnet_cont + +helpFunction() +{ + echo "" + echo "Usage: $0 -p port" + echo -e "\t-p http_port" + exit 1 # Exit script after printing help +} + +while getopts "p:" opt +do + case "$opt" in + p ) port="$OPTARG" ;; + ? ) helpFunction ;; # Print helpFunction in case parameter is non-existent + esac +done + +# Print helpFunction in case parameters are empty +if [ -z "$port" ] +then + echo "Some or all of the parameters are empty"; + helpFunction +fi + +echo "Stopping and removing docker container '$def_cont' if it is running on port $port" +echo "Ignore No such container Error messages" +docker stop "$def_cont_name" || true +docker rm "$def_cont_name" || true + +# 7860 is gradio's default port +docker run \ + -ti --rm \ + -p "0.0.0.0:$port:7860" \ + -v "$PWD/models:/home/user1/models" \ + --gpus "device=0" \ + --name "$def_cont_name" \ + controlnet:latest From a61740a3d1d144ae1a1e07f165dee94f50666a39 Mon Sep 17 00:00:00 2001 From: SamSamhuns Date: Sun, 12 Mar 2023 11:06:38 +0400 Subject: [PATCH 2/5] Use mamba solver for faster env resoln --- Dockerfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Dockerfile b/Dockerfile index 152bebd527..aaa45f14bc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -35,6 +35,8 @@ COPY environment.yaml "/home/$uname" # create conda env RUN conda init bash \ + && conda install -n base conda-libmamba-solver \ + && conda config --set solver libmamba \ && conda env create -f environment.yaml # add conda env activation to bashrc From 797ebc41d07d324d2d58eaf2b5adbd7092175af3 Mon Sep 17 00:00:00 2001 From: SamSamhuns Date: Sat, 18 Mar 2023 14:52:41 +0400 Subject: [PATCH 3/5] Provide uid from host --- Dockerfile | 28 ++++++++++++++++++---------- scripts/build_docker.sh | 2 +- scripts/run_docker.sh | 2 +- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index aaa45f14bc..2feeb1c09a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,28 +10,29 @@ RUN apt-get autoremove -y \ && rm -rf /var/lib/apt/lists/* # set username inside docker -ARG uname=user1 +ARG UNAME=user1 +ARG UID=1000 -# add user uname as a member of the sudoers group -RUN useradd -rm --home-dir "/home/$uname" --shell /bin/bash -g root -G sudo -u 1001 "$uname" +# add user UNAME as a member of the sudoers group +RUN useradd -rm --home-dir "/home/$UNAME" --shell /bin/bash -g root -G sudo -u "$UID" "$UNAME" # activate user -USER "$uname" -WORKDIR "/home/$uname" +USER "$UNAME" +WORKDIR "/home/$UNAME" ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 -ENV PATH="/home/$uname/miniconda3/bin:${PATH}" -ARG PATH="/home/$uname/miniconda3/bin:${PATH}" +ENV PATH="/home/$UNAME/miniconda3/bin:${PATH}" +ARG PATH="/home/$UNAME/miniconda3/bin:${PATH}" # download and install miniconda RUN wget \ https://repo.anaconda.com/miniconda/Miniconda3-py38_23.1.0-1-Linux-x86_64.sh \ - && mkdir "/home/$uname/.conda" \ + && mkdir "/home/$UNAME/.conda" \ && bash Miniconda3-py38_23.1.0-1-Linux-x86_64.sh -b \ && rm -f Miniconda3-py38_23.1.0-1-Linux-x86_64.sh # copy env yaml file -COPY environment.yaml "/home/$uname" +COPY environment.yaml "/home/$UNAME" # create conda env RUN conda init bash \ @@ -43,6 +44,13 @@ RUN conda init bash \ RUN echo "conda activate control" >> ~/.bashrc # copy all files from current dir except those in .dockerignore -COPY . "/home/$uname" +COPY . "/home/$UNAME/ControlNet" +# change file ownership to docker user +USER root +RUN chown -R "$UNAME" "/home/$UNAME/ControlNet" +USER "$UNAME" + +# switch to ControlNet dir +WORKDIR "/home/$UNAME/ControlNet" CMD ["/bin/bash"] \ No newline at end of file diff --git a/scripts/build_docker.sh b/scripts/build_docker.sh index a1724fb9fe..c45c106827 100644 --- a/scripts/build_docker.sh +++ b/scripts/build_docker.sh @@ -1,3 +1,3 @@ #!/bin/bash -docker build -t controlnet:latest . +docker build -t controlnet:latest --build-arg UID=$(id -u) . diff --git a/scripts/run_docker.sh b/scripts/run_docker.sh index 78f04e97df..2066b7ae13 100644 --- a/scripts/run_docker.sh +++ b/scripts/run_docker.sh @@ -34,7 +34,7 @@ docker rm "$def_cont_name" || true docker run \ -ti --rm \ -p "0.0.0.0:$port:7860" \ - -v "$PWD/models:/home/user1/models" \ + -v "$PWD/models:/home/user1/ControlNet/models" \ --gpus "device=0" \ --name "$def_cont_name" \ controlnet:latest From 1ee32c36d174db0f11f8145e34f4d3aafd77dc0d Mon Sep 17 00:00:00 2001 From: Sam_S Date: Tue, 11 Apr 2023 13:20:30 +0400 Subject: [PATCH 4/5] Update existing cont exist chk --- scripts/run_docker.sh | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/scripts/run_docker.sh b/scripts/run_docker.sh index 2066b7ae13..a5e8e86596 100644 --- a/scripts/run_docker.sh +++ b/scripts/run_docker.sh @@ -25,10 +25,13 @@ then helpFunction fi -echo "Stopping and removing docker container '$def_cont' if it is running on port $port" -echo "Ignore No such container Error messages" -docker stop "$def_cont_name" || true -docker rm "$def_cont_name" || true +# Check if the container is running +if [ "$(docker ps -q -f name=$def_cont_name)" ]; then + # Stop the container + echo "Stopping docker container '$def_cont_name'" + docker stop "$def_cont_name" + echo "Stopped container '$def_cont_name'" +fi # 7860 is gradio's default port docker run \ From 2f7ae48871d8f7f8d3814ce35f52947132277a0b Mon Sep 17 00:00:00 2001 From: Sam_S Date: Mon, 1 May 2023 10:48:53 +0400 Subject: [PATCH 5/5] Add docker rm call --- scripts/run_docker.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/run_docker.sh b/scripts/run_docker.sh index a5e8e86596..36249a3bdd 100644 --- a/scripts/run_docker.sh +++ b/scripts/run_docker.sh @@ -30,7 +30,8 @@ if [ "$(docker ps -q -f name=$def_cont_name)" ]; then # Stop the container echo "Stopping docker container '$def_cont_name'" docker stop "$def_cont_name" - echo "Stopped container '$def_cont_name'" + docker rm "$def_cont_name" + echo "Stopped & removed container '$def_cont_name'" fi # 7860 is gradio's default port