Files
docker-stacks/base-notebook/start.sh
Troels Schwarz-Linnet 0766b5f005 This is to fix running SUDO as eihter the jovyan user or root.
Please refer to alias, in bottom of commit.

> dbn
Execute the command: jupyter notebook

> dbn bash
jovyan@eedcc93a837d:~$

> dbn start.sh
Execute the command:
exit

> dbnr
Set username to: jovyan
usermod: no changes
Set jovyan GID to: 100
Execute the command: jupyter notebook
Running as root is not recommended. Use --allow-root to bypass.
exit

> dbnr bash
root@893cb78b8c9c:~#

> dbnr start.sh
Set username to: jovyan
usermod: no changes
Set jovyan GID to: 100
Execute the command:
No arguments supplied
HOSTNAME=d45c52e788b7
TERM=xterm
SHELL=/bin/bash
NB_USER=jovyan
LC_ALL=en_US.UTF-8
PATH=/opt/conda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env
MINICONDA_VERSION=4.3.30
PWD=/home/jovyan
LANG=en_US.UTF-8
HOME=/home/jovyan
SHLVL=2
LANGUAGE=en_US.UTF-8
no_proxy=*.local, 169.254/16
DEBIAN_FRONTEND=noninteractive
CONDA_DIR=/opt/conda
NB_GID=100
NB_UID=1000
root@d45c52e788b7:~#

> dbnrs
Set username to: jovyan
usermod: no changes
Set jovyan GID to: 100
Granting jovyan sudo access
Execute the command: jupyter notebook
Copy/paste this URL into your browser when you connect for the first time, ....

> dbnrs bash
root@f293dce949db:~#

> dbnrs start.sh
Set username to: jovyan
usermod: no changes
Set jovyan GID to: 100
Granting jovyan sudo access
Execute the command:
No arguments supplied
HOSTNAME=d0cd57ea32e2
SHELL=/bin/bash
TERM=xterm
LC_ALL=en_US.UTF-8
USER=jovyan
SUDO_USER=root
SUDO_UID=0
USERNAME=jovyan
MAIL=/var/mail/jovyan
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
PWD=/home/jovyan
LANG=en_US.UTF-8
SHLVL=1
SUDO_COMMAND=/bin/bash -c env; PATH=$PATH; bash
HOME=/home/jovyan
LANGUAGE=en_US.UTF-8
LOGNAME=jovyan
SUDO_GID=0
_=/usr/bin/env
jovyan@d0cd57ea32e2:~$

> dbns
Container must be run as root to grant sudo permissions
Execute the command: jupyter notebook
Copy/paste this URL in..

> dbns bash
jovyan@ce5c2491fa32:~$

> dbns start.sh
Container must be run as root to grant sudo permissions
Execute the command:
exit

###### Build user setup
docker build -t $USER/base-notebook -f Dockerfile .

# Normal, dbn: docker-base-notebook
alias dbn='docker run -ti --rm -p 8888:8888 -v "$PWD":/home/jovyan/work --name base-notebook $USER/base-notebook'
# Root, dbnr: dbn with root
alias dbnr='docker run -ti --rm -p 8888:8888 --user root -v "$PWD":/home/jovyan/work --name base-notebook $USER/base-notebook'
# Jovyan SUDO, dbnr: dbn with SUDO for jovyan
alias dbnrs='docker run -ti --rm -p 8888:8888 --user root -e GRANT_SUDO=yes -v "$PWD":/home/jovyan/work --name base-notebook $USER/base-notebook'
# Root, fail to sudo for jovyan.
alias dbns='docker run -ti --rm -p 8888:8888 -e GRANT_SUDO=yes -v "$PWD":/home/jovyan/work --name base-notebook $USER/base-notebook'
2017-12-29 11:38:53 -05:00

79 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
set -e
# Handle special flags if we're root
if [ $(id -u) == 0 ] ; then
# Handle username change. Since this is cheap, do this unconditionally
echo "Set username to: $NB_USER"
usermod -d /home/$NB_USER -l $NB_USER jovyan
# handle home and working directory if the username changed
if [[ "$NB_USER" != "jovyan" ]]; then
# changing username, make sure homedir exists
# (it could be mounted, and we shouldn't create it if it already exists)
if [[ ! -e "/home/$NB_USER" ]]; then
echo "Relocating home dir to /home/$NB_USER"
mv /home/jovyan "/home/$NB_USER"
fi
# if workdir is in /home/jovyan, cd to /home/$NB_USER
if [[ "$PWD/" == "/home/jovyan/"* ]]; then
newcwd="/home/$NB_USER/${PWD:13}"
echo "Setting CWD to $newcwd"
cd "$newcwd"
fi
fi
# Change UID of NB_USER to NB_UID if it does not match
if [ "$NB_UID" != $(id -u $NB_USER) ] ; then
echo "Set $NB_USER UID to: $NB_UID"
usermod -u $NB_UID $NB_USER
fi
# Change GID of NB_USER to NB_GID if it does not match
if [ "$NB_GID" != $(id -g $NB_USER) ] ; then
echo "Set $NB_USER GID to: $NB_GID"
groupmod -g $NB_GID -o $(id -g -n $NB_USER)
fi
# Enable sudo if requested
if [[ "$GRANT_SUDO" == "1" || "$GRANT_SUDO" == 'yes' ]]; then
echo "Granting $NB_USER sudo access"
echo "$NB_USER ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/notebook
fi
# Exec the command as NB_USER or root
echo "Execute the command: $*"
if [[ "$GRANT_SUDO" == "1" || "$GRANT_SUDO" == 'yes' ]]; then
if [ $# -eq 0 ]; then
echo "No arguments supplied"
sudo -H -u $NB_USER bash -c 'env; PATH=$PATH; bash'
else
exec su $NB_USER -c "env PATH=$PATH $*"
fi
else
if [ $# -eq 0 ]; then
echo "No arguments supplied"
bash -c 'env; PATH=$PATH; bash'
else
env PATH=$PATH $*
fi
fi
else
if [[ ! -z "$NB_UID" && "$NB_UID" != "$(id -u)" ]]; then
echo 'Container must be run as root to set $NB_UID'
fi
if [[ ! -z "$NB_GID" && "$NB_GID" != "$(id -g)" ]]; then
echo 'Container must be run as root to set $NB_GID'
fi
if [[ "$GRANT_SUDO" == "1" || "$GRANT_SUDO" == 'yes' ]]; then
echo 'Container must be run as root to grant sudo permissions'
fi
# Exec the command
echo "Execute the command: $*"
exec $*
fi