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
13
3
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
18
5
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
20
9
21
10
WORKDIR /app
22
- RUN --mount=type=cache,target=/root/.cache/pip python3 -m pip install wheel
23
11
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/*
26
22
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
29
34
30
35
# 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
35
44
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/*
39
56
40
57
# Fix missing libnvinfer7
41
- USER root
42
58
RUN ln -s /usr/lib/x86_64-linux-gnu/libnvinfer.so /usr/lib/x86_64-linux-gnu/libnvinfer.so.7 && \
43
59
ln -s /usr/lib/x86_64-linux-gnu/libnvinfer_plugin.so /usr/lib/x86_64-linux-gnu/libnvinfer_plugin.so.7
44
60
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
49
64
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"
51
72
ENV LD_PRELOAD=libtcmalloc.so
52
73
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" ]
0 commit comments