首页 > 解决方案 > Run Jupyter lab container on given virtualenv

问题描述

I want to use a Jupyter lab inside the virtualenv on a Docker container. I tried to run Jupyter lab container inside the virtualenv, but cannot because $PATH variable is wrong.

Background: I'm using a kind of hpc cluster, so we don't have much permissions. I must run Jupyter lab on a read-only docker container. My home and work directories are mounted. But non-root users cannot build any new container images (I am planning to propose this solution to admin).

Requirements: I want to install new packages that is not provided on the read-only container. And I want to install new packages to the specified location.

So I want to create a Docker image that runs Jupyter lab in the virtualenv if the $VENV_PATH environment variable is given.

I created a Dockerfile and a script based on jupyter/datascience-notebook image on DockerHub.

Dockerfile:

FROM jupyter/datascience-notebook

USER $NB_UID
COPY start-venv-notebook.sh /usr/local/bin/
CMD ["start-venv-notebook.sh"]

start-venv-notebook.sh:

#!/bin/bash

set -e

if [[ ! -z "${VENV_PATH}" ]]; then
    abspath=$(realpath "${VENV_PATH}")
    if [ ! -e "${abspath}" ]; then
        # create venv
        python -m venv "${abspath}"
    fi
    # activate venv
    export VIRTUAL_ENV=$abspath
    export PATH="$abspath/bin:$PATH"
fi

# exec original CMD
exec /usr/local/bin/start-notebook.sh "$@"

And I want to use this container like this:

docker run -p 8888:8888 -e VENV_PATH=testvenv -e JUPYTER_ENABLE_LAB=yes venvjupyterlab

However, when I run this container, it does not run in the virtualenv. I think the cause is from $PATH. /opt/conda/bin is added from somewhere. I cannot find the point.

$ echo $PATH
/opt/conda/bin:/home/jovyan/testvenv/bin:/opt/conda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Plsease give me some help or tell me other solutions. Thanks.

标签: pythondockerjupyter-notebookjupyter

解决方案


推荐阅读