/
host-jupyter-using-docker.sh
executable file
·71 lines (64 loc) · 2.17 KB
/
host-jupyter-using-docker.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash -ue
#
# This script requires that Docker (or podman) is installed.
#
# Arguments: mount-path, port-number, Dockerfile-path
#
# To host a local jupyter notebook server rung e.g.:
#
# $ ./scripts/host-jupyter-using-docker.sh
# $ ./scripts/host-jupyter-using-docker.sh . 8888 ./scripts/environment
#
# To instead run the test suite, specify "0" as the port number:
#
# $ ./scripts/host-jupyter-using-docker.sh . 0 ./scripts/environment
#
MOUNT=${1:-.}
PORT=${2:-8888}
DOCKERIMAGE=${3:-./scripts/environment}
PKG=$(find . -maxdepth 2 -name __init__.py -print0 | xargs -0 -n1 dirname | xargs basename)
HOST_USER=${SUDO_USER:-${LOGNAME}}
if [[ "${HOST_USER}" == root ]]; then
>&2 echo "Need another user name than root (pip will fail)"
exit 1
fi
if which podman; then
PODMAN=podman
elif which docker; then
if groups | grep docker; then
PODMAN=docker
else
PODMAN="sudo docker"
fi
else
>&2 echo "Neither podman nor docker found on \$PATH"
exit 1
fi
if [[ "$MOUNT" == .* ]]; then
MOUNT="$(pwd)/$MOUNT"
fi
if [[ "$DOCKERIMAGE" == ./* ]]; then
DOCKERIMAGE=$($PODMAN build $DOCKERIMAGE | tee /dev/tty | tail -1 | cut -d' ' -f3)
fi
if [[ "$PORT" == "0" ]]; then
LOCALCMD="pytest -sv -ra --pyargs $PKG"
PORTFWD=""
else
LOCALCMD="jupyter notebook --no-browser --port $PORT --ip=* index.ipynb"
PORTFWD="-p ${4:-127.0.0.1}:$PORT:$PORT"
fi
MYCMD="\
groupadd -f --gid \$HOST_GID \$HOST_WHOAMI; \
useradd --uid \$HOST_UID --gid \$HOST_GID --home /mount/.env \$HOST_WHOAMI; \
sudo -u \$HOST_WHOAMI PYCVODES_NO_LAPACK=1 PYCVODES_NO_KLU=1 python3 -m pip install --cache-dir /mount/.env/pypi-cache --user -e .[all]; \
sudo -u \$HOST_WHOAMI /mount/.env/.local/bin/jupyter-nbextension enable --user --py widgetsnbextension; \
sudo -u \$HOST_WHOAMI LD_LIBRARY_PATH=/usr/local/lib MPLBACKEND=Agg PYTHONPATH=/mount /mount/.env/.local/bin/$LOCALCMD"
set -x
mkdir -m 777 -p .env
$PODMAN run \
--rm \
--name "${PKG}_nb_${PORT}" $PORTFWD \
-e HOST_WHOAMI=${HOST_USER} \
-e HOST_UID=$(id -u ${HOST_USER}) \
-e HOST_GID=$(id -g ${HOST_USER}) \
-v $MOUNT:/mount -w /mount -it $DOCKERIMAGE /bin/bash -x -c "$MYCMD"