jupyter_container.sh 2 KB
Newer Older
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
#!/bin/bash

dir="`dirname "$(readlink -f "$0")"`"

jupyter_port="8888"

args=("$@")

usage()
{
    echo "Help:"
    echo "-n, --name - name of a container. Default is" $USER"_work."
    echo "-i, --interactive - whether to start the command in interactive mode."
    echo "-d, --detach - whether to detach shell from the command."
    echo "-r, --run - whether to run container if it is not running."
    echo "-e, --exec - whether to execute command if the container is already running."
    echo "-h, --help - show this message."
    echo "-p, --port - choose a port to forward from the container. The first port will be used for jupyter lab and should be specified as a number. The default is 8888."
    echo "             For example, if you specify port as a number, e.g. 8888 (standard Jupyter port), container port 8888 will be forwarded to host port 8888. "
    echo "             Otherwise, you can specify port as <host port>:<container port> (i.e. 8880:80) if you want to connect it to a different host port."
    echo ""
    echo "Note: if neither -r nor -e are specified, the default mode is to allow both."
}

for (( i=0; i<$#; i++ )); do
    arg=${args[i]}
    if [[ $arg == "-p" ]] || [[ $arg == "--port" ]] ; then
        jupyter_port=${args[i+1]}
        args=( "${args[@]:0:$i}" "${args[@]:$((i + 2))}" )
        break
    elif [[ $arg == "-h" ]] || [[ $arg == "--help" ]] ; then
        usage
        exit
    fi
done

for i in {1..10}; do
    nc -z 127.0.0.1 $jupyter_port
    if [ $? == 1 ]; then
        echo "Port $jupyter_port is available"
        break
    else
        echo "Port $jupyter_port is not available"
        jupyter_port=$((jupyter_port+1))
        echo "Trying port $jupyter_port..."
    fi
done

args+=( '-p' $jupyter_port )

echo "Use Ctrl+P,Q to detach from container. Use \"docker attach <container name>\" if you need to check Jupyter status or stop it."

$dir/execute_in_container.sh -i "${args[@]}" jupyter lab  --notebook-dir=/$USER/work/ --no-browser --ip=0.0.0.0 --allow-root --port=$jupyter_port