Skip to content

Commit 83bf2e0

Browse files
committed
chore(docker): rewrite Dockerfile
Signed-off-by: 陳鈞 <jim60105@gmail.com>
1 parent f8d2673 commit 83bf2e0

6 files changed

+102
-49
lines changed

.dockerignore

+8
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ bitsandbytes_windows_deprecated/
55
dataset/
66
__pycache__/
77
venv/
8+
**/.hadolint.yml
9+
**/*.log
10+
**/.git
11+
**/.gitignore
12+
**/.env
13+
**/.github
14+
**/.vscode
15+
**/*.ps1

.hadolint.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ignored:
2+
- DL3042 # Avoid use of cache directory with pip. Use `pip install --no-cache-dir <package>`
3+
- DL3013 # Pin versions in pip. Instead of `pip install <package>` use `pip install <package>==<version>`
4+
- DL3008 # Pin versions in apt get install. Instead of `apt-get install <package>` use `apt-get install <package>=<version>`
5+
- DL4006 # Set the SHELL option -o pipefail before RUN with a pipe in it
6+
- SC2015 # Note that A && B || C is not if-then-else. C may run when A is true.

Dockerfile

+74-37
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,91 @@
1-
FROM nvcr.io/nvidia/pytorch:23.04-py3 as base
2-
ENV DEBIAN_FRONTEND=noninteractive
3-
ENV TZ=Europe/London
4-
5-
RUN apt update && apt-get install -y software-properties-common
6-
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
7-
--mount=type=cache,target=/var/lib/apt,sharing=locked \
8-
add-apt-repository ppa:deadsnakes/ppa && \
9-
apt update && \
10-
apt-get install -y git curl libgl1 libglib2.0-0 libgoogle-perftools-dev \
11-
python3.10-dev python3.10-tk python3-html5lib python3-apt python3-pip python3.10-distutils && \
12-
rm -rf /var/lib/apt/lists/*
1+
# syntax=docker/dockerfile:1
2+
ARG UID=1000
133

14-
# Set python 3.10 and cuda 11.8 as default
15-
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 3 && \
16-
update-alternatives --set python3 /usr/bin/python3.10 && \
17-
update-alternatives --set cuda /usr/local/cuda-11.8
4+
FROM python:3.10 as build
185

19-
RUN curl -sS https://bootstrap.pypa.io/get-pip.py | python3
6+
# RUN mount cache for multi-arch: https://github.com/docker/buildx/issues/549#issuecomment-1788297892
7+
ARG TARGETARCH
8+
ARG TARGETVARIANT
209

2110
WORKDIR /app
22-
RUN --mount=type=cache,target=/root/.cache/pip python3 -m pip install wheel
2311

24-
# Todo: Install torch 2.1.0 for cu121 support (only available as nightly as of writing)
25-
## RUN --mount=type=cache,target=/root/.cache/pip python3 -m pip install --pre torch ninja setuptools --extra-index-url https://download.pytorch.org/whl/nightly/cu121
12+
# Install under /root/.local
13+
ENV PIP_USER="true"
14+
ARG PIP_NO_WARN_SCRIPT_LOCATION=0
15+
ARG PIP_ROOT_USER_ACTION="ignore"
16+
17+
# Install build dependencies
18+
RUN apt-get update && apt-get upgrade -y && \
19+
apt-get install -y --no-install-recommends python3-launchpadlib git curl && \
20+
apt-get clean && \
21+
rm -rf /var/lib/apt/lists/*
2622

27-
# Todo: Install xformers nightly for Torch 2.1.0 support
28-
## RUN --mount=type=cache,target=/root/.cache/pip python3 -m pip install -v -U git+https://github.com/facebookresearch/xformers.git@main#egg=xformers
23+
# Install PyTorch and TensorFlow
24+
# The versions must align and be in sync with the requirements_linux_docker.txt
25+
# hadolint ignore=SC2102
26+
RUN --mount=type=cache,id=pip-$TARGETARCH$TARGETVARIANT,sharing=locked,target=/root/.cache/pip \
27+
pip install -U --extra-index-url https://download.pytorch.org/whl/cu121 --extra-index-url https://pypi.nvidia.com \
28+
torch==2.1.2 torchvision==0.16.2 \
29+
xformers==0.0.23.post1 \
30+
# Why [and-cuda]: https://github.com/tensorflow/tensorflow/issues/61468#issuecomment-1759462485
31+
tensorflow[and-cuda]==2.14.0 \
32+
ninja \
33+
pip setuptools wheel
2934

3035
# Install requirements
31-
COPY ./requirements.txt ./requirements_linux_docker.txt ./
32-
COPY ./setup/docker_setup.py ./setup.py
33-
RUN --mount=type=cache,target=/root/.cache/pip python3 -m pip install -r ./requirements_linux_docker.txt
34-
RUN --mount=type=cache,target=/root/.cache/pip python3 -m pip install -r ./requirements.txt
36+
RUN --mount=type=cache,id=pip-$TARGETARCH$TARGETVARIANT,sharing=locked,target=/root/.cache/pip \
37+
--mount=source=requirements_linux_docker.txt,target=requirements_linux_docker.txt \
38+
--mount=source=requirements.txt,target=requirements.txt \
39+
--mount=source=setup/docker_setup.py,target=setup.py \
40+
pip install -r requirements_linux_docker.txt -r requirements.txt && \
41+
# Replace pillow with pillow-simd
42+
pip uninstall -y pillow && \
43+
CC="cc -mavx2" pip install -U --force-reinstall pillow-simd
3544

36-
# Replace pillow with pillow-simd
37-
RUN --mount=type=cache,target=/root/.cache/pip python3 -m pip uninstall -y pillow && \
38-
CC="cc -mavx2" python3 -m pip install -U --force-reinstall pillow-simd
45+
FROM python:3.10 as final
46+
47+
ARG UID
48+
49+
WORKDIR /app
50+
51+
# Install runtime dependencies
52+
RUN apt-get update && \
53+
apt-get install -y --no-install-recommends libgl1 libglib2.0-0 libgoogle-perftools-dev dumb-init && \
54+
apt-get clean && \
55+
rm -rf /var/lib/apt/lists/*
3956

4057
# Fix missing libnvinfer7
41-
USER root
4258
RUN ln -s /usr/lib/x86_64-linux-gnu/libnvinfer.so /usr/lib/x86_64-linux-gnu/libnvinfer.so.7 && \
4359
ln -s /usr/lib/x86_64-linux-gnu/libnvinfer_plugin.so /usr/lib/x86_64-linux-gnu/libnvinfer_plugin.so.7
4460

45-
RUN useradd -m -s /bin/bash appuser && \
46-
chown -R appuser: /app
47-
USER appuser
48-
COPY --chown=appuser . .
61+
# Create user
62+
RUN groupadd -g $UID $UID && \
63+
useradd -l -u $UID -g $UID -m -s /bin/sh -N $UID
4964

50-
STOPSIGNAL SIGINT
65+
# Copy dist and support arbitrary user ids (OpenShift best practice)
66+
COPY --chown=$UID:0 --chmod=775 \
67+
--from=build /root/.local /home/$UID/.local
68+
COPY --chown=$UID:0 --chmod=775 . .
69+
70+
ENV PATH="/home/$UID/.local/bin:$PATH"
71+
ENV PYTHONPATH="${PYTHONPATH}:/home/$UID/.local/lib/python3.10/site-packages"
5172
ENV LD_PRELOAD=libtcmalloc.so
5273
ENV PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
53-
ENV PATH="$PATH:/home/appuser/.local/bin"
54-
CMD python3 "./kohya_gui.py" ${CLI_ARGS} --listen 0.0.0.0 --server_port 7860
74+
75+
# Create directories with correct permissions
76+
RUN install -d -m 775 -o $UID -g 0 /dataset && \
77+
install -d -m 775 -o $UID -g 0 /licenses && \
78+
install -d -m 775 -o $UID -g 0 /app
79+
80+
# Copy licenses (OpenShift Policy)
81+
COPY --chmod=775 LICENSE.md /licenses/LICENSE.md
82+
83+
VOLUME [ "/dataset" ]
84+
85+
USER $UID
86+
87+
STOPSIGNAL SIGINT
88+
89+
# Use dumb-init as PID 1 to handle signals properly
90+
ENTRYPOINT ["dumb-init", "--"]
91+
CMD ["python3", "kohya_gui.py", "--listen", "0.0.0.0", "--server_port", "7860"]

docker-compose.yaml

+10-8
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ services:
33
kohya-ss-gui:
44
container_name: kohya-ss-gui
55
image: kohya-ss-gui:latest
6+
user: 1000:0
67
build:
78
context: .
9+
args:
10+
- UID=1000
811
ports:
9-
- 127.0.0.1:3000:3000
1012
- 7860:7860
1113
- 6006:6006
1214
tty: true
@@ -16,15 +18,15 @@ services:
1618
SAFETENSORS_FAST_GPU: 1
1719
DISPLAY: $DISPLAY
1820
tmpfs:
19-
- /tmp
21+
- /tmp
2022
volumes:
21-
- ./dataset:/dataset
22-
- ./.cache/user:/home/appuser/.cache
23-
- ./.cache/triton:/home/appuser/.triton
24-
- ./.cache/config:/app/appuser/.config
25-
- ./.cache/nv:/home/appuser/.nv
26-
- ./.cache/keras:/home/appuser/.keras
2723
- /tmp/.X11-unix:/tmp/.X11-unix
24+
- ./dataset:/dataset
25+
- ./.cache/user:/home/1000/.cache
26+
- ./.cache/triton:/home/1000/.triton
27+
- ./.cache/nv:/home/1000/.nv
28+
- ./.cache/keras:/home/1000/.keras
29+
- ./.cache/config:/home/1000/.config
2830
deploy:
2931
resources:
3032
reservations:

requirements_linux_docker.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
xformers==0.0.20
1+
xformers>=0.0.20
22
bitsandbytes==0.41.1
3-
accelerate==0.19.0
3+
accelerate==0.25.0
44
tensorboard==2.14.1
5-
tensorflow==2.14.0
5+
tensorflow==2.14.0

setup/docker_setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from setuptools import setup, find_packages
22

3-
setup(name="library", version="1.0.3", packages=find_packages())
3+
setup()

0 commit comments

Comments
 (0)