From 3253fc39b56fcfc08baf1a2aa59161e2cf8fa994 Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Mon, 4 Dec 2023 21:09:23 +0100 Subject: [PATCH] Fix conda hook to work in both terminal and Jupyter Notebook (#2047) * Fix conda hook to work in both terminal and Jupyter Notebook * Fix hook for Jupyter Terminals * Rename startup hook to have order of precedence * Try to increase sleep * Comment making env_name default in custom_environment --- .../recipe_code/custom_environment.dockerfile | 23 +++++++++---------- .../10activate-conda-env.sh | 8 +++++++ images/docker-stacks-foundation/Dockerfile | 7 ++++-- tests/base-notebook/test_start_container.py | 2 +- 4 files changed, 25 insertions(+), 15 deletions(-) create mode 100755 images/docker-stacks-foundation/10activate-conda-env.sh diff --git a/docs/using/recipe_code/custom_environment.dockerfile b/docs/using/recipe_code/custom_environment.dockerfile index e53c9690c5..9fd94b01e5 100644 --- a/docs/using/recipe_code/custom_environment.dockerfile +++ b/docs/using/recipe_code/custom_environment.dockerfile @@ -28,17 +28,16 @@ RUN "${CONDA_DIR}/envs/${env_name}/bin/python" -m ipykernel install --user --nam RUN "${CONDA_DIR}/envs/${env_name}/bin/pip" install --no-cache-dir \ 'flake8' -# Creating a startup hook, which will activate our custom environment by default in Jupyter Notebook -# More info about startup hooks: https://jupyter-docker-stacks.readthedocs.io/en/latest/using/common.html#startup-hooks -# You can comment this section to keep the default environment in Jupyter Notebook -USER root -RUN activate_custom_env_script=/usr/local/bin/before-notebook.d/activate_custom_env.sh && \ - echo "#!/bin/bash" > ${activate_custom_env_script} && \ - echo "eval \"$(conda shell.bash activate "${env_name}")\"" >> ${activate_custom_env_script} && \ - chmod +x ${activate_custom_env_script} +# Uncomment this section to activate custom environment by default +# Note: uncommenting this section makes "${env_name}" default both for Jupyter Notebook and Terminals +# More information here: https://github.com/jupyter/docker-stacks/pull/2047 +# USER root +# RUN \ +# # This changes a startup hook, which will activate our custom environment for the process +# echo conda activate "${env_name}" >> /usr/local/bin/before-notebook.d/10activate-conda-env.sh && \ +# # This makes the custom environment default in Jupyter Terminals for all users which might be created later +# echo conda activate "${env_name}" >> /etc/skel/.bashrc && \ +# # This makes the custom environment default in Jupyter Terminals for already existing NB_USER +# echo conda activate "${env_name}" >> "/home/${NB_USER}/.bashrc" USER ${NB_UID} - -# Making this environment default in Terminal -# You can comment this line to keep the default environment in a Terminal -RUN echo "conda activate ${env_name}" >> "${HOME}/.bashrc" diff --git a/images/docker-stacks-foundation/10activate-conda-env.sh b/images/docker-stacks-foundation/10activate-conda-env.sh new file mode 100755 index 0000000000..ed7347f3b6 --- /dev/null +++ b/images/docker-stacks-foundation/10activate-conda-env.sh @@ -0,0 +1,8 @@ +#!/bin/bash +# Copyright (c) Jupyter Development Team. +# Distributed under the terms of the Modified BSD License. + +# This registers the initialization code for the conda shell code +# It also activates default environment in the end, so we don't need to activate it manually +# Documentation: https://docs.conda.io/projects/conda/en/latest/dev-guide/deep-dives/activation.html +eval "$(conda shell.bash hook)" diff --git a/images/docker-stacks-foundation/Dockerfile b/images/docker-stacks-foundation/Dockerfile index 3fc4ec1521..34a3077e33 100644 --- a/images/docker-stacks-foundation/Dockerfile +++ b/images/docker-stacks-foundation/Dockerfile @@ -59,8 +59,9 @@ RUN chmod a+rx /usr/local/bin/fix-permissions # Enable prompt color in the skeleton .bashrc before creating the default NB_USER # hadolint ignore=SC2016 RUN sed -i 's/^#force_color_prompt=yes/force_color_prompt=yes/' /etc/skel/.bashrc && \ - # Add call to conda init script see https://stackoverflow.com/a/58081608/4413446 - echo 'eval "$(command conda shell.bash hook 2> /dev/null)"' >> /etc/skel/.bashrc + # More information in: https://github.com/jupyter/docker-stacks/pull/2047 + # and docs: https://docs.conda.io/projects/conda/en/latest/dev-guide/deep-dives/activation.html + echo 'eval "$(conda shell.bash hook)"' >> /etc/skel/.bashrc # Create NB_USER with name jovyan user with UID=1000 and in the 'users' group # and make sure these dirs are writable by the `users` group. @@ -135,6 +136,8 @@ USER root RUN mkdir /usr/local/bin/start-notebook.d && \ mkdir /usr/local/bin/before-notebook.d +COPY 10activate-conda-env.sh /usr/local/bin/before-notebook.d/ + # Switch back to jovyan to avoid accidental container runs as root USER ${NB_UID} diff --git a/tests/base-notebook/test_start_container.py b/tests/base-notebook/test_start_container.py index 830b36c7e7..3fbf08aab3 100644 --- a/tests/base-notebook/test_start_container.py +++ b/tests/base-notebook/test_start_container.py @@ -48,7 +48,7 @@ def test_start_notebook( ports={"8888/tcp": host_port}, ) # sleeping some time to let the server start - time.sleep(1) + time.sleep(2) logs = running_container.logs().decode("utf-8") LOGGER.debug(logs) # checking that the expected command is launched