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
This commit is contained in:
Ayaz Salikhov
2023-12-04 21:09:23 +01:00
committed by GitHub
parent 2927745fb2
commit 3253fc39b5
4 changed files with 25 additions and 15 deletions

View File

@@ -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"

View File

@@ -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)"

View File

@@ -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}

View File

@@ -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