Skip to content

Commit

Permalink
Fix conda hook to work in both terminal and Jupyter Notebook (#2047)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mathbunnyru authored Dec 4, 2023
1 parent 2927745 commit 3253fc3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
23 changes: 11 additions & 12 deletions docs/using/recipe_code/custom_environment.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
8 changes: 8 additions & 0 deletions images/docker-stacks-foundation/10activate-conda-env.sh
Original file line number Diff line number Diff line change
@@ -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)"
7 changes: 5 additions & 2 deletions images/docker-stacks-foundation/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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}

Expand Down
2 changes: 1 addition & 1 deletion tests/base-notebook/test_start_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3253fc3

Please sign in to comment.