Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FAST_BUILD] Clean conda env #2055

Closed
wants to merge 12 commits into from
16 changes: 4 additions & 12 deletions docs/using/recipe_code/custom_environment.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,8 @@ RUN "${CONDA_DIR}/envs/${env_name}/bin/pip" install --no-cache-dir \
# hadolint ignore=DL3059
RUN /opt/setup-scripts/activate_notebook_custom_env.py "${env_name}"

# Comment the line above and uncomment the section below instead to activate the custom environment by default
# Note: uncommenting this section makes "${env_name}" default both for Jupyter Notebook and Terminals
# Comment the line above and uncomment the line below instead
# to activate the "${env_name}" custom environment by default
# for both Jupyter Notebook(s) and Terminal(s)
# More information here: https://github.com/jupyter/docker-stacks/pull/2047
# USER root
# RUN \
# # This changes a startup hook, which will activate the 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}
ENV DOCKER_STACKS_CONDA_ENV "${env_name}"
8 changes: 7 additions & 1 deletion images/docker-stacks-foundation/10activate-conda-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
# 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)"
# Conda hook activates `base` environment by default
# Instead, we activate "${DOCKER_STACKS_CONDA_ENV}" by default
# This is done to provide cleaner environment when using custom conda environments
eval "$(conda shell.bash hook | sed 's/conda activate base//g')"
set +e
conda activate "${DOCKER_STACKS_CONDA_ENV}"
set -e
7 changes: 5 additions & 2 deletions images/docker-stacks-foundation/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,22 @@ ENV CONDA_DIR=/opt/conda \
LC_ALL=en_US.UTF-8 \
LANG=en_US.UTF-8 \
LANGUAGE=en_US.UTF-8
ENV PATH="${CONDA_DIR}/bin:${PATH}" \
ENV DOCKER_STACKS_CONDA_ENV=base \
PATH="${CONDA_DIR}/bin:${PATH}" \
HOME="/home/${NB_USER}"

# Copy a script that we will use to correct permissions after running certain commands
COPY fix-permissions /usr/local/bin/fix-permissions
RUN chmod a+rx /usr/local/bin/fix-permissions

COPY conda_env.bashrc /opt/

# 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 && \
# 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
cat /opt/conda_env.bashrc >> /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
4 changes: 4 additions & 0 deletions images/docker-stacks-foundation/conda_env.bashrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eval "$(conda shell.bash hook | sed 's/conda activate base//g')"
set +e
conda activate "${DOCKER_STACKS_CONDA_ENV}"
set -e
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
"PATH": f"{CONDA_DIR}/envs/{env_name}/bin:$PATH",
"CONDA_PREFIX": f"{CONDA_DIR}/envs/{env_name}",
"CONDA_PROMPT_MODIFIER": f"({env_name}) ",
"CONDA_SHLVL": "2",
"CONDA_SHLVL": "1",
"CONDA_DEFAULT_ENV": env_name,
"CONDA_PREFIX_1": CONDA_DIR,
}

file.write_text(json.dumps(content, indent=1))