Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docker build & run #256

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -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
48 changes: 48 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
FROM pytorch/pytorch:1.12.1-cuda11.3-cudnn8-runtime
LABEL maintainer="[email protected]"

# 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 install -n base conda-libmamba-solver \
&& conda config --set solver libmamba \
&& 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"]
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions scripts/build_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

docker build -t controlnet:latest .
40 changes: 40 additions & 0 deletions scripts/run_docker.sh
Original file line number Diff line number Diff line change
@@ -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