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

AUTOMATIC1111: Enable running as non root user. #546

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
61 changes: 50 additions & 11 deletions services/AUTOMATIC1111/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
FROM alpine/git:2.36.2 as download

COPY clone.sh /clone.sh


Expand All @@ -19,7 +18,6 @@ FROM alpine:3.17 as xformers
RUN apk add --no-cache aria2
RUN aria2c -x 5 --dir / --out wheel.whl 'https://github.com/AbdBarho/stable-diffusion-webui-docker/releases/download/6.0.0/xformers-0.0.21.dev544-cp310-cp310-manylinux2014_x86_64-pytorch201.whl'


FROM python:3.10.9-slim

ENV DEBIAN_FRONTEND=noninteractive PIP_PREFER_BINARY=1
Expand All @@ -29,8 +27,46 @@ RUN --mount=type=cache,target=/var/cache/apt \
# we need those
apt-get install -y fonts-dejavu-core rsync git jq moreutils aria2 \
# extensions needs those
ffmpeg libglfw3-dev libgles2-mesa-dev pkg-config libcairo2 libcairo2-dev build-essential

ffmpeg libglfw3-dev libgles2-mesa-dev pkg-config libcairo2 libcairo2-dev build-essential \
# TODO: remove if fixed in A1111 (unlikely)
libgoogle-perftools-dev && apt-get clean

ARG PUID=0
ARG PGID=0
ARG USER_HOME=/root
# set build args as container environment variables for entrypoint reference
ENV PUID=$PUID
ENV PGID=$PGID
ENV USER_HOME=$USER_HOME

# if user home does not exist, create it
RUN mkdir -p "$USER_HOME"

# home already exists, chown it
RUN chown -R "${PUID}:${PGID}" "$USER_HOME"

# Only groupadd if we're non root
RUN if [ "$PGID" -ne "0" ]; then \
echo non root group detected; \
groupadd \
--gid "$PGID" \
stablediffusion ;\
else \
echo "root group detected" ; \
fi

# Only useradd if we're non root
RUN if [ "$PUID" -ne "0" ]; then \
echo non root user detected; \
useradd \
--gid="$PGID" \
--no-user-group \
-M \
--home "$USER_HOME" \
stablediffusion ; \
else \
echo "root group detected" ; \
fi

RUN --mount=type=cache,target=/cache --mount=type=cache,target=/root/.cache/pip \
aria2c -x 5 --dir /cache --out torch-2.0.1-cp310-cp310-linux_x86_64.whl -c \
Expand All @@ -48,10 +84,17 @@ RUN --mount=type=cache,target=/root/.cache/pip \
--mount=type=bind,from=xformers,source=/wheel.whl,target=/xformers-0.0.21.dev544-cp310-cp310-manylinux2014_x86_64.whl \
pip install /xformers-0.0.21.dev544-cp310-cp310-manylinux2014_x86_64.whl

ENV ROOT=/stable-diffusion-webui
# one of the ugliest hacks I ever wrote
RUN sed -i 's/in_app_dir = .*/in_app_dir = True/g' /usr/local/lib/python3.10/site-packages/gradio/routes.py

RUN chown -R "$PUID:$PGID" /stable-diffusion-webui

# drop permissions (if build targets non root)
USER $PUID:$PGID

ENV ROOT=/stable-diffusion-webui

COPY --from=download /repositories/ ${ROOT}/repositories/
COPY --from=download --chown=${PUID}:${PGID} /repositories/ ${ROOT}/repositories/
RUN mkdir ${ROOT}/interrogate && cp ${ROOT}/repositories/clip-interrogator/clip_interrogator/data/* ${ROOT}/interrogate
RUN --mount=type=cache,target=/root/.cache/pip \
pip install -r ${ROOT}/repositories/CodeFormer/requirements.txt
Expand All @@ -66,8 +109,6 @@ RUN --mount=type=cache,target=/root/.cache/pip \
# Note: don't update the sha of previous versions because the install will take forever
# instead, update the repo state in a later step

# TODO: either remove if fixed in A1111 (unlikely) or move to the top with other apt stuff
RUN apt-get -y install libgoogle-perftools-dev && apt-get clean
ENV LD_PRELOAD=libtcmalloc.so

ARG SHA=5ef669de080814067961f28357256e8fe27544f4
Expand All @@ -77,13 +118,11 @@ RUN --mount=type=cache,target=/root/.cache/pip \
git reset --hard ${SHA} && \
pip install -r requirements_versions.txt

COPY . /docker
COPY --chown=$PUID:$PGID . /docker

RUN \
python3 /docker/info.py ${ROOT}/modules/ui.py && \
mv ${ROOT}/style.css ${ROOT}/user.css && \
# one of the ugliest hacks I ever wrote \
sed -i 's/in_app_dir = .*/in_app_dir = True/g' /usr/local/lib/python3.10/site-packages/gradio/routes.py && \
git config --global --add safe.directory '*'

WORKDIR ${ROOT}
Expand Down
11 changes: 6 additions & 5 deletions services/AUTOMATIC1111/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash

set -Eeuo pipefail
RSYNC_FLAGS="${RSYNC_FLAGS:--a}"

# TODO: move all mkdir -p ?
mkdir -p /data/config/auto/scripts/
Expand All @@ -22,12 +23,12 @@ fi
# copy models from original models folder
mkdir -p /data/models/VAE-approx/ /data/models/karlo/

rsync -a --info=NAME ${ROOT}/models/VAE-approx/ /data/models/VAE-approx/
rsync -a --info=NAME ${ROOT}/models/karlo/ /data/models/karlo/
rsync "$RSYNC_FLAGS" --info=NAME ${ROOT}/models/VAE-approx/ /data/models/VAE-approx/
rsync "$RSYNC_FLAGS" --info=NAME ${ROOT}/models/karlo/ /data/models/karlo/

declare -A MOUNTS

MOUNTS["/root/.cache"]="/data/.cache"
MOUNTS["${USER_HOME}/.cache"]="/data/.cache"
MOUNTS["${ROOT}/models"]="/data/models"

MOUNTS["${ROOT}/embeddings"]="/data/embeddings"
Expand Down Expand Up @@ -55,8 +56,8 @@ done
echo "Installing extension dependencies (if any)"

# because we build our container as root:
chown -R root ~/.cache/
chmod 766 ~/.cache/
chown -R $PUID:$PGID ~/.cache/
chmod 776 ~/.cache/

shopt -s nullglob
# For install.py, please refer to https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Developing-extensions#installpy
Expand Down