|
| 1 | +ARG ROOT_CONTAINER=ubuntu:18.04 |
| 2 | + |
| 3 | +FROM $ROOT_CONTAINER |
| 4 | + |
| 5 | +ARG NB_USER="HwHiAiUser" |
| 6 | +ARG NB_UID="1000" |
| 7 | +ARG NB_GID="1000" |
| 8 | + |
| 9 | +# Fix: https://github.com/hadolint/hadolint/wiki/DL4006 |
| 10 | +# Fix: https://github.com/koalaman/shellcheck/wiki/SC3014 |
| 11 | +SHELL ["/bin/bash", "-o", "pipefail", "-c"] |
| 12 | + |
| 13 | +USER root |
| 14 | + |
| 15 | +# Install all OS dependencies for the Server that starts |
| 16 | +# but lacks all features (e.g., download as all possible file formats) |
| 17 | +ENV DEBIAN_FRONTEND noninteractive |
| 18 | +RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports focal main universe" >> /etc/apt/sources.list |
| 19 | +RUN apt-get update --yes && \ |
| 20 | + # - `apt-get upgrade` is run to patch known vulnerabilities in system packages |
| 21 | + # as the Ubuntu base image is rebuilt too seldom sometimes (less than once a month) |
| 22 | + apt-get upgrade --yes && \ |
| 23 | + apt-get install --yes --no-install-recommends \ |
| 24 | + # - bzip2 is necessary to extract the micromamba executable. |
| 25 | + bzip2 \ |
| 26 | + ca-certificates \ |
| 27 | + locales \ |
| 28 | + sudo \ |
| 29 | + # - Add necessary fonts for matplotlib/seaborn |
| 30 | + # See https://github.com/jupyter/docker-stacks/pull/380 for details |
| 31 | + fonts-liberation \ |
| 32 | + # - `pandoc` is used to convert notebooks to html files |
| 33 | + # it's not present in the aarch64 Ubuntu image, so we install it here |
| 34 | + pandoc \ |
| 35 | + # - `run-one` - a wrapper script that runs no more |
| 36 | + # than one unique instance of some command with a unique set of arguments, |
| 37 | + # we use `run-one-constantly` to support the `RESTARTABLE` option |
| 38 | + run-one \ |
| 39 | + # - `tini` is installed as a helpful container entrypoint, |
| 40 | + # that reaps zombie processes and such of the actual executable we want to start |
| 41 | + # See https://github.com/krallin/tini#why-tini for details |
| 42 | + tini \ |
| 43 | + wget && \ |
| 44 | + apt-get clean && rm -rf /var/lib/apt/lists/* && \ |
| 45 | + echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && \ |
| 46 | + echo "C.UTF-8 UTF-8" >> /etc/locale.gen && \ |
| 47 | + locale-gen |
| 48 | + |
| 49 | +# Configure environment |
| 50 | +ENV CONDA_DIR=/opt/conda \ |
| 51 | + SHELL=/bin/bash \ |
| 52 | + NB_USER="${NB_USER}" \ |
| 53 | + NB_UID=${NB_UID} \ |
| 54 | + NB_GID=${NB_GID} \ |
| 55 | + LC_ALL=C.UTF-8 \ |
| 56 | + LANG=C.UTF-8 \ |
| 57 | + LANGUAGE=C.UTF-8 |
| 58 | +ENV PATH="${CONDA_DIR}/bin:${PATH}" \ |
| 59 | + HOME="/home/${NB_USER}" |
| 60 | + |
| 61 | +# Copy a script that we will use to correct permissions after running certain commands |
| 62 | +COPY fix-permissions /usr/local/bin/fix-permissions |
| 63 | +RUN chmod a+rx /usr/local/bin/fix-permissions |
| 64 | + |
| 65 | +# Enable prompt color in the skeleton .bashrc before creating the default NB_USER |
| 66 | +# hadolint ignore=SC2016 |
| 67 | +RUN sed -i 's/^#force_color_prompt=yes/force_color_prompt=yes/' /etc/skel/.bashrc && \ |
| 68 | + # More information in: https://github.com/jupyter/docker-stacks/pull/2047 |
| 69 | + # and docs: https://docs.conda.io/projects/conda/en/latest/dev-guide/deep-dives/activation.html |
| 70 | + echo 'eval "$(conda shell.bash hook)"' >> /etc/skel/.bashrc |
| 71 | + |
| 72 | +# Create NB_USER with name jovyan user with UID=1000 and in the 'users' group |
| 73 | +# and make sure these dirs are writable by the `users` group. |
| 74 | +RUN echo "auth requisite pam_deny.so" >> /etc/pam.d/su && \ |
| 75 | + sed -i.bak -e 's/^%admin/#%admin/' /etc/sudoers && \ |
| 76 | + sed -i.bak -e 's/^%sudo/#%sudo/' /etc/sudoers && \ |
| 77 | + useradd --no-log-init --create-home --shell /bin/bash --uid "${NB_UID}" --no-user-group "${NB_USER}" && \ |
| 78 | + mkdir -p "${CONDA_DIR}" && \ |
| 79 | + chown "${NB_USER}:${NB_GID}" "${CONDA_DIR}" && \ |
| 80 | + chmod g+w /etc/passwd && \ |
| 81 | + fix-permissions "${CONDA_DIR}" && \ |
| 82 | + fix-permissions "/home/${NB_USER}" |
| 83 | + |
| 84 | +USER ${NB_UID} |
| 85 | + |
| 86 | +# Pin the Python version here, or set it to "default" |
| 87 | +ARG PYTHON_VERSION=3.7.5 |
| 88 | + |
| 89 | +# Setup work directory for backward-compatibility |
| 90 | +RUN mkdir "/home/${NB_USER}/work" && \ |
| 91 | + fix-permissions "/home/${NB_USER}" |
| 92 | + |
| 93 | +# Download and install Micromamba, and initialize the Conda prefix. |
| 94 | +# <https://github.com/mamba-org/mamba#micromamba> |
| 95 | +# Similar projects using Micromamba: |
| 96 | +# - Micromamba-Docker: <https://github.com/mamba-org/micromamba-docker> |
| 97 | +# - repo2docker: <https://github.com/jupyterhub/repo2docker> |
| 98 | +# Install Python, Mamba, and jupyter_core |
| 99 | +# Cleanup temporary files and remove Micromamba |
| 100 | +# Correct permissions |
| 101 | +# Do all this in a single RUN command to avoid duplicating all of the |
| 102 | +# files across image layers when the permissions change |
| 103 | +COPY --chown="${NB_UID}:${NB_GID}" initial-condarc "${CONDA_DIR}/.condarc" |
| 104 | +WORKDIR /tmp |
| 105 | +RUN set -x && \ |
| 106 | + arch=$(uname -m) && \ |
| 107 | + if [ "${arch}" = "x86_64" ]; then \ |
| 108 | + # Should be simpler, see <https://github.com/mamba-org/mamba/issues/1437> |
| 109 | + arch="64"; \ |
| 110 | + fi && \ |
| 111 | + # https://mamba.readthedocs.io/en/latest/installation/micromamba-installation.html#linux-and-macos |
| 112 | + wget --progress=dot:giga -O - \ |
| 113 | + "https://micro.mamba.pm/api/micromamba/linux-${arch}/latest" | tar -xvj bin/micromamba && \ |
| 114 | + PYTHON_SPECIFIER="python=${PYTHON_VERSION}" && \ |
| 115 | + if [[ "${PYTHON_VERSION}" == "default" ]]; then PYTHON_SPECIFIER="python"; fi && \ |
| 116 | + # Install the packages |
| 117 | + ./bin/micromamba install \ |
| 118 | + --root-prefix="${CONDA_DIR}" \ |
| 119 | + --prefix="${CONDA_DIR}" \ |
| 120 | + --yes \ |
| 121 | + "${PYTHON_SPECIFIER}" \ |
| 122 | + 'mamba' \ |
| 123 | + 'jupyter_core' && \ |
| 124 | + rm -rf /tmp/bin/ && \ |
| 125 | + # Pin major.minor version of python |
| 126 | + # https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-pkgs.html#preventing-packages-from-updating-pinning |
| 127 | + mamba list --full-name 'python' | tail -1 | tr -s ' ' | cut -d ' ' -f 1,2 | sed 's/\.[^.]*$/.*/' >> "${CONDA_DIR}/conda-meta/pinned" && \ |
| 128 | + mamba install --yes \ |
| 129 | + 'jupyterlab' \ |
| 130 | + 'notebook' \ |
| 131 | + 'jupyterhub' \ |
| 132 | + 'nbclassic' && \ |
| 133 | + jupyter server --generate-config && \ |
| 134 | + mamba clean --all -f -y && \ |
| 135 | + npm cache clean --force && \ |
| 136 | + jupyter lab clean && \ |
| 137 | + rm -rf "/home/${NB_USER}/.cache/yarn" && \ |
| 138 | + fix-permissions "${CONDA_DIR}" && \ |
| 139 | + fix-permissions "/home/${NB_USER}" |
| 140 | + |
| 141 | +# Copy local files as late as possible to avoid cache busting |
| 142 | +COPY run-hooks.sh start.sh /usr/local/bin/ |
| 143 | + |
| 144 | +# Configure container entrypoint |
| 145 | +ENTRYPOINT ["tini", "-g", "--", "start.sh"] |
| 146 | + |
| 147 | +ENV JUPYTER_PORT=8888 |
| 148 | +EXPOSE $JUPYTER_PORT |
| 149 | + |
| 150 | +# Configure container startup |
| 151 | +CMD ["start-notebook.py"] |
| 152 | + |
| 153 | +# Copy local files as late as possible to avoid cache busting |
| 154 | +COPY start-notebook.py start-notebook.sh start-singleuser.py start-singleuser.sh /usr/local/bin/ |
| 155 | +COPY jupyter_server_config.py docker_healthcheck.py /etc/jupyter/ |
| 156 | + |
| 157 | +USER root |
| 158 | + |
| 159 | +RUN fix-permissions /etc/jupyter/ |
| 160 | + |
| 161 | +RUN chmod a+rx /usr/local/bin/start.sh && \ |
| 162 | + chmod a+rx /usr/local/bin/run-hooks.sh |
| 163 | + |
| 164 | +# Create dirs for startup hooks |
| 165 | +RUN mkdir /usr/local/bin/start-notebook.d && \ |
| 166 | + mkdir /usr/local/bin/before-notebook.d |
| 167 | + |
| 168 | +COPY 10activate-conda-env.sh /usr/local/bin/before-notebook.d/ |
| 169 | + |
| 170 | +# HEALTHCHECK documentation: https://docs.docker.com/engine/reference/builder/#healthcheck |
| 171 | +# This healtcheck works well for `lab`, `notebook`, `nbclassic`, `server`, and `retro` jupyter commands |
| 172 | +# https://github.com/jupyter/docker-stacks/issues/915#issuecomment-1068528799 |
| 173 | +HEALTHCHECK --interval=3s --timeout=1s --start-period=3s --retries=3 \ |
| 174 | + CMD /etc/jupyter/docker_healthcheck.py || exit 1 |
| 175 | + |
| 176 | +# Switch back to jovyan to avoid accidental container runs as root |
| 177 | +USER ${NB_UID} |
| 178 | + |
| 179 | +WORKDIR "${HOME}" |
0 commit comments