Skip to content

Commit 3253fc3

Browse files
authored
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
1 parent 2927745 commit 3253fc3

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

docs/using/recipe_code/custom_environment.dockerfile

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,16 @@ RUN "${CONDA_DIR}/envs/${env_name}/bin/python" -m ipykernel install --user --nam
2828
RUN "${CONDA_DIR}/envs/${env_name}/bin/pip" install --no-cache-dir \
2929
'flake8'
3030

31-
# Creating a startup hook, which will activate our custom environment by default in Jupyter Notebook
32-
# More info about startup hooks: https://jupyter-docker-stacks.readthedocs.io/en/latest/using/common.html#startup-hooks
33-
# You can comment this section to keep the default environment in Jupyter Notebook
34-
USER root
35-
RUN activate_custom_env_script=/usr/local/bin/before-notebook.d/activate_custom_env.sh && \
36-
echo "#!/bin/bash" > ${activate_custom_env_script} && \
37-
echo "eval \"$(conda shell.bash activate "${env_name}")\"" >> ${activate_custom_env_script} && \
38-
chmod +x ${activate_custom_env_script}
31+
# Uncomment this section to activate custom environment by default
32+
# Note: uncommenting this section makes "${env_name}" default both for Jupyter Notebook and Terminals
33+
# More information here: https://github.com/jupyter/docker-stacks/pull/2047
34+
# USER root
35+
# RUN \
36+
# # This changes a startup hook, which will activate our custom environment for the process
37+
# echo conda activate "${env_name}" >> /usr/local/bin/before-notebook.d/10activate-conda-env.sh && \
38+
# # This makes the custom environment default in Jupyter Terminals for all users which might be created later
39+
# echo conda activate "${env_name}" >> /etc/skel/.bashrc && \
40+
# # This makes the custom environment default in Jupyter Terminals for already existing NB_USER
41+
# echo conda activate "${env_name}" >> "/home/${NB_USER}/.bashrc"
3942

4043
USER ${NB_UID}
41-
42-
# Making this environment default in Terminal
43-
# You can comment this line to keep the default environment in a Terminal
44-
RUN echo "conda activate ${env_name}" >> "${HOME}/.bashrc"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
# Copyright (c) Jupyter Development Team.
3+
# Distributed under the terms of the Modified BSD License.
4+
5+
# This registers the initialization code for the conda shell code
6+
# It also activates default environment in the end, so we don't need to activate it manually
7+
# Documentation: https://docs.conda.io/projects/conda/en/latest/dev-guide/deep-dives/activation.html
8+
eval "$(conda shell.bash hook)"

images/docker-stacks-foundation/Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ RUN chmod a+rx /usr/local/bin/fix-permissions
5959
# Enable prompt color in the skeleton .bashrc before creating the default NB_USER
6060
# hadolint ignore=SC2016
6161
RUN sed -i 's/^#force_color_prompt=yes/force_color_prompt=yes/' /etc/skel/.bashrc && \
62-
# Add call to conda init script see https://stackoverflow.com/a/58081608/4413446
63-
echo 'eval "$(command conda shell.bash hook 2> /dev/null)"' >> /etc/skel/.bashrc
62+
# More information in: https://github.com/jupyter/docker-stacks/pull/2047
63+
# and docs: https://docs.conda.io/projects/conda/en/latest/dev-guide/deep-dives/activation.html
64+
echo 'eval "$(conda shell.bash hook)"' >> /etc/skel/.bashrc
6465

6566
# Create NB_USER with name jovyan user with UID=1000 and in the 'users' group
6667
# and make sure these dirs are writable by the `users` group.
@@ -135,6 +136,8 @@ USER root
135136
RUN mkdir /usr/local/bin/start-notebook.d && \
136137
mkdir /usr/local/bin/before-notebook.d
137138

139+
COPY 10activate-conda-env.sh /usr/local/bin/before-notebook.d/
140+
138141
# Switch back to jovyan to avoid accidental container runs as root
139142
USER ${NB_UID}
140143

tests/base-notebook/test_start_container.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_start_notebook(
4848
ports={"8888/tcp": host_port},
4949
)
5050
# sleeping some time to let the server start
51-
time.sleep(1)
51+
time.sleep(2)
5252
logs = running_container.logs().decode("utf-8")
5353
LOGGER.debug(logs)
5454
# checking that the expected command is launched

0 commit comments

Comments
 (0)