From 853699c249f8cc2d62211d36e49c6520bdf216fd Mon Sep 17 00:00:00 2001 From: Oliver Ofenloch <57812959+ofenloch@users.noreply.github.com> Date: Tue, 2 Apr 2024 21:35:16 +0200 Subject: [PATCH 01/11] pass notebok dir as build argument the argument is named TENSORFLOW_NOTEBOOK_DIR and defaults to /home/tf_user/notebooks --- tensorflow_runtime_dockerfiles/cpu.Dockerfile | 8 +++++--- tensorflow_runtime_dockerfiles/gpu.Dockerfile | 8 +++++--- tensorflow_runtime_dockerfiles/tpu.Dockerfile | 8 +++++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/tensorflow_runtime_dockerfiles/cpu.Dockerfile b/tensorflow_runtime_dockerfiles/cpu.Dockerfile index 280f9a6a..df409433 100644 --- a/tensorflow_runtime_dockerfiles/cpu.Dockerfile +++ b/tensorflow_runtime_dockerfiles/cpu.Dockerfile @@ -26,6 +26,8 @@ RUN /setup.packages.sh /cpu.packages.txt ARG PYTHON_VERSION=python3.11 ARG TENSORFLOW_PACKAGE=tf-nightly +ARG TENSORFLOW_NOTEBOOK_DIR=/home/tf_user/notebooks +ENV TENSORFLOW_NOTEBOOK_DIR=${TENSORFLOW_NOTEBOOK_DIR} COPY setup.python.sh /setup.python.sh COPY cpu.requirements.txt /cpu.requirements.txt RUN /setup.python.sh $PYTHON_VERSION /cpu.requirements.txt @@ -40,12 +42,12 @@ COPY jupyter.requirements.txt /jupyter.requirements.txt COPY setup.jupyter.sh /setup.jupyter.sh RUN python3 -m pip install --no-cache-dir -r /jupyter.requirements.txt -U RUN /setup.jupyter.sh -COPY jupyter.readme.md /tf/tensorflow-tutorials/README.md +COPY jupyter.readme.md ${TENSORFLOW_NOTEBOOK_DIR}/tensorflow-tutorials/README.md -WORKDIR /tf +WORKDIR ${TENSORFLOW_NOTEBOOK_DIR} EXPOSE 8888 -CMD ["bash", "-c", "source /etc/bash.bashrc && jupyter notebook --notebook-dir=/tf --ip 0.0.0.0 --no-browser --allow-root"] +CMD ["bash", "-c", "source /etc/bash.bashrc && jupyter notebook --notebook-dir=${TENSORFLOW_NOTEBOOK_DIR} --ip 0.0.0.0 --no-browser --allow-root"] FROM base as test diff --git a/tensorflow_runtime_dockerfiles/gpu.Dockerfile b/tensorflow_runtime_dockerfiles/gpu.Dockerfile index 73512d40..ceeb8db1 100644 --- a/tensorflow_runtime_dockerfiles/gpu.Dockerfile +++ b/tensorflow_runtime_dockerfiles/gpu.Dockerfile @@ -25,6 +25,8 @@ RUN /setup.packages.sh /gpu.packages.txt ARG PYTHON_VERSION=python3.11 ARG TENSORFLOW_PACKAGE=tf-nightly +ARG TENSORFLOW_NOTEBOOK_DIR=/home/tf_user/notebooks +ENV TENSORFLOW_NOTEBOOK_DIR=${TENSORFLOW_NOTEBOOK_DIR} COPY setup.python.sh /setup.python.sh COPY gpu.requirements.txt /gpu.requirements.txt RUN /setup.python.sh $PYTHON_VERSION /gpu.requirements.txt @@ -42,12 +44,12 @@ COPY jupyter.requirements.txt /jupyter.requirements.txt COPY setup.jupyter.sh /setup.jupyter.sh RUN python3 -m pip install --no-cache-dir -r /jupyter.requirements.txt -U RUN /setup.jupyter.sh -COPY jupyter.readme.md /tf/tensorflow-tutorials/README.md +COPY jupyter.readme.md ${TENSORFLOW_NOTEBOOK_DIR}/tensorflow-tutorials/README.md -WORKDIR /tf +WORKDIR ${TENSORFLOW_NOTEBOOK_DIR} EXPOSE 8888 -CMD ["bash", "-c", "source /etc/bash.bashrc && jupyter notebook --notebook-dir=/tf --ip 0.0.0.0 --no-browser --allow-root"] +CMD ["bash", "-c", "source /etc/bash.bashrc && jupyter notebook --notebook-dir=${TENSORFLOW_NOTEBOOK_DIR} --ip 0.0.0.0 --no-browser --allow-root"] FROM base as test diff --git a/tensorflow_runtime_dockerfiles/tpu.Dockerfile b/tensorflow_runtime_dockerfiles/tpu.Dockerfile index 501b410e..edd8ed17 100644 --- a/tensorflow_runtime_dockerfiles/tpu.Dockerfile +++ b/tensorflow_runtime_dockerfiles/tpu.Dockerfile @@ -26,6 +26,8 @@ RUN /setup.packages.sh /tpu.packages.txt ARG PYTHON_VERSION=python3.11 ARG TENSORFLOW_PACKAGE=tf-nightly-tpu +ARG TENSORFLOW_NOTEBOOK_DIR=/home/tf_user/notebooks +ENV TENSORFLOW_NOTEBOOK_DIR=${TENSORFLOW_NOTEBOOK_DIR} COPY setup.python.sh /setup.python.sh COPY cpu.requirements.txt /tpu.requirements.txt RUN /setup.python.sh $PYTHON_VERSION /tpu.requirements.txt @@ -40,12 +42,12 @@ COPY jupyter.requirements.txt /jupyter.requirements.txt COPY setup.jupyter.sh /setup.jupyter.sh RUN python3 -m pip install --no-cache-dir -r /jupyter.requirements.txt -U RUN /setup.jupyter.sh -COPY jupyter.readme.md /tf/tensorflow-tutorials/README.md +COPY jupyter.readme.md ${TENSORFLOW_NOTEBOOK_DIR}/tensorflow-tutorials/README.md -WORKDIR /tf +WORKDIR ${TENSORFLOW_NOTEBOOK_DIR} EXPOSE 8888 -CMD ["bash", "-c", "source /etc/bash.bashrc && jupyter notebook --notebook-dir=/tf --ip 0.0.0.0 --no-browser --allow-root"] +CMD ["bash", "-c", "source /etc/bash.bashrc && jupyter notebook --notebook-dir=${TENSORFLOW_NOTEBOOK_DIR} --ip 0.0.0.0 --no-browser --allow-root"] FROM base as test From 830ebce8a7bca058e9d8868c1e2521b7ed785e0e Mon Sep 17 00:00:00 2001 From: Oliver Ofenloch <57812959+ofenloch@users.noreply.github.com> Date: Tue, 2 Apr 2024 21:35:59 +0200 Subject: [PATCH 02/11] use environment variable TENSORFLOW_NOTEBOOK_DIR to create the tutorials directory --- tensorflow_runtime_dockerfiles/setup.jupyter.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tensorflow_runtime_dockerfiles/setup.jupyter.sh b/tensorflow_runtime_dockerfiles/setup.jupyter.sh index 35cad5dd..059a2781 100755 --- a/tensorflow_runtime_dockerfiles/setup.jupyter.sh +++ b/tensorflow_runtime_dockerfiles/setup.jupyter.sh @@ -1,13 +1,15 @@ #!/bin/bash jupyter serverextension enable --py jupyter_http_over_ws -mkdir -p /tf/tensorflow-tutorials -chmod -R a+rwx /tf/ +TENSORFLOW_TUTORIALS="${TENSORFLOW_NOTEBOOK_DIR}/tensorflow-tutorials" +mkdir -p -v ${TENSORFLOW_TUTORIALS} + +chmod -R a+rwx ${TENSORFLOW_NOTEBOOK_DIR} mkdir /.local chmod a+rwx /.local apt-get update apt-get install -y --no-install-recommends wget git -cd /tf/tensorflow-tutorials +cd ${TENSORFLOW_TUTORIALS} wget https://raw.githubusercontent.com/tensorflow/docs/master/site/en/tutorials/keras/classification.ipynb wget https://raw.githubusercontent.com/tensorflow/docs/master/site/en/tutorials/keras/overfit_and_underfit.ipynb wget https://raw.githubusercontent.com/tensorflow/docs/master/site/en/tutorials/keras/regression.ipynb From cf7a849d98f4c1738a5594233bb228e356a4ba88 Mon Sep 17 00:00:00 2001 From: Oliver Ofenloch <57812959+ofenloch@users.noreply.github.com> Date: Tue, 2 Apr 2024 21:50:30 +0200 Subject: [PATCH 03/11] add little script creating user TENSORFLOW_USER --- .../setup.tensorflow.user.sh | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100755 tensorflow_runtime_dockerfiles/setup.tensorflow.user.sh diff --git a/tensorflow_runtime_dockerfiles/setup.tensorflow.user.sh b/tensorflow_runtime_dockerfiles/setup.tensorflow.user.sh new file mode 100755 index 00000000..b028d348 --- /dev/null +++ b/tensorflow_runtime_dockerfiles/setup.tensorflow.user.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# +# Copyright 2022 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +# +# setup.tensorflow.user.sh: create an user account used to run tensorflow +# Usage: set envrionment variables TENSORFLOW_USER, TENSORFLOW_GROUP, TENSORFLOW_UID, TENSORFLOW_GID and run script setup.tensorflow.user.sh +# + +echo "creating group ${TENSORFLOW_GROUP} with gid ${TENSORFLOW_GID} ..." && \ +groupadd --system --gid ${TENSORFLOW_GID} ${TF_GROUP} && \ +echo "creating user ${TENSORFLOW_USER}:${TENSORFLOW_GROUP} (${TENSORFLOW_UID}:${TENSORFLOW_GID}) ..." && \ +useradd --system --uid ${TENSORFLOW_UID} --home-dir=/home/${TENSORFLOW_USER} --create-home --gid ${TENSORFLOW_GID} ${TENSORFLOW_USER} \ No newline at end of file From fc0303ff55a76499941bcc1102fee5b745eb4c9f Mon Sep 17 00:00:00 2001 From: Oliver Ofenloch <57812959+ofenloch@users.noreply.github.com> Date: Tue, 2 Apr 2024 22:12:22 +0200 Subject: [PATCH 04/11] fix variable name --- tensorflow_runtime_dockerfiles/setup.tensorflow.user.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_runtime_dockerfiles/setup.tensorflow.user.sh b/tensorflow_runtime_dockerfiles/setup.tensorflow.user.sh index b028d348..21af5ecf 100755 --- a/tensorflow_runtime_dockerfiles/setup.tensorflow.user.sh +++ b/tensorflow_runtime_dockerfiles/setup.tensorflow.user.sh @@ -20,6 +20,6 @@ # echo "creating group ${TENSORFLOW_GROUP} with gid ${TENSORFLOW_GID} ..." && \ -groupadd --system --gid ${TENSORFLOW_GID} ${TF_GROUP} && \ +groupadd --system --gid ${TENSORFLOW_GID} ${TENSORFLOW_GROUP} && \ echo "creating user ${TENSORFLOW_USER}:${TENSORFLOW_GROUP} (${TENSORFLOW_UID}:${TENSORFLOW_GID}) ..." && \ useradd --system --uid ${TENSORFLOW_UID} --home-dir=/home/${TENSORFLOW_USER} --create-home --gid ${TENSORFLOW_GID} ${TENSORFLOW_USER} \ No newline at end of file From 17ed504e58176d856422446e23b44a58904456aa Mon Sep 17 00:00:00 2001 From: Oliver Ofenloch <57812959+ofenloch@users.noreply.github.com> Date: Wed, 3 Apr 2024 18:49:24 +0200 Subject: [PATCH 05/11] Add ARGs and ENV variables needed to create the TENSORFLOW_USER. Run script setup.tensorflow.user.sh to create TENSORFLOW_USER and TENSORFLOW_GROUP. Make sure TENSORFLOW_USER owns his home directory and TENSORFLOW_NOTEBOOK_DIR. To run jupyter as TENSORFLOW_USER it is neccessary to split the python and jupyter setup properly: All the apt stuff must be done as root and all the pip stuff must be done as TENSORFLOW_USER. Once this is done, we can do a "USER=${TENSORFLOW_USER} before we run jupyter. --- tensorflow_runtime_dockerfiles/cpu.Dockerfile | 14 +++++++++++++- tensorflow_runtime_dockerfiles/gpu.Dockerfile | 14 +++++++++++++- tensorflow_runtime_dockerfiles/tpu.Dockerfile | 14 +++++++++++++- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/tensorflow_runtime_dockerfiles/cpu.Dockerfile b/tensorflow_runtime_dockerfiles/cpu.Dockerfile index df409433..c7713275 100644 --- a/tensorflow_runtime_dockerfiles/cpu.Dockerfile +++ b/tensorflow_runtime_dockerfiles/cpu.Dockerfile @@ -26,7 +26,15 @@ RUN /setup.packages.sh /cpu.packages.txt ARG PYTHON_VERSION=python3.11 ARG TENSORFLOW_PACKAGE=tf-nightly -ARG TENSORFLOW_NOTEBOOK_DIR=/home/tf_user/notebooks +ARG TENSORFLOW_USER=tfuser +ARG TENSORFLOW_GROUP=tensorflow +ARG TENSORFLOW_UID=2234 +ARG TENSORFLOW_GID=5567 +ARG TENSORFLOW_NOTEBOOK_DIR=/home/${TENSORFLOW_USER}/notebooks +ENV TENSORFLOW_USER=${TENSORFLOW_USER} +ENV TENSORFLOW_GROUP=${TENSORFLOW_GROUP} +ENV TENSORFLOW_UID=${TENSORFLOW_UID} +ENV TENSORFLOW_GID=${TENSORFLOW_GID} ENV TENSORFLOW_NOTEBOOK_DIR=${TENSORFLOW_NOTEBOOK_DIR} COPY setup.python.sh /setup.python.sh COPY cpu.requirements.txt /cpu.requirements.txt @@ -44,6 +52,10 @@ RUN python3 -m pip install --no-cache-dir -r /jupyter.requirements.txt -U RUN /setup.jupyter.sh COPY jupyter.readme.md ${TENSORFLOW_NOTEBOOK_DIR}/tensorflow-tutorials/README.md +COPY setup.tensorflow.user.sh /setup.tensorflow.user.sh +RUN /setup.tensorflow.user.sh +RUN chown -R ${TENSORFLOW_USER}:${TENSORFLOW_GROUP} ${TENSORFLOW_NOTEBOOK_DIR} /home/${TENSORFLOW_USER} + WORKDIR ${TENSORFLOW_NOTEBOOK_DIR} EXPOSE 8888 diff --git a/tensorflow_runtime_dockerfiles/gpu.Dockerfile b/tensorflow_runtime_dockerfiles/gpu.Dockerfile index ceeb8db1..a75e02bc 100644 --- a/tensorflow_runtime_dockerfiles/gpu.Dockerfile +++ b/tensorflow_runtime_dockerfiles/gpu.Dockerfile @@ -25,7 +25,15 @@ RUN /setup.packages.sh /gpu.packages.txt ARG PYTHON_VERSION=python3.11 ARG TENSORFLOW_PACKAGE=tf-nightly -ARG TENSORFLOW_NOTEBOOK_DIR=/home/tf_user/notebooks +ARG TENSORFLOW_USER=tfuser +ARG TENSORFLOW_GROUP=tensorflow +ARG TENSORFLOW_UID=2234 +ARG TENSORFLOW_GID=5567 +ARG TENSORFLOW_NOTEBOOK_DIR=/home/${TENSORFLOW_USER}/notebooks +ENV TENSORFLOW_USER=${TENSORFLOW_USER} +ENV TENSORFLOW_GROUP=${TENSORFLOW_GROUP} +ENV TENSORFLOW_UID=${TENSORFLOW_UID} +ENV TENSORFLOW_GID=${TENSORFLOW_GID} ENV TENSORFLOW_NOTEBOOK_DIR=${TENSORFLOW_NOTEBOOK_DIR} COPY setup.python.sh /setup.python.sh COPY gpu.requirements.txt /gpu.requirements.txt @@ -46,6 +54,10 @@ RUN python3 -m pip install --no-cache-dir -r /jupyter.requirements.txt -U RUN /setup.jupyter.sh COPY jupyter.readme.md ${TENSORFLOW_NOTEBOOK_DIR}/tensorflow-tutorials/README.md +COPY setup.tensorflow.user.sh /setup.tensorflow.user.sh +RUN /setup.tensorflow.user.sh +RUN chown -R ${TENSORFLOW_USER}:${TENSORFLOW_GROUP} ${TENSORFLOW_NOTEBOOK_DIR} /home/${TENSORFLOW_USER} + WORKDIR ${TENSORFLOW_NOTEBOOK_DIR} EXPOSE 8888 diff --git a/tensorflow_runtime_dockerfiles/tpu.Dockerfile b/tensorflow_runtime_dockerfiles/tpu.Dockerfile index edd8ed17..ee978a97 100644 --- a/tensorflow_runtime_dockerfiles/tpu.Dockerfile +++ b/tensorflow_runtime_dockerfiles/tpu.Dockerfile @@ -26,7 +26,15 @@ RUN /setup.packages.sh /tpu.packages.txt ARG PYTHON_VERSION=python3.11 ARG TENSORFLOW_PACKAGE=tf-nightly-tpu -ARG TENSORFLOW_NOTEBOOK_DIR=/home/tf_user/notebooks +ARG TENSORFLOW_USER=tfuser +ARG TENSORFLOW_GROUP=tensorflow +ARG TENSORFLOW_UID=2234 +ARG TENSORFLOW_GID=5567 +ARG TENSORFLOW_NOTEBOOK_DIR=/home/${TENSORFLOW_USER}/notebooks +ENV TENSORFLOW_USER=${TENSORFLOW_USER} +ENV TENSORFLOW_GROUP=${TENSORFLOW_GROUP} +ENV TENSORFLOW_UID=${TENSORFLOW_UID} +ENV TENSORFLOW_GID=${TENSORFLOW_GID} ENV TENSORFLOW_NOTEBOOK_DIR=${TENSORFLOW_NOTEBOOK_DIR} COPY setup.python.sh /setup.python.sh COPY cpu.requirements.txt /tpu.requirements.txt @@ -44,6 +52,10 @@ RUN python3 -m pip install --no-cache-dir -r /jupyter.requirements.txt -U RUN /setup.jupyter.sh COPY jupyter.readme.md ${TENSORFLOW_NOTEBOOK_DIR}/tensorflow-tutorials/README.md +COPY setup.tensorflow.user.sh /setup.tensorflow.user.sh +RUN /setup.tensorflow.user.sh +RUN chown -R ${TENSORFLOW_USER}:${TENSORFLOW_GROUP} ${TENSORFLOW_NOTEBOOK_DIR} /home/${TENSORFLOW_USER} + WORKDIR ${TENSORFLOW_NOTEBOOK_DIR} EXPOSE 8888 From b5378e3d15f0fe080a26aee512cae76c6255a309 Mon Sep 17 00:00:00 2001 From: Oliver Ofenloch <57812959+ofenloch@users.noreply.github.com> Date: Wed, 3 Apr 2024 19:04:32 +0200 Subject: [PATCH 06/11] add comments about who should do what --- tensorflow_runtime_dockerfiles/cpu.Dockerfile | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tensorflow_runtime_dockerfiles/cpu.Dockerfile b/tensorflow_runtime_dockerfiles/cpu.Dockerfile index c7713275..20d1ef49 100644 --- a/tensorflow_runtime_dockerfiles/cpu.Dockerfile +++ b/tensorflow_runtime_dockerfiles/cpu.Dockerfile @@ -20,7 +20,9 @@ ENV LANG C.UTF-8 COPY setup.sources.sh /setup.sources.sh COPY setup.packages.sh /setup.packages.sh COPY cpu.packages.txt /cpu.packages.txt +# set up apt sources (must be done as root): RUN /setup.sources.sh +# install required packages (must be done as root): RUN /setup.packages.sh /cpu.packages.txt @@ -38,6 +40,7 @@ ENV TENSORFLOW_GID=${TENSORFLOW_GID} ENV TENSORFLOW_NOTEBOOK_DIR=${TENSORFLOW_NOTEBOOK_DIR} COPY setup.python.sh /setup.python.sh COPY cpu.requirements.txt /cpu.requirements.txt +# install python (must be done as root) and do some pip stuff (should possibly be done as TENSORFLOW_USER): RUN /setup.python.sh $PYTHON_VERSION /cpu.requirements.txt RUN pip install --no-cache-dir ${TENSORFLOW_PACKAGE} @@ -46,19 +49,23 @@ RUN chmod a+rwx /etc/bash.bashrc FROM base as jupyter +# install and setup jupyter (should be done as TENSORFLOW_USER): COPY jupyter.requirements.txt /jupyter.requirements.txt COPY setup.jupyter.sh /setup.jupyter.sh RUN python3 -m pip install --no-cache-dir -r /jupyter.requirements.txt -U RUN /setup.jupyter.sh COPY jupyter.readme.md ${TENSORFLOW_NOTEBOOK_DIR}/tensorflow-tutorials/README.md +# create and setup user ${TENSORFLOW_USER}:${TENSORFLOW_GROUP} (must be done as root): COPY setup.tensorflow.user.sh /setup.tensorflow.user.sh RUN /setup.tensorflow.user.sh +# make sure TENSORFLOW_USER owns his home and TENSORFLOW_NOTEBOOK_DIR (must be done as root, shouldn't be neccassary): RUN chown -R ${TENSORFLOW_USER}:${TENSORFLOW_GROUP} ${TENSORFLOW_NOTEBOOK_DIR} /home/${TENSORFLOW_USER} WORKDIR ${TENSORFLOW_NOTEBOOK_DIR} EXPOSE 8888 +# finally start jupyter (this should be done as TENSORFLOW_USER): CMD ["bash", "-c", "source /etc/bash.bashrc && jupyter notebook --notebook-dir=${TENSORFLOW_NOTEBOOK_DIR} --ip 0.0.0.0 --no-browser --allow-root"] FROM base as test From baa5defa3b86d598f1775b3fdbd4860f95aab561 Mon Sep 17 00:00:00 2001 From: Oliver Ofenloch <57812959+ofenloch@users.noreply.github.com> Date: Wed, 3 Apr 2024 19:07:33 +0200 Subject: [PATCH 07/11] Move apt-get commands from setup.jupyter.sh to setup.packages.sh Now setup.jupyter can be run as non-root user. --- tensorflow_runtime_dockerfiles/setup.jupyter.sh | 6 +----- tensorflow_runtime_dockerfiles/setup.packages.sh | 1 + 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/tensorflow_runtime_dockerfiles/setup.jupyter.sh b/tensorflow_runtime_dockerfiles/setup.jupyter.sh index 059a2781..44f61aff 100755 --- a/tensorflow_runtime_dockerfiles/setup.jupyter.sh +++ b/tensorflow_runtime_dockerfiles/setup.jupyter.sh @@ -7,8 +7,7 @@ mkdir -p -v ${TENSORFLOW_TUTORIALS} chmod -R a+rwx ${TENSORFLOW_NOTEBOOK_DIR} mkdir /.local chmod a+rwx /.local -apt-get update -apt-get install -y --no-install-recommends wget git + cd ${TENSORFLOW_TUTORIALS} wget https://raw.githubusercontent.com/tensorflow/docs/master/site/en/tutorials/keras/classification.ipynb wget https://raw.githubusercontent.com/tensorflow/docs/master/site/en/tutorials/keras/overfit_and_underfit.ipynb @@ -17,7 +16,4 @@ wget https://raw.githubusercontent.com/tensorflow/docs/master/site/en/tutorials/ wget https://raw.githubusercontent.com/tensorflow/docs/master/site/en/tutorials/keras/text_classification.ipynb wget https://raw.githubusercontent.com/tensorflow/docs/master/site/en/tutorials/keras/text_classification_with_hub.ipynb -apt-get autoremove -y -apt-get remove -y wget - python3 -m ipykernel.kernelspec diff --git a/tensorflow_runtime_dockerfiles/setup.packages.sh b/tensorflow_runtime_dockerfiles/setup.packages.sh index c7f9b5af..37c30219 100755 --- a/tensorflow_runtime_dockerfiles/setup.packages.sh +++ b/tensorflow_runtime_dockerfiles/setup.packages.sh @@ -23,6 +23,7 @@ set -e export DEBIAN_FRONTEND=noninteractive apt-get update +apt-get install -y --no-install-recommends wget git # Remove commented lines and blank lines from the package list apt-get install -y --no-install-recommends $(sed -e '/^\s*#.*$/d' -e '/^\s*$/d' "$1" | sort -u) From 6e2f75bfbd57b2d16a4db418401a0c6aaf0fa850 Mon Sep 17 00:00:00 2001 From: Oliver Ofenloch <57812959+ofenloch@users.noreply.github.com> Date: Thu, 4 Apr 2024 17:50:39 +0200 Subject: [PATCH 08/11] install and run jupyter (with tensorflow) as non-root user The entire installation of jupyter and tensorflow is done as ${TENSORFLOW_USER} with pip. Folder /home/${TENSORFLOW_USER}/.local/bin is added to the environment variable PATH. A notebook folder /home/${TENSORFLOW_USER}/notebooks is created and the tutorials are install in this folder. Finally the command source /etc/bash.bashrc && jupyter notebook --notebook-dir=${TENSORFLOW_NOTEBOOK_DIR} --ip 0.0.0.0 --no-browser --allow-root is run as TENSORFLOW_USER. The only thing still bothering me is the call to ldconfig in /etc/bash.bashrc. I don't understand why we need this. And it can't be done as non-root user. --- tensorflow_runtime_dockerfiles/cpu.Dockerfile | 29 ++++++++++++------- .../setup.jupyter.sh | 8 ++--- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/tensorflow_runtime_dockerfiles/cpu.Dockerfile b/tensorflow_runtime_dockerfiles/cpu.Dockerfile index 20d1ef49..7aace02c 100644 --- a/tensorflow_runtime_dockerfiles/cpu.Dockerfile +++ b/tensorflow_runtime_dockerfiles/cpu.Dockerfile @@ -40,27 +40,34 @@ ENV TENSORFLOW_GID=${TENSORFLOW_GID} ENV TENSORFLOW_NOTEBOOK_DIR=${TENSORFLOW_NOTEBOOK_DIR} COPY setup.python.sh /setup.python.sh COPY cpu.requirements.txt /cpu.requirements.txt -# install python (must be done as root) and do some pip stuff (should possibly be done as TENSORFLOW_USER): +# install python (must be done as root): RUN /setup.python.sh $PYTHON_VERSION /cpu.requirements.txt -RUN pip install --no-cache-dir ${TENSORFLOW_PACKAGE} - COPY bashrc /etc/bash.bashrc RUN chmod a+rwx /etc/bash.bashrc +# create and setup user ${TENSORFLOW_USER}:${TENSORFLOW_GROUP} (must be done as root): +COPY setup.tensorflow.user.sh /setup.tensorflow.user.sh +RUN /setup.tensorflow.user.sh && \ + mkdir -p /home/${TENSORFLOW_USER}/.local && \ + chown -R ${TENSORFLOW_USER}:${TENSORFLOW_GROUP} /home/${TENSORFLOW_USER}/.local +ENV PATH="${PATH}:/home/${TENSORFLOW_USER}/.local/bin" + +# the rest of the commands is run by TENSORFLOW_USER +USER ${TENSORFLOW_USER} + +RUN pip install --no-cache-dir ${TENSORFLOW_PACKAGE} + FROM base as jupyter +USER ${TENSORFLOW_USER} # install and setup jupyter (should be done as TENSORFLOW_USER): -COPY jupyter.requirements.txt /jupyter.requirements.txt -COPY setup.jupyter.sh /setup.jupyter.sh +COPY --chown=${TENSORFLOW_USER}:${TENSORFLOW_GROUP} jupyter.requirements.txt /jupyter.requirements.txt +COPY --chown=${TENSORFLOW_USER}:${TENSORFLOW_GROUP} setup.jupyter.sh /setup.jupyter.sh RUN python3 -m pip install --no-cache-dir -r /jupyter.requirements.txt -U RUN /setup.jupyter.sh -COPY jupyter.readme.md ${TENSORFLOW_NOTEBOOK_DIR}/tensorflow-tutorials/README.md +COPY --chown=${TENSORFLOW_USER}:${TENSORFLOW_GROUP} jupyter.readme.md ${TENSORFLOW_NOTEBOOK_DIR}/tensorflow-tutorials/README.md + -# create and setup user ${TENSORFLOW_USER}:${TENSORFLOW_GROUP} (must be done as root): -COPY setup.tensorflow.user.sh /setup.tensorflow.user.sh -RUN /setup.tensorflow.user.sh -# make sure TENSORFLOW_USER owns his home and TENSORFLOW_NOTEBOOK_DIR (must be done as root, shouldn't be neccassary): -RUN chown -R ${TENSORFLOW_USER}:${TENSORFLOW_GROUP} ${TENSORFLOW_NOTEBOOK_DIR} /home/${TENSORFLOW_USER} WORKDIR ${TENSORFLOW_NOTEBOOK_DIR} EXPOSE 8888 diff --git a/tensorflow_runtime_dockerfiles/setup.jupyter.sh b/tensorflow_runtime_dockerfiles/setup.jupyter.sh index 44f61aff..81101453 100755 --- a/tensorflow_runtime_dockerfiles/setup.jupyter.sh +++ b/tensorflow_runtime_dockerfiles/setup.jupyter.sh @@ -4,10 +4,6 @@ jupyter serverextension enable --py jupyter_http_over_ws TENSORFLOW_TUTORIALS="${TENSORFLOW_NOTEBOOK_DIR}/tensorflow-tutorials" mkdir -p -v ${TENSORFLOW_TUTORIALS} -chmod -R a+rwx ${TENSORFLOW_NOTEBOOK_DIR} -mkdir /.local -chmod a+rwx /.local - cd ${TENSORFLOW_TUTORIALS} wget https://raw.githubusercontent.com/tensorflow/docs/master/site/en/tutorials/keras/classification.ipynb wget https://raw.githubusercontent.com/tensorflow/docs/master/site/en/tutorials/keras/overfit_and_underfit.ipynb @@ -16,4 +12,6 @@ wget https://raw.githubusercontent.com/tensorflow/docs/master/site/en/tutorials/ wget https://raw.githubusercontent.com/tensorflow/docs/master/site/en/tutorials/keras/text_classification.ipynb wget https://raw.githubusercontent.com/tensorflow/docs/master/site/en/tutorials/keras/text_classification_with_hub.ipynb -python3 -m ipykernel.kernelspec + +python3 -m ipykernel install --user +python3 -m ipykernel.kernelspec --user From 4b3cb9a3dd537ca2ed65440a879caf1eb7460e55 Mon Sep 17 00:00:00 2001 From: Oliver Ofenloch <57812959+ofenloch@users.noreply.github.com> Date: Thu, 4 Apr 2024 18:04:36 +0200 Subject: [PATCH 09/11] chown script test.import_cpu.sh to : --- tensorflow_runtime_dockerfiles/cpu.Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_runtime_dockerfiles/cpu.Dockerfile b/tensorflow_runtime_dockerfiles/cpu.Dockerfile index 7aace02c..0b2ce3f3 100644 --- a/tensorflow_runtime_dockerfiles/cpu.Dockerfile +++ b/tensorflow_runtime_dockerfiles/cpu.Dockerfile @@ -77,5 +77,5 @@ CMD ["bash", "-c", "source /etc/bash.bashrc && jupyter notebook --notebook-dir=$ FROM base as test -COPY test.import_cpu.sh /test.import_cpu.sh +COPY --chown=${TENSORFLOW_USER}:${TENSORFLOW_GROUP} test.import_cpu.sh /test.import_cpu.sh RUN /test.import_cpu.sh From abe1872573fd7fa74577e43ae9902909d6b37ac1 Mon Sep 17 00:00:00 2001 From: Oliver Ofenloch <57812959+ofenloch@users.noreply.github.com> Date: Thu, 4 Apr 2024 19:09:34 +0200 Subject: [PATCH 10/11] apply changes from 6e2f75bfbd57b2d16a4db418401a0c6aaf0fa850 to other Dockerfiles --- tensorflow_runtime_dockerfiles/gpu.Dockerfile | 32 +++++++++++++++---- tensorflow_runtime_dockerfiles/tpu.Dockerfile | 30 +++++++++++------ 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/tensorflow_runtime_dockerfiles/gpu.Dockerfile b/tensorflow_runtime_dockerfiles/gpu.Dockerfile index a75e02bc..1aac1f04 100644 --- a/tensorflow_runtime_dockerfiles/gpu.Dockerfile +++ b/tensorflow_runtime_dockerfiles/gpu.Dockerfile @@ -20,9 +20,12 @@ ENV LANG C.UTF-8 COPY setup.sources.sh /setup.sources.sh COPY setup.packages.sh /setup.packages.sh COPY gpu.packages.txt /gpu.packages.txt +# set up apt sources (must be done as root): RUN /setup.sources.sh +# install required packages (must be done as root): RUN /setup.packages.sh /gpu.packages.txt + ARG PYTHON_VERSION=python3.11 ARG TENSORFLOW_PACKAGE=tf-nightly ARG TENSORFLOW_USER=tfuser @@ -37,34 +40,49 @@ ENV TENSORFLOW_GID=${TENSORFLOW_GID} ENV TENSORFLOW_NOTEBOOK_DIR=${TENSORFLOW_NOTEBOOK_DIR} COPY setup.python.sh /setup.python.sh COPY gpu.requirements.txt /gpu.requirements.txt +# install python (must be done as root): RUN /setup.python.sh $PYTHON_VERSION /gpu.requirements.txt RUN pip install --no-cache-dir ${TENSORFLOW_PACKAGE} COPY setup.cuda.sh /setup.cuda.sh RUN /setup.cuda.sh +# setup cuda (must be done as root): COPY bashrc /etc/bash.bashrc RUN chmod a+rwx /etc/bash.bashrc +# create and setup user ${TENSORFLOW_USER}:${TENSORFLOW_GROUP} (must be done as root): +COPY setup.tensorflow.user.sh /setup.tensorflow.user.sh +RUN /setup.tensorflow.user.sh && \ + mkdir -p /home/${TENSORFLOW_USER}/.local && \ + chown -R ${TENSORFLOW_USER}:${TENSORFLOW_GROUP} /home/${TENSORFLOW_USER}/.local +ENV PATH="${PATH}:/home/${TENSORFLOW_USER}/.local/bin" + +# the rest of the commands is run by TENSORFLOW_USER +USER ${TENSORFLOW_USER} + +RUN pip install --no-cache-dir ${TENSORFLOW_PACKAGE} + FROM base as jupyter -COPY jupyter.requirements.txt /jupyter.requirements.txt -COPY setup.jupyter.sh /setup.jupyter.sh +USER ${TENSORFLOW_USER} +# install and setup jupyter (should be done as TENSORFLOW_USER): +COPY --chown=${TENSORFLOW_USER}:${TENSORFLOW_GROUP} jupyter.requirements.txt /jupyter.requirements.txt +COPY --chown=${TENSORFLOW_USER}:${TENSORFLOW_GROUP} setup.jupyter.sh /setup.jupyter.sh RUN python3 -m pip install --no-cache-dir -r /jupyter.requirements.txt -U RUN /setup.jupyter.sh -COPY jupyter.readme.md ${TENSORFLOW_NOTEBOOK_DIR}/tensorflow-tutorials/README.md +COPY --chown=${TENSORFLOW_USER}:${TENSORFLOW_GROUP} jupyter.readme.md ${TENSORFLOW_NOTEBOOK_DIR}/tensorflow-tutorials/README.md + -COPY setup.tensorflow.user.sh /setup.tensorflow.user.sh -RUN /setup.tensorflow.user.sh -RUN chown -R ${TENSORFLOW_USER}:${TENSORFLOW_GROUP} ${TENSORFLOW_NOTEBOOK_DIR} /home/${TENSORFLOW_USER} WORKDIR ${TENSORFLOW_NOTEBOOK_DIR} EXPOSE 8888 +# finally start jupyter (this should be done as TENSORFLOW_USER): CMD ["bash", "-c", "source /etc/bash.bashrc && jupyter notebook --notebook-dir=${TENSORFLOW_NOTEBOOK_DIR} --ip 0.0.0.0 --no-browser --allow-root"] FROM base as test ENV LD_LIBRARY_PATH /usr/local/cuda/lib64/stubs/:$LD_LIBRARY_PATH -COPY test.import_cpu.sh /test.import_cpu.sh +COPY --chown=${TENSORFLOW_USER}:${TENSORFLOW_GROUP} test.import_cpu.sh /test.import_cpu.sh RUN /test.import_cpu.sh diff --git a/tensorflow_runtime_dockerfiles/tpu.Dockerfile b/tensorflow_runtime_dockerfiles/tpu.Dockerfile index ee978a97..f12ee9f4 100644 --- a/tensorflow_runtime_dockerfiles/tpu.Dockerfile +++ b/tensorflow_runtime_dockerfiles/tpu.Dockerfile @@ -20,7 +20,9 @@ ENV LANG C.UTF-8 COPY setup.sources.sh /setup.sources.sh COPY setup.packages.sh /setup.packages.sh COPY tpu.packages.txt /tpu.packages.txt +# set up apt sources (must be done as root): RUN /setup.sources.sh +# install required packages (must be done as root): RUN /setup.packages.sh /tpu.packages.txt @@ -38,23 +40,33 @@ ENV TENSORFLOW_GID=${TENSORFLOW_GID} ENV TENSORFLOW_NOTEBOOK_DIR=${TENSORFLOW_NOTEBOOK_DIR} COPY setup.python.sh /setup.python.sh COPY cpu.requirements.txt /tpu.requirements.txt +# install python (must be done as root): RUN /setup.python.sh $PYTHON_VERSION /tpu.requirements.txt -RUN pip install --no-cache-dir ${TENSORFLOW_PACKAGE} -f https://storage.googleapis.com/libtpu-tf-releases/index.html - COPY bashrc /etc/bash.bashrc RUN chmod a+rwx /etc/bash.bashrc +# create and setup user ${TENSORFLOW_USER}:${TENSORFLOW_GROUP} (must be done as root): +COPY setup.tensorflow.user.sh /setup.tensorflow.user.sh +RUN /setup.tensorflow.user.sh && \ + mkdir -p /home/${TENSORFLOW_USER}/.local && \ + chown -R ${TENSORFLOW_USER}:${TENSORFLOW_GROUP} /home/${TENSORFLOW_USER}/.local +ENV PATH="${PATH}:/home/${TENSORFLOW_USER}/.local/bin" + +# the rest of the commands is run by TENSORFLOW_USER +USER ${TENSORFLOW_USER} + +RUN pip install --no-cache-dir ${TENSORFLOW_PACKAGE} -f https://storage.googleapis.com/libtpu-tf-releases/index.html FROM base as jupyter -COPY jupyter.requirements.txt /jupyter.requirements.txt -COPY setup.jupyter.sh /setup.jupyter.sh +USER ${TENSORFLOW_USER} +# install and setup jupyter (should be done as TENSORFLOW_USER): +COPY --chown=${TENSORFLOW_USER}:${TENSORFLOW_GROUP} jupyter.requirements.txt /jupyter.requirements.txt +COPY --chown=${TENSORFLOW_USER}:${TENSORFLOW_GROUP} setup.jupyter.sh /setup.jupyter.sh RUN python3 -m pip install --no-cache-dir -r /jupyter.requirements.txt -U RUN /setup.jupyter.sh -COPY jupyter.readme.md ${TENSORFLOW_NOTEBOOK_DIR}/tensorflow-tutorials/README.md +COPY --chown=${TENSORFLOW_USER}:${TENSORFLOW_GROUP} jupyter.readme.md ${TENSORFLOW_NOTEBOOK_DIR}/tensorflow-tutorials/README.md + -COPY setup.tensorflow.user.sh /setup.tensorflow.user.sh -RUN /setup.tensorflow.user.sh -RUN chown -R ${TENSORFLOW_USER}:${TENSORFLOW_GROUP} ${TENSORFLOW_NOTEBOOK_DIR} /home/${TENSORFLOW_USER} WORKDIR ${TENSORFLOW_NOTEBOOK_DIR} EXPOSE 8888 @@ -63,5 +75,5 @@ CMD ["bash", "-c", "source /etc/bash.bashrc && jupyter notebook --notebook-dir=$ FROM base as test -COPY test.import_tpu.sh /test.import_tpu.sh +COPY --chown=${TENSORFLOW_USER}:${TENSORFLOW_GROUP} test.import_tpu.sh /test.import_tpu.sh RUN /test.import_tpu.sh From bc5bbb2a87ca2dfa5a2e9b53ea4274a9a8432067 Mon Sep 17 00:00:00 2001 From: Oliver Ofenloch <57812959+ofenloch@users.noreply.github.com> Date: Thu, 4 Apr 2024 19:11:47 +0200 Subject: [PATCH 11/11] add script building the images --- .../build_images.sh | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100755 tensorflow_runtime_dockerfiles/build_images.sh diff --git a/tensorflow_runtime_dockerfiles/build_images.sh b/tensorflow_runtime_dockerfiles/build_images.sh new file mode 100755 index 00000000..772ca9a2 --- /dev/null +++ b/tensorflow_runtime_dockerfiles/build_images.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +# +# build the CPU images +# +docker build --target=jupyter \ + --build-arg TENSORFLOW_PACKAGE=tf-nightly-cpu \ + --build-arg TENSORFLOW_USER=tf \ + --build-arg TENSORFLOW_GROUP=tfuser \ + --build-arg TENSORFLOW_UID=2024 \ + --build-arg TENSORFLOW_GID=2024 \ + -t tensorflow-nightly-cpu -f cpu.Dockerfile . + +docker build --target=test \ + --build-arg TENSORFLOW_PACKAGE=tf-nightly-cpu \ + --build-arg TENSORFLOW_USER=tf \ + --build-arg TENSORFLOW_GROUP=tfuser \ + --build-arg TENSORFLOW_UID=2024 \ + --build-arg TENSORFLOW_GID=2024 \ + -t tensorflow-nightly-cpu-test -f cpu.Dockerfile . + +# +# build the GPU images +# +docker build --target=jupyter \ + --build-arg TENSORFLOW_PACKAGE=tf-nightly-gpu \ + --build-arg TENSORFLOW_USER=tf \ + --build-arg TENSORFLOW_GROUP=tfuser \ + --build-arg TENSORFLOW_UID=2024 \ + --build-arg TENSORFLOW_GID=2024 \ + -t tensorflow-nightly-gpu -f gpu.Dockerfile . + +docker build --target=test \ + --build-arg TENSORFLOW_PACKAGE=tf-nightly-gpu \ + --build-arg TENSORFLOW_USER=tf \ + --build-arg TENSORFLOW_GROUP=tfuser \ + --build-arg TENSORFLOW_UID=2024 \ + --build-arg TENSORFLOW_GID=2024 \ + -t tensorflow-nightly-gpu-test -f cpu.Dockerfile . + +# +# build the TPU images +# +docker build --target=jupyter \ + --build-arg TENSORFLOW_PACKAGE=tf-nightly-tpu \ + --build-arg TENSORFLOW_USER=tf \ + --build-arg TENSORFLOW_GROUP=tfuser \ + --build-arg TENSORFLOW_UID=2024 \ + --build-arg TENSORFLOW_GID=2024 \ + -t tensorflow-nightly-tpu -f cpu.Dockerfile . + +docker build --target=test \ + --build-arg TENSORFLOW_PACKAGE=tf-nightly-tpu \ + --build-arg TENSORFLOW_USER=tf \ + --build-arg TENSORFLOW_GROUP=tfuser \ + --build-arg TENSORFLOW_UID=2024 \ + --build-arg TENSORFLOW_GID=2024 \ + -t tensorflow-nightly-tpu-test -f cpu.Dockerfile . \ No newline at end of file